我正在使用SQL Server 2008,并且我将包含@list
的参数'1,2,3,4'
作为值传递给存储过程。 @list
应转换为'1','2','3','4'
。我必须使用哪个查询?
答案 0 :(得分:0)
我正在使用这样的解决方案:
DECLARE @list VARCHAR(100) = '1,2,3,4'
SET @list = '<N>' + Replace(@list, ',', '</N><N>') + '</N>'
;WITH cte
AS (SELECT c1.value('.', 'varchar(30)') AS List
FROM (SELECT Cast(@list AS XML)) t(c)
CROSS apply c.nodes('/N') AS t1(c1))
SELECT *
FROM cte
--join abc on cte.List = abc.Column
注意:如果您只想将数字放在单引号之间,那么:
DECLARE @list VARCHAR(100) = '1,2,3,4'
SET @list = '''' + REPLACE(@list, ',', ''',''') + ''''
SELECT @list