Vous pouvez passer une liste séparée par des virgules, utiliser une fonction de table pour la diviser en une table, puis utiliser une clause IN. This article va plus loin.
fonction de table
:
CREATE FUNCTION dbo.funcListToTableInt(@list as varchar(8000), @delim as varchar(10))
RETURNS @listTable table(
Value INT
)
AS
BEGIN
--Declare helper to identify the position of the delim
DECLARE @DelimPosition INT
--Prime the loop, with an initial check for the delim
SET @DelimPosition = CHARINDEX(@delim, @list)
--Loop through, until we no longer find the delimiter
WHILE @DelimPosition > 0
BEGIN
--Add the item to the table
INSERT INTO @listTable(Value)
VALUES(CAST(RTRIM(LEFT(@list, @DelimPosition - 1)) AS INT))
--Remove the entry from the List
SET @list = right(@list, len(@list) - @DelimPosition)
--Perform position comparison
SET @DelimPosition = CHARINDEX(@delim, @list)
END
--If we still have an entry, add it to the list
IF len(@list) > 0
insert into @listTable(Value)
values(CAST(RTRIM(@list) AS INT))
RETURN
END
GO
Ensuite, votre procédure stockée peut faire:
SELECT *
FROM tbl
WHERE id IN (
SELECT Value
FROM funcListToTableInt(@ids,',')
)
quel type de base de données? MS SQL? 2005? 2008? Oracle? MySQL? – Arthur
J'utilise MS SQL 2005 –