将SQL表转换为XML

时间:2015-02-13 17:58:06

标签: sql-server xml

我一直在努力将SQL表元素转换为3层XML表。我几乎在那里,但我知道我只是在某处丢失了一小块。

以下是我要找的内容:

<labels _FORMAT="\US Labels\ChemSamples.lwl" _QUANTITY="1" _PRINTERNUMBER="10">
  <label>
    <variable name="HBANumber">1234</variable>
    <variable name="BatchNumber">567</variable>
    <variable name="BagNumber">1</variable>
    <variable name="CombinedBarcodeTop">123601</variable>
    <variable name="CombinedBarcodeText">123601</variable>
    <variable name="HBANumberSmall">1234</variable>
    <variable name="CombinedSmall">123601</variable>
    <variable name="CombinedBarcodeBottom">123601</variable>
    <variable name="CreatedDate">02/10/2015</variable>
    <variable name="Operator">7197</variable>
    <variable name="PrintDate">02/10/2015</variable>
  </label>
</labels>

我创建了一个临时表来生成数据,我的SQL查询如下所示:

declare @tmpTable table(fieldname varchar(25), fieldvalue varchar(20))

insert into @tmpTable values('HBANumber','1234')
insert into @tmpTable values('BatchNumber','567')
insert into @tmpTable values('BagNumber','1')
insert into @tmpTable values('CombinedBarcodeTop','123601')
insert into @tmpTable values('CombinedBarcodeText','123601')
insert into @tmpTable values('HBANumberSmall','1234')
insert into @tmpTable values('CombinedSmall','123601')
insert into @tmpTable values('CombinedBarcodeBottom','123601')
insert into @tmpTable values('CreatedDate','02/10/2015')
insert into @tmpTable values('Operator','7197')
insert into @tmpTable values('PrintDate','02/10/2015')
SELECT
    '\US Labels\ChemSamples.lwl' AS '@_FORMAT',
    1 AS '@_QUANTITY',
    10 AS '@_PRINTERNUMBER',
    (select null,
        (select fieldname AS '@name', fieldvalue as 'variable'
            from @tmpTable
            for XML path ('variable'), type)
    for XML PATH ('label'), TYPE)
FOR XML PATH ('labels')

执行此查询将返回以下结果:

<labels _FORMAT="\US Labels\ChemSamples.lwl" _QUANTITY="1" _PRINTERNUMBER="10">
  <label>
    <variable name="HBANumber">
      <variable>1234</variable>
    </variable>
    <variable name="BatchNumber">
      <variable>567</variable>
    </variable>
    <variable name="BagNumber">
      <variable>1</variable>
    </variable>
    <variable name="CombinedBarcodeTop">
      <variable>123601</variable>
    </variable>
    <variable name="CombinedBarcodeText">
      <variable>123601</variable>
    </variable>
    <variable name="HBANumberSmall">
      <variable>1234</variable>
    </variable>
    <variable name="CombinedSmall">
      <variable>123601</variable>
    </variable>
    <variable name="CombinedBarcodeBottom">
      <variable>123601</variable>
    </variable>
    <variable name="CreatedDate">
      <variable>02/10/2015</variable>
    </variable>
    <variable name="Operator">
      <variable>7197</variable>
    </variable>
    <variable name="PrintDate">
      <variable>02/10/2015</variable>
    </variable>
  </label>
</labels>

有没有人知道我能做什么才能让它看起来像我列出的第一个项目?

由于

2 个答案:

答案 0 :(得分:0)

我认为这应该适合你:

SELECT 
    '\US Labels\ChemSamples.lwl' AS '@_FORMAT',
    1 AS '@_QUANTITY',
    10 AS '@_PRINTERNUMBER',
    (select null,
        (SELECT 1 AS Tag, 
                NULL AS Parent, 
                fieldname AS [variable!1!name], 
                fieldvalue AS [variable!1!!ELEMENT]
          FROM @tmpTable
        FOR XML EXPLICIT, TYPE)
     FOR XML PATH ('label'), TYPE)
FOR XML PATH ('labels')

关键是使用FOR XML EXPLICIT子句,允许您使用更多控制来格式化XML,并且将属性和值混合到同一节点中的秘诀是省略第二个子句中的节点名称(两个“!!”彼此相邻)。

当我运行它时,这会给我带来什么:

<labels _FORMAT="\US Labels\ChemSamples.lwl" _QUANTITY="1" _PRINTERNUMBER="10">
  <label>
    <variable name="HBANumber">1234</variable>
    <variable name="BatchNumber">567</variable>
    <variable name="BagNumber">1</variable>
    <variable name="CombinedBarcodeTop">123601</variable>
    <variable name="CombinedBarcodeText">123601</variable>
    <variable name="HBANumberSmall">1234</variable>
    <variable name="CombinedSmall">123601</variable>
    <variable name="CombinedBarcodeBottom">123601</variable>
    <variable name="CreatedDate">02/10/2015</variable>
    <variable name="Operator">7197</variable>
    <variable name="PrintDate">02/10/2015</variable>
  </label>
</labels>

我希望这有帮助!

答案 1 :(得分:0)

@pmbAustin,实际上不需要XML EXPLICIT,这将获得请求的结果。要获得结果,请将, [variable] = fieldvalue替换为, [text()] = fieldvalue

SELECT 
      [@_FORMAT]          = '\US Labels\ChemSamples.lwl'
    , [@_QUANTITY]        = 1
    , [@_PRINTERNUMBER]   = 10
    , [label]             = (   SELECT 
                                      [@name]       = fieldname 
                                    , [variable]    = fieldvalue
                                FROM @tmpTable
                                FOR XML PATH ('variable'), TYPE
                            )
FOR XML PATH ('labels')