Possible Duplicate:
Parameterizing an SQL IN clause?
Passing an array of parameters to a stored procedure
I need to pass a collection of unique identifiers to my stored procedure. I wrote this query to test what I want to do:
SELECT *
FROM MyTable
WHERE MyTable.TypeId IN ('852D7F39-302A-4E35-94D4-9A0335F8E9DC,1DF6EAA0-D0EC-4E4D-9E6B-1779F0E42A82')
This query runs correctly. So I created this stored procedure:
CREATE PROCEDURE [dbo].[MySproc] (
@TypeIdsList varchar(8000)
) AS
BEGIN
EXEC(
'SELECT *
FROM MyTable
WHERE MyTable.TypeId IN (' + @TypeIdsList + ')'
)
END
But when I execute it with some sample data:
DECLARE @return_value int
EXEC @return_value = [dbo].[MySproc]
@TypeIdsList = N'852D7F39-302A-4E35-94D4-9A0335F8E9DC,1DF6EAA0-D0EC-4E4D-9E6B-1779F0E42A82'
SELECT 'Return Value' = @return_value
GO
I get this error:
Incorrect syntax near 'D7F39'.
Anyone know what I'm doing wrong?