本文介绍了单独的主&单个表中的子表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
输入
Name Ids
------------
a 1,2,3
b 1,2
输出
Output
Name Ids
------------
a 1
a 2
a 3
b 1
b 2
需要帮助解决此问题
:)
need help to solve this
:)
推荐答案
select A.ID, b.Val
from A
cross apply dbo.ParseCSV(A.CSV) b
就我而言,查询就像这样
in my case query is like this
select name,b.id as ids
from mytbl as a
cross apply dbo.fn_splitter(a.ids) as b
dbo.fn_splitter 是我用来按逗号分隔ids列的值的功能
祝您编码愉快!
:)
dbo.fn_splitter is function i use to split values of ids column by comma
Happy Coding!
:)
WITH Namescte AS (
SELECT Name, CAST('<i>' + REPLACE([Ids], ',', '</i><i>') +
'</i>' AS XML) AS ids
FROM YourTable
)
SELECT
Name, x.i.value('.', 'VARCHAR(10)') AS ids
FROM Namescte
CROSS APPLY ids.nodes('//i') x(i)
谢谢
Thank you
这篇关于单独的主&单个表中的子表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!