The long way is to create a wrapper that does this - a function that takes a list of states and adds them to the final table to be returned.
You can also use any technology that calls this procedure to concatenate records (i.e. when .NET adds a result set for each state you are looking at)
If youβd like to go to the state list with the 'state' parameter, you can create a dynamic SQL query
CREATE PROCEDURE [dbo].[MyStored] @state nvarchar(150) AS -- @state needs to be pre-formatted in a list for an in-clause -- ie 1,2,10 (if it was a string list, you'd need to do use double single quotes around the items - ''1'',''2'',''10'' DECLARE @SQL nVarChar(5000) = ' SELECT blahblahblah FROM LotsOfJoins WHERE StoredState in (' + @state + ')' exec sp_executeSql @sql
This works great for simple procedures; although it may take longer to maintain if changes are needed in the future.
.
Here is the CodeProject Article and MS SQL Tips Article , which does the best job in detail
.
EDIT: param @state should be nVarChar as you are passing a list of int values ββwith commas.
Ray k
source share