devxlogo

Passing a CSV List Within an IN Statement

Question:
I have a CSV list I am passing through as a parameter in a stored procedure.

-------------------------------Example of Region list:1,2,3,4,5,6,7,8,9,10CREATE PROCEDURE sp_getReport   @Regionlist 
varchar(100) AS .......Select * from tblRegionwhere 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?

Answer:
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) ASEXEC ('Select * from tblRegion where RegionID IN 
(' + @regionlist +')')

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Five Early Architecture Decisions That Quietly Get Expensive

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.