0

给定两个表:

T1( EntityID int , EmailAddress varchar(55),MaxNumberOfConnections int )

T2( EntityID int , EntityAttributes XML )

如何使用单个语句将所有数据插入到其中,从而将 (all but) 中的所有列T1转换为以下中的一个 XML 列:T2T1EntityIDT2

T1( 1,'1234@1234.com',454)

T2(1, '<Attributes>
          <Attribute EmailAddress="1234@1234.com">
          <Attribute MaxNumberOfConnections ="454">
      </Attributes>' )
4

1 回答 1

1

以下是基于我的评论的两个解决方案 - 具有多个属性的单个“属性”元素:

SELECT
    EntityId,
    (
        SELECT
            EmailAddress AS [Attribute/@EmailAddress],
            MaxNumberOfConnections AS [Attribute/@MaxNumberOfConnections]
        FROM
            T1 i
        WHERE
            i.EntityId = o.EntityId
        FOR XML PATH('Attributes')
    ) AS EntityAttributes 
FROM
    T1 o

每个字段的单个元素:

SELECT
    EntityId,
    (
        SELECT
            EmailAddress,
            MaxNumberOfConnections
        FROM
            T1 i
        WHERE
            i.EntityId = o.EntityId
        FOR XML PATH('Attributes')
    ) AS EntityAttributes 
FROM
    T1 o
于 2012-10-04T19:50:00.210 回答