I\'m writing an SQL query as follows:
ALTER proc [dbo].[Invoice_GetHomePageInvoices] (
@AreaIdList varchar(max)
, @FinancialYearStartDate datetime = n
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)