I am a beginner in SQL.This may be a old post but I did not get any simplified solution for that.
My concern is how to use IN clause in dynamic SQL where the IN items are based on the parameters having multiple values.
I have a table named Employee.
I have written a dynamic SQL that will return the employee lists where Department = 'IT,System'
SQL:
Declare @Department varchar(50)
Declare @SQL nvarchar(MAX)
Declare @Parameters nvarchar(50)
Set @Department='IT,System'
SET @SQL = 'select * from Employee where Department IN(@PDepartment)'
SET @Parameters=N'@PDepartment nvarchar(50)'
--PRINT @SQL
EXEC sp_Executesql @SQL,@Parameters,@PDepartment=@Department
This script is not working for more than one departments and returning no result. The same query is returning result if @Department = 'IT'
Here I have mentioned a small part of the real scenario. In the actual case the table name, the column name that will include in IN operator are both dynamic and will be accepted as stored proc parameter.
Thanks for the help.
