sql server 数据行转表

 1 DECLARE @code NVARCHAR(500)
 2 DECLARE @table TABLE
 3 (
 4     code NVARCHAR(500)
 5 )
 6 
 7 DECLARE myCursor CURSOR FOR
 8 SELECT code from tablename
 9 
10 OPEN myCursor
11 
12 FETCH NEXT FROM myCursor
13 INTO @code
14 
15 WHILE @@FETCH_STATUS = 0
16 BEGIN
17     INSERT @table
18     (
19         code
20     )
21     SELECT value
22     FROM STRING_SPLIT(@code, ',')
23 
24     FETCH NEXT FROM myCursor
25     INTO @code
26 END
27 
28 CLOSE myCursor
29 DEALLOCATE myCursor
30 
31 SELECT DISTINCT * FROM @table

 

posted on 2021-09-01 09:13  木龙哥  阅读(104)  评论(0编辑  收藏  举报

导航