Monday, 30 December 2013

MS Sql 2013 - How to fix error Must declare the scalar variable - another example

Replace

Declare @CustomerIDs VARCHAR(50) = '1,2'

declare @CustomerTbl table(Stat nvarchar(300))

insert into @CustomerTbl

select DISTINCT CustomerID

from CustomerMaster

WHERE ','+ Convert(varchar(50),@CustomerIDs) +',' like '%,'+ Convert(varchar(50),CustomerID) +',%'

Select * FROM CustomerMaster INNER JOIN

@CustomerTbl ON CONVERT(VARCHAR(300),CustomerID) = @CustomerTbl.Stat

With

Declare @CustomerIDs VARCHAR(50) = '1,2'

declare @CustomerTbl table(Stat nvarchar(300))

insert into @CustomerTbl

select DISTINCT CustomerID

from CustomerMaster

WHERE ','+ Convert(varchar(50),@CustomerIDs) +',' like '%,'+ Convert(varchar(50),CustomerID) +',%'

Select * FROM CustomerMaster INNER JOIN

@CustomerTbl ct  ON CONVERT(VARCHAR(300),CustomerID) = ct.Stat

No comments:

Post a Comment