2014-03-07 83 views
0

您好我需要已經字段名作爲actionname SQL表數據轉換,actiondesc我需要轉換,在下面的格式SQL錶轉換爲XML名稱值對

<Details> 
    <Field name="actiondesc" value="val1"/> 
    <Field name="actionname" value="val2"/> 
    </Details> 

在此先感謝 阿倫

回答

2

SQL Fiddle

MS SQL Server 2008的架構設置

create table dbo.YourTable 
(
    actiondesc int, 
    actionname int 
); 

insert into dbo.YourTable values(1,2); 
insert into dbo.YourTable values(3,4); 

查詢1

select 'actiondesc' as [Field/@name], 
     T.actiondesc as [Field/@value], 
     '', 
     'actionname' as [Field/@name], 
     T.actionname as [Field/@value] 
from dbo.YourTable as T 
for xml path('Details') 

Results

<Details> 
    <Field name="actiondesc" value="1" /> 
    <Field name="actionname" value="2" /> 
</Details> 
<Details> 
    <Field name="actiondesc" value="3" /> 
    <Field name="actionname" value="4" /> 
</Details>