table列作为函数的参数

时间:2014-04-24 04:40:15

标签: sql sql-function

我有一张像

这样的表格
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

依旧......

需要一个功能。 提前致谢

1 个答案:

答案 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)

SQL Fiddle DEMO