在向您介绍SO或在此处回答它之间,我感到很痛苦,因为这几乎是一个编程问题。但是因为我已经有了解决方案,所以我使用...我将其发布;)
这种方法的工作方式是,您将以逗号分隔的字符串(简单拆分,不执行CSV样式拆分)作为varchar(4000)馈入存储过程,然后将该列表馈入此函数并得到一个方便的表,一张只有varchars的表。
这样,您就可以仅发送要处理的id的值,然后可以进行简单的连接。
或者,您可以使用CLR DataTable进行某些操作并将其输入,但这需要更多的支持开销,并且每个人都可以理解CSV列表。
USE [Database]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[splitListToTable] (@list nvarchar(MAX), @delimiter nchar(1) = N',')
RETURNS @tbl TABLE (value varchar(4000) NOT NULL) AS
/*
http://www.sommarskog.se/arrays-in-sql.html
This guy is apparently THE guy in SQL arrays and lists
Need an easy non-dynamic way to split a list of strings on input for comparisons
Usage like thus:
DECLARE @sqlParam VARCHAR(MAX)
SET @sqlParam = 'a,b,c'
SELECT * FROM (
select 'a' as col1, '1' as col2 UNION
select 'a' as col1, '2' as col2 UNION
select 'b' as col1, '3' as col2 UNION
select 'b' as col1, '4' as col2 UNION
select 'c' as col1, '5' as col2 UNION
select 'c' as col1, '6' as col2 ) x
WHERE EXISTS( SELECT value FROM splitListToTable(@sqlParam,',') WHERE x.col1 = value )
*/
BEGIN
DECLARE @endpos int,
@startpos int,
@textpos int,
@chunklen smallint,
@tmpstr nvarchar(4000),
@leftover nvarchar(4000),
@tmpval nvarchar(4000)
SET @textpos = 1
SET @leftover = ''
WHILE @textpos <= datalength(@list) / 2
BEGIN
SET @chunklen = 4000 - datalength(@leftover) / 2
SET @tmpstr = @leftover + substring(@list, @textpos, @chunklen)
SET @textpos = @textpos + @chunklen
SET @startpos = 0
SET @endpos = charindex(@delimiter, @tmpstr)
WHILE @endpos > 0
BEGIN
SET @tmpval = ltrim(rtrim(substring(@tmpstr, @startpos + 1,
@endpos - @startpos - 1)))
INSERT @tbl (value) VALUES(@tmpval)
SET @startpos = @endpos
SET @endpos = charindex(@delimiter, @tmpstr, @startpos + 1)
END
SET @leftover = right(@tmpstr, datalength(@tmpstr) / 2 - @startpos)
END
INSERT @tbl(value) VALUES (ltrim(rtrim(@leftover)))
RETURN
END