[英]Select from list of xml elements with SQL Server
假设我有这个xml-
<book>
<author>Gambardella, Matthew</author>
<title>XML Developers Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book>
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book>
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
我需要一个T-SQL查询来一次从该列表中选择一本书,因为在将它插入表之前,我需要对其进行某种处理。
假设我要在此列表中选择第二个元素,并将其显示为表格。
我怎样才能做到这一点?
就个人而言,我会首先将整个XML文档切成表格,如果我真的不能对它们进行基于集合的操作,请使用行号和循环或游标之类的方法遍历该表。
如果您只想显式地访问xml文档的第n个成员,则可以在编写xpath表达式时进行访问。 我不确定您如何知道需要迭代多少个元素。 这是获取第二本书节点的示例:
--Assumes you've assigned the XML document you provided to an XML variable called @xml
select t.c.value('author[1]', 'varchar(50)')
from @xml.nodes('/book[2]') as t(c)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.