【发布时间】:2020-09-20 10:13:09
【问题描述】:
我正在寻找一种不需要手动编辑文件或复制粘贴内容的解决方案。我正在尝试使用这种方法来查询一个 xml 文档
DECLARE @myDoc xml
DECLARE @ProdID int
SET @myDoc = '<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features>
<Warranty>1 year parts and labor</Warranty>
<Maintenance>3 year parts and labor extended maintenance is available</Maintenance>
</Features>
</ProductDescription>
</Root>'
SET @ProdID = @myDoc.value('(/Root/ProductDescription/@ProductID)[1]', 'int' )
SELECT @ProdID
但是当我尝试使用这个示例数据时,当我想设置变量时它就不起作用了。我的 xml 文件还包含单引号,这就是我选择此示例的原因。
DECLARE @myDoc xml
DECLARE @ProdID int
SET @myDoc = '<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's 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 id="bk102">
<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>
</catalog>'
来自https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms762271(v=vs.85)的样本数据
【问题讨论】:
标签: sql-server xml tsql