Sql server How to Use Comma Seperated Value in Subquery

–Declare Two Variable
–One For Comma Sepearated Value and the Other For Query
declare @test nvarchar(4000)
declare @values nvarchar (200)
–Tale your Comma seperated value i.e. 14,16,3,18 in @@values
set @values=(Select left(Empid,Len(EmpId)-1) as  EmployeeId  from TableName Where ColumnName=4)
–Add that @values in Test Which is nvarchar Qoery
set @test=’select * from Employee where EmployeeId in (‘+ (@values)+’)’
— Finally Execute that @test Query
EXEC(@test)

In Short

declare @test nvarchar(4000)
declare @values nvarchar (200)
set @values=(Select left(Empid,Len(EmpId)-1) as  EmployeeId  from TableName Where ColumnName=4)
set @test=’select * from Employee where EmployeeId in (‘+ (@values)+’)’
EXEC(@test)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s