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
(
@id varchar(100)
)
as
select id,names from testing
where charindex(','+CAST(id as varchar(10))+',',','+@id+',')>0
go
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
1 comment:
How will this perform on a large table? I have a feeling it's not going to scale, but it's a good solution for a small data set.
Thanks for sharing it!
Post a Comment