2017-03-03 33 views
2

如何爲SQL Server 2000(兼容級別= 80)和SQL Server 2008(兼容級別= 100)生成相同的xml輸出?在SQL Server 2000和2008中使用XML auto的不同XML結果

實施例:

create table #temp(a int, b varchar(10)) 

insert into #temp 
select 1, 'abc' 
union 
select 1,'def' 

select * 
from 
    (select * from #temp child) parent 
for xml auto, elements 

用於SQL Server 2000(兼容級別80)輸出:

<child> 
     <a>1</a> 
     <b>abc</b> 
</child> 
<child> 
     <a>1</a> 
     <b>def</b> 
</child> 

輸出的SQL Server 2008(兼容級別100):

<parent> 
      <a>1</a> 
      <b>abc</b> 
    </parent> 
    <parent> 
      <a>1</a> 
      <b>def</b> 
    </parent> 

由於

+0

題外話:如果真有可能去升級。 2000甚至不再支持(相當長一段時間)。優點和新功能 - 在大多數情況下 - 值得。 – Shnugo

回答

0

更換別名childparent應該做的伎倆:

create table #temp(a int, b varchar(10)) 

insert into #temp 
select 1, 'abc' 
union 
select 1,'def' 

select * 
from 
    (select * from #temp parent) parent 
for xml auto, elements 

我嘗試這個查詢有兼容級別80和100在這兩種情況下,結果是:

<parent> 
    <a>1</a> 
    <b>abc</b> 
</parent> 
<parent> 
    <a>1</a> 
    <b>def</b> 
</parent>