我会推荐一个 CLR 表值函数。将这个函数转换为处理整数应该不需要太多。
然后你可以说:
SELECT t.Columns
FROM dbo.YourTable AS t
INNER JOIN dbo.CLRFunctionName(@IntegerList) AS f
ON t.ColumnName = f.Item;
对于 XML(在您无法实现 CLR 的情况下),您可以这样做:
CREATE FUNCTION [dbo].[SplitInts]
(
@List VARCHAR(MAX),
@Delimiter CHAR(1)
)
RETURNS TABLE
AS
RETURN ( SELECT Item FROM ( SELECT Item = x.i.value('(./text())[1]', 'int') FROM
( SELECT [XML] = CONVERT(XML, '<i>' + REPLACE(@List, @Delimiter, '</i><i>')
+ '</i>').query('.') ) AS a CROSS APPLY [XML].nodes('i') AS x(i)
) AS y WHERE Item IS NOT NULL
);
GO
并做同样的事情...
SELECT t.Columns
FROM dbo.YourTable AS t
INNER JOIN dbo.SplitInts(@IntegerList) AS f
ON t.ColumnName = f.Item;
在这两种情况下,都不需要临时表或表变量。