使用TSQL生成XML的最佳方法

时间:2015-06-09 12:50:39

标签: sql-server xml tsql

以下代码在如此多的级别上是错误的,我无法将它们全部列出。 我想任何试图回答此问题的人都只会使用测试数据表。哈!

我拼凑了几个帖子,在EXPLICIT,RAW和PATH之间蹦蹦跳跳,它有点多了。

我认为使用SQL经常生成XML的人会从头脑中知道这一点。

我不喜欢Ex,Raw或Path - 我只需要最好的工具。在一天结束时,将有65个专栏组成最终文件的各个级别。

我需要代码来查询该表并生成它:

DECLARE @tvTest TABLE (contactID INT, FirstName VARCHAR(25), LastName VARCHAR(25), [type] VARCHAR(25), [address] VARCHAR(25))
INSERT INTO @tvTest (contactID, FirstName, LastName, [type], [address])
SELECT 123, 'Johnny', 'Testguy', 'email', 'example@sendwordnow.com'
UNION
SELECT 321, 'Sally', 'Testgirl', 'email', '1example@sendwordnow.com';

--Outer
SELECT
      A.contactID AS "@contactID"
    , 'AddOrModify' AS "@action"
FROM 
    @tvTest A
FOR XML PATH('contact'), ROOT('batchContactList')

--Level 1
DECLARE @xmldata XML;
SELECT @xmldata = (SELECT contactID, FirstName, LastName FROM @tvTest FOR XML PATH (''));

SELECT
      ColumnName AS "@name"
    , ColumnValue AS "text()"
FROM
    (SELECT 
          i.value('local-name(.)','varchar(100)') ColumnName
        , i.value('.','varchar(100)') ColumnValue
    FROM 
        @xmldata.nodes('//*[text()]') x(i)) tmp
FOR XML PATH ('contactField'), ROOT('contact')

SELECT @xmldata = (SELECT contactID, [type], [address] FROM @tvTest FOR XML PATH (''));

--Level 2, not complete
SELECT
      ColumnName AS "@name"
    , ColumnValue AS "text()"
FROM
    (SELECT 
          i.value('local-name(.)','varchar(100)') ColumnName
        , i.value('.','varchar(100)') ColumnValue
    FROM 
        @xmldata.nodes('//*[text()]') x(i)) tmp
FOR XML PATH ('contactPoint'), ROOT('contactPointList')

测试代码

- 测试数据

DirectoryInfo info = Directory.CreateDirectory(myPath);

1 个答案:

答案 0 :(得分:6)

我总是使用PATH。

这样的东西可以给你你想要的东西。

select T.contactID as [@contactID],
       'AddOrModify' as [@Action],
       'FirstName' as [contactField/@name],
       T.FirstName as [contactField],
       null,
       'LastName' as [contactField/@name],
       T.LastName as [contactField],
       (
       select 'Email' as [contactPoint/@type],
              'Label' as [contactPointField/@name],
              T.type as [contactPointField],
              null,
              'Address' as [contactPointField/@name],
              T.address as [contactPointField]
       for xml path('contactPointList'), type
       )
from @tvTest as T
for xml path('contact'), root('catchContactList')

结果:

<catchContactList>
  <contact contactID="123" Action="AddOrModify">
    <contactField name="FirstName">Johnny</contactField>
    <contactField name="LastName">Testguy</contactField>
    <contactPointList>
      <contactPoint type="Email" />
      <contactPointField name="Label">email</contactPointField>
      <contactPointField name="Address">example@sendwordnow.com</contactPointField>
    </contactPointList>
  </contact>
  <contact contactID="321" Action="AddOrModify">
    <contactField name="FirstName">Sally</contactField>
    <contactField name="LastName">Testgirl</contactField>
    <contactPointList>
      <contactPoint type="Email" />
      <contactPointField name="Label">email</contactPointField>
      <contactPointField name="Address">1example@sendwordnow.com</contactPointField>
    </contactPointList>
  </contact>
</catchContactList>