Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass MultiValue Parameter to Store Procedure

Status
Not open for further replies.

johnnieh

Programmer
Jun 6, 2002
4
US
I need to create and call a store procedure that has a Select statement with an IN clause in the WHERE Condition.
The data type of the field is int.
How do I pass the variable to the IN clause?

Stored Procedure:
sp_GetRecords
@IDList
AS
SELECT *
FROM TABLE
WHERE IDField IN @IDList
 
Dynamic SQL will solve this. Create the procedure thusly:

Code:
sp_GetRecords
  @IDList varchar(200)
AS
BEGIN
  DECLARE @sql varchar(500)

  SET @sql = "SELECT * FROM table WHERE IDField IN (" + @IDList + ")"

  EXEC (@sql)
END

Pass the list of IDs to the stored procedure as a comma-separated list:

Code:
sp_GetRecords '1,3,5,7,9,11'


--Angel
-----------------------------------
SELECT * FROM users WHERE clue > 0
0 row(s) selected
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top