2011-08-31 54 views
0

您好我正在嘗試使用sql創建一個xml。我的代碼如下:使用SQL創建Xml

SELECT 
    --so.SalesOrderID, 
    --so.name as 'OrderName', 
    so.zon_cartid as "zcom_order_id", 
    c.zon_emailaddress1 As "email", 
    so.OrderNumber, 
    so.createdon As "created", 
    so.totallineitemamount As "Sub_total", 
    so.totalamount as "Order_total", 
    so.totaltax As "oder_tax", 
    so.[description] As "Order_note", 
    so.zon_ordersubsource as "order_source", 
    so.submitstatus As "order_status", 
    so.discountamount As "order_discount", 
    --c.FirstName, 
    --c.LastName, 
    --a.name as 'AccountName', 
    (SELECT --sod.SalesOrderDetailID, 
     p.zon_isbn10, 
     sod.Zon_producttype, 
     --p.Zon_title, 
     --sod.Zon_status, 
     SOD.Quantity As "Quantity", 
     SOD.PricePerUnit As "Price", 
     SOD.Tax As "Tax", 
     SOD.ManualDiscountAmount As "Product_discount" 
     --sod.extendedamount 
    FROM SalesOrderDetail sod 
    inner join Product p on sod.productid = p.productid 
    WHERE so.SalesOrderId = sod.SalesOrderId 
    ORDER by sod.SalesOrderDetailID 
    FOR XML PATH('Product'),Type) As "Products", 
    (Select so3.ShippingMethodCode, 
        so3.freightamount, 
    (Select c.firstname, 
        c.lastname, 
       so2.Shipto_Line1, 
       so2.Shipto_Line2, 
       so2.Shipto_Line3, 
       so2.Shipto_City As "city", 
       so2.Shipto_StateorProvince As "state", 
       so2.Shipto_PostalCode As "postal", 
       so2.shipto_country As "country", 
       so2.Shipto_telephone As "phone", 
       so2.Shipto_fax As "fax" 
       From SalesOrder so2 
       WHERE so2.SalesOrderId = so.SalesOrderId 
      FOR XML PATH(''),Type) As "ShippingAddress" 
       From SalesOrder so3 
       WHERE so3.SalesOrderId = so.SalesOrderId 
       FOR XML PATH(''),Type), 
    (Select  c.firstname, 
       c.lastname, 
       so4.billto_Line1, 
       so4.billto_Line2, 
       so4.billto_Line3, 
       so4.billto_City As "city", 
       so4.billto_StateorProvince As "state", 
       so4.billto_PostalCode As "postal", 
       so4.billto_country As "country", 
       so4.billto_telephone As "phone", 
       so4.billto_fax As "fax" 
       From SalesOrder so4 
       WHERE so4.SalesOrderId = so.SalesOrderId 
      FOR XML PATH(''),Type) As "MailingAddress" 
FROM SalesOrder so 
left outer join Contact c on c.contactid = so.customerid 
left outer join Account a on a.accountid = so.customerid 
WHERE so.CreatedOn > '2011-08-08' 
FOR XML PATH('Order'),ROOT('message') 

我的輸出看起來liek這樣的:

<message> 
    <Order> 
    <OrderNumber>ORD-01003-H8M0J</OrderNumber> 
    <created>2011-08-16T13:52:48</created> 
    <Sub_total>233.9400</Sub_total> 
    <Order_total>228.4400</Order_total> 
    <oder_tax>2.5000</oder_tax> 
    <order_source>The Story</order_source> 
    <order_discount>8.0000</order_discount> 
    <Products> 
     <Product> 
     <zon_isbn10>0310591635</zon_isbn10> 
     <Quantity>10.0000000000</Quantity> 
     <Price>4.0000</Price> 
     </Product> 
     <Product> 
     <zon_isbn10>0310649781</zon_isbn10> 
     <Quantity>3.0000000000</Quantity> 
     <Price>43.9800</Price> 
     <Tax>1.4000</Tax> 
     <Product_discount>10.0000</Product_discount> 
     </Product> 
     <Product> 
     <zon_isbn10>031023414X</zon_isbn10> 
     <Quantity>4.0000000000</Quantity> 
     <Price>20.0000</Price> 
     <Tax>1.1000</Tax> 
     <Product_discount>8.0000</Product_discount> 
     </Product> 
    </Products> 
    <ShippingAddress> 
     <Shipto_Line1>1211 E BADILLO ST</Shipto_Line1> 
     <Shipto_Line3>WEST COVINA CA 91790</Shipto_Line3> 
     <postal>91790</postal> 
    </ShippingAddress> 
    <MailingAddress></MailingAddress> 
    </Order> 

,但我需要的輸出是它應該顯示的順序基本細節,例如訂單標題如下所示,我嘗試了所有不同如果有人可以幫助,那麼我會錯過一些東西,那會很棒。

<message> 
    <Order> 
<orderheader> 
    <OrderNumber>ORD-01003-H8M0J</OrderNumber> 
    <created>2011-08-16T13:52:48</created> 
    <Sub_total>233.9400</Sub_total> 
    <Order_total>228.4400</Order_total> 
    <oder_tax>2.5000</oder_tax> 
    <order_source>The Story</order_source> 
    <order_discount>8.0000</order_discount> 
</orderheader> 
    <Products> 
     <Product> 
     <zon_isbn10>0310591635</zon_isbn10> 
     <Quantity>10.0000000000</Quantity> 
     <Price>4.0000</Price> 
     </Product> 
     <Product> 
     <zon_isbn10>0310649781</zon_isbn10> 
     <Quantity>3.0000000000</Quantity> 
     <Price>43.9800</Price> 
     <Tax>1.4000</Tax> 
     <Product_discount>10.0000</Product_discount> 
     </Product> 
     <Product> 
     <zon_isbn10>031023414X</zon_isbn10> 
     <Quantity>4.0000000000</Quantity> 
     <Price>20.0000</Price> 
     <Tax>1.1000</Tax> 
     <Product_discount>8.0000</Product_discount> 
     </Product> 
    </Products> 
    <ShippingAddress> 
     <Shipto_Line1>1211 E BADILLO ST</Shipto_Line1> 
     <Shipto_Line3>WEST COVINA CA 91790</Shipto_Line3> 
     <postal>91790</postal> 
    </ShippingAddress> 
    <MailingAddress></MailingAddress> 
    </Order> 

Thanks in advance 

回答

2

您可以在orderheader中將需要的項目的別名寫爲'orderheader/created'等,以獲得所需的輸出。

SELECT 
so.OrderNumber as 'orderheader/OrderNumber', 
so.createdon As 'orderheader/created', 
so.totallineitemamount As 'orderheader/Sub_total', 
so.totalamount as 'orderheader/Order_total', 
... 

將產生的輸出作爲

<orderheader> 
    <OrderNumber>something</OrderNumber> 
    <created>something</created> 
    <Sub_total>something</Sub_total> 
    <Order_total>something</Order_total> 
</orderheader> 
+0

我需要使用此XML作爲SSIS包源所以XML源將所有標記爲一個輸出,以便減少無輸出我想把這些細節放在訂單標題下,這樣我就不需要再單獨將它們分開,並且所有訂單都在訂單標題下一次出現 – Sharat

+0

謝謝查詢正在工作。非常感謝 – Sharat