SQL how to get an equality comparator on a list of ints

后端 未结 1 362
别那么骄傲
别那么骄傲 2021-01-24 06:36

I\'m writing an SQL query as follows:

ALTER proc [dbo].[Invoice_GetHomePageInvoices] (
      @AreaIdList varchar(max)
,      @FinancialYearStartDate datetime = n         


        
1条回答
  •  一整个雨季
    2021-01-24 06:55

    Unpack your ID list to a table and use where AreadID in (select ID from ...)

    ALTER proc [dbo].[Invoice_GetHomePageInvoices] (
           @AreaIdList varchar(max)
    ,      @FinancialYearStartDate datetime = null
    ,      @FinancialYearEndDate datetime = null
    ) as
    
    set nocount on
    
    set @AreaIdList = @AreaIdList+','
    declare @T table(ID int primary key)
    while len(@AreaIdList) > 1
    begin
      insert into @T(ID) values (left(@AreaIdList, charindex(',', @AreaIdList)-1))
      set @AreaIdList = stuff(@AreaIdList, 1, charindex(',', @AreaIdList), '')
    end
    
    select *
    from Invoice i
      left outer join Organisation o on i.OrganisationId = o.Id
      left outer join Area a on i.AreaId = a.Id
    where i.InvoiceDate BETWEEN @FinancialYearStartDate AND @FinancialYearEndDate and
          i.AreadID in (select ID from @T) 
    

    0 讨论(0)
提交回复
热议问题