且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

拆分数据库行中的字符串并将结果复制到不同的行 - SQL Server

更新时间:2023-01-30 19:37:28

这是一个使用 SQL Server 2005 及更高版本中的 XML 功能的简单示例.我已经从这里逐字逐句地摘录了它,但是如果您有很多例子谷歌拆分字符串sql server xml"

Here's a simple example using the XML features in SQL Server 2005 and above. I've taken it verbatim from here but there are lots of examples if you google "split string sql server xml"

DECLARE @xml as xml,@str as varchar(100),@delimiter as varchar(10)
SET @str='A,B,C,D,E'
SET @delimiter =','
SET @xml = cast(('<X>'+replace(@str,@delimiter ,'</X><X>')+'</X>') as xml)
SELECT N.value('.', 'varchar(10)') as value FROM @xml.nodes('X') as T(N)

还有其他使用游标的解决方案,但这种方法对我来说效果很好.

There are other solutions with cursors but this approach as worked well for me.