2015-04-29 80 views
0

在management studio中可以創建關係XML文件嗎?將查詢導出到關係xml

例如:

select a.product, a.cost, b.productType, c.brand 
from products a 
inner join productTypes b on a.productTypeID = b.productTypeID 
inner join brands c on b.brandID = c.brandID 

創建XML文件

<brands> 
    <brand>SomeBrand1 
     <productType>SomeProductType 
      <product>Product1</product> 
      <Cost>$10.00</cost> 
     </productType> 
    <brand>  
    <brand>SomeBrand2 
     <productType>SomeProductType 
      <product>Product3</product> 
      <Cost>$10.00</cost> 
      <product>Product4</product> 
      <Cost>$10.00</cost> 
     </productType> 
    <brand> 
</brands> 

謝謝, 喬

+0

您可以使用FOR XML子句從關係數據[msdn](https://msdn.microsoft.com/en-us/library/ms178107.aspx)創建xml。我建議閱讀FOR XML PATH部分。 – Blim

回答

1

您可以使用嵌套生成所需形狀的XML。此代碼創建我使用的表格。

--create products 
select 'product1' product, 10.00 cost, 1 productTypeID into #Products 
insert into #Products select 'product3', 10.00 cost, 2 productTypeID 
insert into #Products select 'product4', 10.00 cost, 2 productTypeID 

--create product types 
select 1 productTypeID, 'SomeProductType1' productType, 1 brandID into #productTypes 
insert into #productTypes select 2 productTypeID, 'SomeProductType2' productType, 2 brandID 

--brands 
select 1 brandID, 'SomeBrand1' brand into #brands 
insert into #brands select 2 brandID, 'SomeBrand2' brand 

我可能是錯的,但我做到了,你需要兩個不同的產品類型,所以我創建了兩個他們的假設。加入查詢的結構方式從品牌到產品類型具有一對多的關係。這意味着每個產品類型只屬於一個品牌。我想這兩個品牌類型的記錄可以有相同的名稱,這將導致您在問題中引用的XML。

下面的語句則應該在你的例子創建XML的形狀:

select ltrim(brand), 
    (select LTRIM(productType), 
     (select product, cost 
     from #Products 
     where #Products.productTypeID = #productTypes.productTypeID 
     order by product 
     for XML PATH(''), type 
     ) 
    from #productTypes 
    where #productTypes.brandID = #brands.brandID 
    order by productType 
    for XML path('productType'), type 
    ) 
from #brands 
order by brand 
for xml path('brand'), root('brands') 

輸出:

<brands> 
    <brand>SomeBrand1 
    <productType>SomeProductType1 
     <product>product1</product> 
     <cost>10.00</cost> 
    </productType> 
    </brand> 
    <brand>SomeBrand2 
    <productType>SomeProductType2 
     <product>product3</product> 
     <cost>10.00</cost> 
     <product>product4</product> 
     <cost>10.00</cost> 
    </productType> 
    </brand> 
</brands> 

最外面的XML路徑聲明「爲XML路徑( '品牌'),根(「品牌」)將整個XML封裝在名爲「品牌」的根中,然後爲返回的每行創建稱爲「品牌」的元素。選擇之後,我將品牌列包裝在一個沒有別名的ltrim函數中,以便它不返回列名,該列名將內聯插入文本,而不會在XML中爲其創建標籤。

所選的下一列是一個子查詢,它返回產品類型的XML片段。 「Type」關鍵字遵循此子查詢的XML路徑,以表明它將被解釋爲XML。

最內層的子查詢只返回屬於產品類型的所有記錄的產品和成本。通過指定「for XML PATH('')」表示不應爲每行創建一個「行」標記。

1

這不是絕對的結構相同,但結果是一樣的:

select a.product, a.cost, b.productType, c.brand 
from products a 
inner join productTypes b on a.productTypeID = b.productTypeID 
inner join brands c on b.brandID = c.brandID 
FOR XML PATH ('brand'), TYPE, ROOT ('brands') 

OUTPUT

<brands> 
    <brand> 
    <brand>SomeBrand1</brand> 
    <product>Product1</product> 
    <cost>10.00</cost> 
    <productType>SomeProductType</productType> 
    </brand> 
    <brand> 
    <brand>SomeBrand2</brand> 
    <product>Product3</product> 
    <cost>10.00</cost> 
    <productType>SomeProductType</productType> 
    </brand> 
    <brand> 
    <brand>SomeBrand2</brand> 
    <product>Product4</product> 
    <cost>10.00</cost> 
    <productType>SomeProductType</productType> 
    </brand> 
</brands> 
+0

是有道理的。似乎可定製。感謝您的迴應 – tjcinnamon