SQL : in clause in stored procedure:how to pass values

Viewed 62518

I want to write a SQL Server 2005 stored procedure which will select and return the user records from the user table for some userids which are passed to the stored procedure as parameter.

How to do this ?

I can pass the user ids as a string separated by comma. So that I can use the

select * 
from users 
where userid in (userids)

E.g. : I want to select records for id's 5,6,7,8,9

How to write the stored procedure ?

8 Answers

Quick and dirty..

CREATE PROCEDURE SelectUsers (@UserIds VARCHAR(8000))
AS
SELECT * FROM Users 
WHERE userid IN (SELECT CONVERT(VARCHAR(8000), value) FROM STRING_SPLIT(@UserIds, ','))
EXEC SelectUsers @UserIds = 'a1b2,c3d4,e5f6'
Related