I always liked this method...
CREATE FUNCTION dbo.Split(@String varchar(max), @Delimiter char(1))
returns @temptable TABLE (Value varchar(max))
as
begin
declare @idx int
declare @slice varchar(max)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
then you can do this...
CREATE PROCEDURE MySp
@list varchar(max)
AS
SELECT <columns>
FROM <mytable> mt
INNER JOIN dbo.split(@list,',') s ON s.Value= my.Key
NOTE: There are many Split functions out there so you do not have to use this specific one.
Another method I have used when using SQL Server 2008 is using a table parameter like this...
CREATE TYPE [dbo].[LookupTable] As Table
(
ID Int primary key
)
CREATE PROCEDURE [dbo].[SampleProcedure]
(
@idTable As [dbo].[LookupTable] Readonly
)
AS
BEGIN
SELECT <columns>
FROM <mytable> mt
INNER JOIN @idTable s ON s.Id= my.Key
END
Pass the parameter into SQL Server from C# in this manner...
DataTable dataTable = new DataTable("SampleDataType");
dataTable.Columns.Add("Id", typeof(Int32));
foreach (var id in <mycollectionofids>)
dataTable.Rows.Add(id);
SqlParameter parameter = new SqlParameter();
parameter.ParameterName="@Id";
parameter.SqlDbType = System.Data.SqlDbType.Structured;
parameter.Value = dataTable;
command.Parameters.Add(parameter);