vedere my previous answer to this
questa è la migliore fonte:
http://www.sommarskog.se/arrays-in-sql.html
creare una funzione split, e utilizzarlo come:
SELECT
*
FROM YourTable y
INNER JOIN dbo.splitFunction(@Parameter) s ON y.ID=s.Value
I prefer the number table approach
Per questo meto d al lavoro, è necessario fare questa configurazione tavolo una volta:
SELECT TOP 10000 IDENTITY(int,1,1) AS Number
INTO Numbers
FROM sys.objects s1
CROSS JOIN sys.objects s2
ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)
Una volta che i numeri della tabella viene istituito, a creare questa funzione:
CREATE FUNCTION [dbo].[FN_ListToTable]
(
@SplitOn char(1) --REQUIRED, the character to split the @List string on
,@List varchar(8000)--REQUIRED, the list to split apart
)
RETURNS TABLE
AS
RETURN
(
----------------
--SINGLE QUERY-- --this will not return empty rows
----------------
SELECT
ListValue
FROM (SELECT
LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue
FROM (
SELECT @SplitOn + @List + @SplitOn AS List2
) AS dt
INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
WHERE SUBSTRING(List2, number, 1) = @SplitOn
) dt2
WHERE ListValue IS NOT NULL AND ListValue!=''
);
GO
Si può ora facilmente dividere una stringa CSV in un tavolo e di unirsi a esso:
select * from dbo.FN_ListToTable(',','1,2,3,,,4,5,6777,,,')
USCITA:
ListValue
-----------------------
1
2
3
4
5
6777
(6 row(s) affected)
Il tuo può passare in una stringa CSV in una procedura e di processo solo le righe per gli ID indicati:
SELECT
y.*
FROM YourTable y
INNER JOIN dbo.FN_ListToTable(',',@GivenCSV) s ON y.ID=s.ListValue
Questa è una domanda comune. Ho un'opzione qui: http://stackoverflow.com/questions/977021/can-a-stored-procedure-have-dynamic-parameters-to-be-used-in-an-in-clause/977114#977114 – ScottE