SQL Server问题 - 查询到XML

时间:2010-11-16 15:59:49

标签: sql xml

好的,我有这个查询:

Select Orders.OrderID, ProductID, UnitPrice, Quantity, Orders.OrderDate From [Order Details]
left join Orders on Orders.OrderID=[Order Details].OrderID
where Orders.OrderID='10248' or Orders.OrderID = '10249'
FOR XML Auto, Elements;

当我执行它时会提供以下XML:

<Orders>
  <OrderID>10248</OrderID>
  <OrderDate>1996-07-04T00:00:00</OrderDate>
  <Order_x0020_Details>
    <ProductID>11</ProductID>
    <UnitPrice>15.4000</UnitPrice>
    <Quantity>12</Quantity>
  </Order_x0020_Details>
  <Order_x0020_Details>
    <ProductID>42</ProductID>
    <UnitPrice>10.7800</UnitPrice>
    <Quantity>10</Quantity>
  </Order_x0020_Details>
  <Order_x0020_Details>
    <ProductID>72</ProductID>
    <UnitPrice>38.2800</UnitPrice>
    <Quantity>5</Quantity>
  </Order_x0020_Details>
</Orders>
<Orders>
  <OrderID>10249</OrderID>
  <OrderDate>1996-07-05T00:00:00</OrderDate>
  <Order_x0020_Details>
    <ProductID>14</ProductID>
    <UnitPrice>20.4600</UnitPrice>
    <Quantity>9</Quantity>
  </Order_x0020_Details>
  <Order_x0020_Details>
    <ProductID>51</ProductID>
    <UnitPrice>46.6400</UnitPrice>
    <Quantity>40</Quantity>
  </Order_x0020_Details>
</Orders>

除了我希望" <Order_x0020_Details> "只能读作" <Order Details> "但我无法弄清楚如何执行此操作之外,我没关系。有什么建议?感谢

2 个答案:

答案 0 :(得分:5)

它正在放置x0020,因为订单明细表名称中有一个空格。

修改您的查询以使用该表的别名,它应该修复它(注意我添加的OrderDetails):

Select Orders.OrderID, ProductID, UnitPrice, Quantity, Orders.OrderDate From [Order Details] OrderDetails
left join Orders on Orders.OrderID=OrderDetails.OrderID
where Orders.OrderID='10248' or Orders.OrderID = '10249'
FOR XML Auto, Elements;

答案 1 :(得分:2)

您应该能够简单地为查询中的[Order Details]表添加别名。

Select Orders.OrderID, ProductID, UnitPrice, Quantity, Orders.OrderDate 
    From [Order Details] OrderDetails
        left join Orders 
            on Orders.OrderID=OrderDetails.OrderID
    where Orders.OrderID = '10248' 
       or Orders.OrderID = '10249'
    FOR XML Auto, Elements;