SQL Server 2005中利用xml拆分字符串序列


问题:SQL Server 2005中如何利用xml拆分字符串序列?

解答:下文中介绍的方法比替换为select union all方法更为见效,并且不用考虑因为字符串过长而导致动态sql语句过长。

代码如下:

DECLARE @str varchar(1000)
DECLARE @idoc int;
DECLARE @doc xml;

 
set @str='1¦3¦4¦25'
set @doc=cast('<Root>
<item><ID>'+replace(@str,'&brvbar;','
</ID></item><item><ID>')+'</ID></item></Root>' as xml) 
EXEC sp_xml_preparedocument @Idoc OUTPUT, @doc

SELECT *   FROM OPENXML (@Idoc, '/Root/item',2)
WITH (
[ID] varchar(10)
) 
 /**//*
ID
-----------
1
3
4
25*/
本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3