假设我有以下名称和逗号分隔的电子邮件地址列表:
SELECT 'a' name, 'bob1@bob.com, bob2@bob.com' email INTO #Temp UNION ALL
SELECT 'b', 'john@john.com' UNION ALL
SELECT 'c', 'pete@pete.com, andrew@andrew.com, will@will.com'
SELECT * FROM #temp
是否可以为每个电子邮件地址选择一行?
即
a bob1@bob.com
a bob2@bob.com
b john@john.com
c pete@pete.com
c andrew@andrew.com
c will@will.com
答案 0 :(得分:3)
请尝试:
SELECT A.name,
Split.a.value('.', 'VARCHAR(100)') AS CVS
FROM
(
SELECT name,
CAST ('<M>' + REPLACE(email, ',', '</M><M>') + '</M>' AS XML) AS CVS
FROM #Temp
) AS A CROSS APPLY CVS.nodes ('/M') AS Split(a)