2017-01-31 11 views
2

SQL ServerでFOR XMLの機能を使用していますが、結果のXMLの書式を変更したいとします。Microsoft SQL Server 2014生成されたXMLの書式変更

これは私のクエリです:

<rows> 
    <row> 
     <name>A Time to Kill</name> 
     <author>John Grisham</author> 
     <price>12.99</price> 
    </row> 
    <row> 
     <name>Blood and Smoke</name> 
     <author>Stephen King</author> 
     <price>10</price> 
    </row> 
</rows> 

が、私が望む結果は次のとおりです:

SELECT * 
FROM dbo.myTable AS row 
FOR XML RAW, ELEMENTS, ROOT('rows') 

結果が

<rows> 
    <row id="1"> 
     <cell>A Time to Kill</cell> 
     <cell>John Grisham</cell> 
     <cell>12.99</cell> 
    </row> 
    <row id="2"> 
     <cell>Blood and Smoke</cell> 
     <cell>Stephen King</cell> 
     <cell>10</cell> 
    </row> 
</rows> 

どのように私はこれを達成することができますか?私は "生"または "パス"の "auto"を変更しようとしましたが、動作しませんでした。

挨拶、ラファウ

私が知っている:-)

答えて

4
Declare @YourTable table (id int,name varchar(50),author varchar(50),price money) 
Insert Into @YourTable values 
(1,'A Time to Kill','John Grisham',12.99) 
,(2,'Blood and Smoke','Stephen King',10) 

Select [@id] = id 
     ,[cell] = name 
     ,null 
     ,[cell] = author 
     ,null 
     ,[cell] = price 
From @YourTable 
For XML Path('row'),root('rows') 

戻り

<rows> 
    <row id="1"> 
    <cell>A Time to Kill</cell> 
    <cell>John Grisham</cell> 
    <cell>12.9900</cell> 
    </row> 
    <row id="2"> 
    <cell>Blood and Smoke</cell> 
    <cell>Stephen King</cell> 
    <cell>10.0000</cell> 
    </row> 
</rows> 
+0

こんにちは、** SQL-Serverの黄金のバッジにおめでとう**、何の風船:-) – Shnugo

+0

@はありませんShnugoありがとう。心配しないで、私はサプライパーティーがそのような機会に騒音メーカーを帽子に抱きつけている...あなたがそれについて考えるとき少し悲しい。 –

+0

おはよう、それは素晴らしい作品! :-) – Rafalsonn

関連する問題