、が「」で列を分割して、XMLです 『;』行
DECLARE @PARAM_STRING VARCHAR(100)='101,1,1,1,5; 102,1,1,1,4; 103,1,1,1,3;11,11,11,11,11;12,12,12,12,12;'
DECLARE @DYNAMIC_QUERY VARCHAR(MAX)
DECLARE @TABLE TABLE(ID INT,DATA VARCHAR(MAX))
INSERT INTO @TABLE
SELECT 1 ID, 'SELECT '+DATA FROM (
SELECT A.B.value('.','VARCHAR(50)')DATA FROM
(SELECT CAST('<A>'+REPLACE(@PARAM_STRING,';','</A><A>')+'</A>' AS XML)COL)T
CROSS APPLY T.COL.nodes('/A') AS A(B))F WHERE DATA<>''
SELECT @DYNAMIC_QUERY=STUFF((SELECT ' UNION ' + CAST(DATA AS VARCHAR(MAX)) [text()]FROM @TABLE WHERE ID = t.ID
FOR XML PATH(''), TYPE).value('.','NVARCHAR(MAX)'),1,7,' ')
FROM @TABLE t GROUP BY ID
EXECUTE(@DYNAMIC_QUERY)
結果:
(No column name) (No column name) (No column name) (No column name) (No column name)
11 11 11 11 11
12 12 12 12 12
101 1 1 1 5
102 1 1 1 4
103 1 1 1 3
こんにちは、本当にありがとう、私は何を探していますか?貴重な時間をありがとう。 –