TSQL FOR XML EXPLICIT - 向Element添加属性

时间:2012-12-20 14:15:09

标签: sql xml tsql

我使用FOR XML EXPLICIT这样的SQL查询:

declare @address table
(
    AddressID int,
    AddressType varchar(12),
    Address1 varchar(20),
    Address2 varchar(20),
    City varchar(25)
)    
insert into @address
select 1, 'Home', 'abc', 'xyz road', 'RJ' union all
select 2, 'Office', 'temp', 'ppp road', 'RJ' union all
select 3, 'Temp', 'eee', 'olkiu road', 'CL'

SELECT
    1 AS Tag,NULL AS Parent,
    0 AS 'AddressCollection!1!Sort!hide',
    NULL AS 'AddressCollection!1!',
    NULL AS 'Address!2!AddressID',
    NULL AS 'Address!2!AddressType!Element',
    NULL AS 'Address!2!Address1!Element',
    NULL AS 'Address!2!Address2!Element',
    NULL AS 'Address!2!City!Element'
UNION ALL
SELECT
    2 AS Tag,1 AS Parent,AddressID * 100,NULL,
    AddressID,AddressType, Address1, Address2, City
    FROM @address
ORDER BY [AddressCollection!1!Sort!hide]
FOR XML EXPLICIT

XML结果是:

<AddressCollection>
  <Address AddressID="1">
    <AddressType>Home</AddressType>
    <Address1>abc</Address1>
    <Address2>xyz road</Address2>
    <City>RJ</City>
  </Address>
  <Address AddressID="2">
    <AddressType>Office</AddressType>
    <Address1>temp</Address1>
    <Address2>ppp road</Address2>
    <City>RJ</City>
  </Address>
  <Address AddressID="3">
    <AddressType>Temp</AddressType>
    <Address1>eee</Address1>
    <Address2>olkiu road</Address2>
    <City>CL</City>
  </Address>
</AddressCollection>

如何向Element添加属性?我已正确地将它们添加到父标签上,但我不知道如何将它们添加到元素标签中。

我需要这样的东西:

<AddressCollection>
  <Address AddressID="1">
    <AddressType>Home</AddressType>
    <Address1>abc</Address1>
    <Address2>xyz road</Address2>
    <City status="modified">RJ</City>
  </Address>
  <Address AddressID="2">
    <AddressType status="modified">Office</AddressType>
    <Address1>temp</Address1>
    <Address2>ppp road</Address2>
    <City>RJ</City>
  </Address>
  <Address AddressID="3">
    <AddressType>Temp</AddressType>
    <Address1 status="modified">eee</Address1>
    <Address2>olkiu road</Address2>
    <City>CL</City>
  </Address>
</AddressCollection>

2 个答案:

答案 0 :(得分:2)

简单地删除显式并改为使用xml path(),root()模型:

XML显示是一个彻头彻尾的痛苦恕我直言。我不会使用它,除非你有某种原因被迫。您可以通过学习XML Path或XML Auto来完成更多工作。

declare @address table
(
    AddressID int,
    AddressType varchar(12),
    Address1 varchar(20),
    Address2 varchar(20),
    City varchar(25)
)    
insert into @address
select 1, 'Home', 'abc', 'xyz road', 'RJ' union all
select 2, 'Office', 'temp', 'ppp road', 'RJ' union all
select 3, 'Temp', 'eee', 'olkiu road', 'CL'

select *
from @address
for xml path('Address'), root('AddressCollection')

-- for even more control and fun:

select
    AddressID as "MainInfo/@ID"
,   AddressType as "MainInfo/@Type"
,   Address1 + ' ' + Address2 as "Address/*"
,   City as "Address/*"
from @Address
for xml path('Address'), root('AddressCollection')

答案 1 :(得分:0)

校正最后一部分溶液:

select
AddressID as "MainInfo/@ID"
,   AddressType as "Address/@Type"
,   Address1 + ' ' + Address2 as "Address/*"
,   City as "City/*"
from @Address
for xml path('Address'), root('AddressCollection')