【问题标题】:SQL Server FOR XML adding in a tagSQL Server FOR XML 添加标签
【发布时间】:2010-11-19 17:59:27
【问题描述】:

所以我从 SQL Server 2000 数据库中提取数据,然后使用 FOR XML AUTO 将其转换为 XML。

我得到的 XML 是这样的。

<Order OrderNumber="2000004" DeliveryPickupCharge="5.9900" SalesTaxTotal="0.0000"      SubTotal="0.0000" Total="5.9900">
  <Customer FirstName="Anthony" LastName="Caporale">
  <Product ProductName="Paper Towels">
  <OrderItem ItemQuantity="1" ItemPrice="8.5900" Total="8.5900" />
</Product>
<Product ProductName="Bathroom Tissue - Giant Roll">
  <OrderItem ItemQuantity="2" ItemPrice="7.1500" Total="14.3000" />
</Product>
<Product ProductName="Napkins - 1-Ply">
  <OrderItem ItemQuantity="1" ItemPrice="3.4900" Total="3.4900" />
</Product>
<Product ProductName="Facial Tissues - 2-Ply - White">
  <OrderItem ItemQuantity="2" ItemPrice="2.0500" Total="4.1000" />
</Product>
<Product ProductName="Anti-Perspirant &amp; Deodorant - Ultra DryCool Rush">
  <OrderItem ItemQuantity="2" ItemPrice="3.5900" Total="7.1800" />
</Product>
<Product ProductName="Flushable Wipes - Fresh Mates Cloths - Refill">
  <OrderItem ItemQuantity="2" ItemPrice="2.9500" Total="5.9000" />
</Product>

首先,2000年没有Root('whatever'),那么有没有办法在T-SQL中添加根节点呢?另外,有没有办法将所有产品包装在产品标签中?我不太明白为什么返回的 XML 格式如此糟糕。如果我使用的是 SQL 2005,我可以稍微修复一下。不幸的是,我现在卡在 2000 上。

谢谢

【问题讨论】:

    标签: sql-server xml sql-server-2000 for-xml


    【解决方案1】:

    您可以使用FOR XML EXPLICIT 模式按照您的意愿操作XML。我认为它比 AUTO(或 2k5 PATH)复杂得多,但一旦你开始使用它就可以正常工作。

    【讨论】:

    【解决方案2】:

    我知道在 2005 年使用嵌套选择允许您将多个结果嵌入到一个核心标签(产品中的多个产品)中,同时还为 XML AUTO 声明了 ensted seelect。

    您还可以使用这种技术,通过使用一个大的嵌套选择将所有内容包装在根标记中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 2020-02-13
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 2015-07-01
      相关资源
      最近更新 更多