In your application, you may need to pass multiple values as a parameter to a stored procedure. This article shows how to pass delimited values to the procedure and get data
Consider this table:
Now if the id’s '1,4' are passed as a parameter, the records for these ids 1 and 4 should be returned. Create the following procedure
create procedure proc_testing
select id,names from testing
where charindex(','+CAST(id as varchar(10))+',',','+@id+',')>0
In the stored procedure shown above, the WHERE condition checks if the value of id is found in the parameter string. The commas are added on both sides for accuracy.
Execute this procedure by passing ids, as shown below:
EXEC proc_testing '1,4'
The result includes records for the id’s 1 and 4
EXEC proc_testing '2,3,5'
The result includes records for the ids 2,3 and 5 as shown below
Did you like this post?
|subscribe via rss||subscribe via e-mail|
|print this post||follow me on twitter|