Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: SQL Server
Expertise: Beginner
Jun 16, 2000



Application Security Testing: An Integral Part of DevOps

Passing a CSV List Within an IN Statement

I have a CSV list I am passing through as a parameter in a stored procedure.
Example of Region list:

CREATE PROCEDURE sp_getReport   @Regionlist 
varchar(100) AS ....... Select * from tblRegion where Region_ID IN (@regionlist) ---------------------------------
The varchar treats the list as one block of data. How can I change my code/syntax to read each value of the list separately within the IN statement?

When you pass variables that are part of the statement in a stored procedure, you need to use the EXEC(UTE) statement. Here's the syntax to call the stored procedure and modify the current stored procedure to take the regions list as a parameter (the only difference is I called the column RegionID instead of Region_ID):

sp_getreport '1,2,3,4,5,6,7,8,9,10'

CREATE PROCEDURE sp_getReport   
@regionlist varchar(100) 

EXEC ('Select * from tblRegion where RegionID IN 
(' + @regionlist +')')
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date