34,590
社区成员
发帖
与我相关
我的任务
分享
DECLARE @inputStr xml
SET @inputStr = '<Request>
<tag>aaa</tag><tag_c>bbb</tag_c>
<tag>ccc</tag><tag_c>ddd</tag_c>
</Request>'
select x.node.value('local-name(.)','varchar(100)') as n ,x.node.value('.','varchar(100)') as v from @inputStr.nodes('Request/*') x(node)
n v
tag aaa
tag_c bbb
tag ccc
tag_c ddd
SELECT @inputStr
DECLARE @count int
SET @count=2;
SELECT @inputStr.query('/Request/tag[position()=sql:variable("@count")]')
SELECT @inputStr.value('count(//Request/tag)','int')
DECLARE @inputStr xml
SET @inputStr = '<Request>
<tag>aaa</tag><tag_c>bbb</tag_c>
<tag>ccc</tag><tag_c>ddd</tag_c>
</Request>'
Select T.C.value('.','varchar(40)')
from @inputStr.nodes('/Request/*') AS T(C)
Where T.C.value('local-name(.)','varchar(20)')='tag'
----------------------------------------
aaa
ccc
(2 row(s) affected)
DECLARE @inputStr xml
SET @inputStr = '<Request>
<tag>aaa</tag><tag_c>bbb</tag_c>
<tag>ccc</tag><tag_c>ddd</tag_c>
</Request>'
DECLARE @idoc int
EXEC sp_xml_preparedocument @Idoc OUTPUT, @inputStr
SELECT [TEXT] FROM OPENXML (@Idoc, '/Request/tag',1) WHERE [text] IS NOT NULL
TEXT
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
aaa
ccc
(2 行受影响)
SELECT @inputStr
DECLARE @count int
SET @count=2;
SELECT @inputStr.query('/Request/tag[position()=sql:variable("@count")]')
用下面的方法可以取的总共有多少个tag
SET @count=1;
SET @totalCount=@data.value('count(/bookstore/book[1]/@*)','INT');
WHILE @count<=@totalCount