我有一张像
这样的表格create table Test_Table(LoginID nvarchar(max),Team nvarchar(max))
它有一些数据,如
insert into Test_Table values
('kbcg','Administrator,Teacher,Student'),
('kbc','Admin,Teacher,Principal')
现在,我希望数据显示为
LoginID | Team
kbcg | Administrator
kbcg | Teacher
kbcg | Student
kbc | Admin
依旧......
需要一个功能。 提前致谢
答案 0 :(得分:1)
像
这样的东西DECLARE @TABLE TABLE(
LoginID NVARCHAR(MAX),
Team nvarchar(max)
)
INSERT INTO @TABLE values
('kbcg','Administrator,Teacher,Student'),
('kbc','Admin,Teacher,Principal')
;WITH Vals AS (
SELECT LoginID,
CAST('<d>' + REPLACE(Team, ',', '</d><d>') + '</d>' AS XML) XMLCol
FROM @TABLE
)
SELECT Vals.LoginID,
B.value('.','NVARCHAR(MAX)') Team
FROM Vals CROSS APPLY
XMLCol.nodes('/d') AS A(B)