SQL Server 2005:如何拆分具有未知拆分次数的堆叠列
我在拆分堆叠列时遇到更多问题,并且希望获得一些帮助来完成最后一部分。我试图应用我拥有的另一个解决方案,但没有运气。
DB 表:
ID INT,
SN varchar(100),
Types varchar(1000)
示例:
ID SN Types
1 123 ABC,XYZ,TEST
2 234 RJK,CDF,TTT,UMB,UVX
3 345 OID,XYZ
所需输出:
ID SN Types
1 123 ABC
1 123 XYZ
1 123 TEST
....
I am having more issues with splitting stacked columns, and would love some help to complete this last part. I was trying to apply another solution I had, but with no luck.
DB Table:
ID INT,
SN varchar(100),
Types varchar(1000)
Sample:
ID SN Types
1 123 ABC,XYZ,TEST
2 234 RJK,CDF,TTT,UMB,UVX
3 345 OID,XYZ
Desired output:
ID SN Types
1 123 ABC
1 123 XYZ
1 123 TEST
....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个 cte 我必须分解一个分隔字符串
here's a cte i have to break up a delimited string
我使用递归 CTE 来分割字符串。第三列
Types
使用 @T 的 Types 列中的第一个单词填充。然后,Stuff
将删除第一个单词并填充 Rest 列,该列随后将包含除第一个单词之外的所有内容。 UNION ALL 之后是递归部分,基本上执行完全相同的操作,但它使用 CTE 作为源,并使用rest
列来选择第一个单词。rest
列的第一个单词被stuff
删除,然后......它是递归的,所以我想我会在这里停止解释。当没有剩余单词时,递归部分将结束其中 len(Rest) > 0 。
I use a recursive CTE to split the string. The third column
Types
is populated with the first word in the Types column of @T.Stuff
will then remove the first word and populate the Rest column that then will contain everything but the first word. After UNION ALL is the recursive part that basically do the exact same thing but it uses the CTE as a source and it uses therest
column to pick the first word. The first word of therest
column is removed withstuff
and then ..... well it is recursive so I think I will stop here with the explanation. The recursive part will end when there are no more words leftwhere len(Rest) > 0
.据我所知,您将需要使用游标和 while 语句...其中一些索引可能会偏离一个,但我认为这应该可以帮助您实现...
You will need to use a cursor and a while statement as far as I can tell... Some of these indexes may be off by one, but I think this should get you there...