从SQL表创建XML - 如何从XML结构中删除不需要的标记?

时间:2016-12-23 11:51:33

标签: sql sql-server sql-server-2012 sql-server-2008-r2

我有一个包含两列的表tmpZstockZART_Article Code(itemcode)和GERNR_SerialNO(serialnumber):

enter image description here

CREATE TABLE tmpZstock (
    [ZART_Article Code] INT,
    GERNR_SerialNO INT
)

INSERT INTO tmpZstock ([ZART_Article Code], GERNR_SerialNO)
VALUES  (200078, 126),
        (200078, 127),
        (200078, 128),
        (200078, 129),
        (200078, 130),
        (200079, 131),
        (200079, 132),
        (200079, 133),
        (200079, 134),
        (200079, 135),
        (200079, 136),
        (200079, 137),
        (200079, 138),
        (200079, 139),
        (200079, 140),
        (200079, 141),
        (200080, 142),
        (200080, 143),
        (200080, 144),
        (200080, 145)

我需要像这样创建xml:

DesiredXML

但是当我写这个查询时:

Select [ZART_Article Code] as ITEMCODE ,
(
Select SERIALS.[GERNR_SerialNO]  as SERIALNO From tmpZstock SERIALS  
where SERIALS.[ZART_Article Code]=T1.[ZART_Article Code] FOR   XML rAW ('SERIALS'), TYPE )
From (select [ZART_Article Code] from tmpZstock group by [ZART_Article Code]) t1  FOR   XML PATH('SERIALNUMBERDETAILS'), TYPE
,Root ('SUMMARY')

结果是:

QueryResult

我不需要<SERIALS>标记 - 如何从结果中删除此标记?

1 个答案:

答案 0 :(得分:4)

您可以使用xml路径代替。这样的事情应该做到。

select T1.[ZART_Article Code] as ITEMCODE,
       (
       select T2.GERNR_SerialNO as SERIALNO
       from tmpZstock as T2
       where T1.[ZART_Article Code] = T2.[ZART_Article Code]
       for xml path(''), type
       )
from tmpZstock as T1
group by T1.[ZART_Article Code]
for xml path('SERIALNUMBERDETAILS'), root('SUMMARY')

使用表变量工作样本。

declare @T table
(
  [ZART_Article Code] int,
  GERNR_SerialNO  int
);

insert into @T values
(200078, 126),
(200078, 127),
(200078, 128),
(200078, 129),
(200078, 130),
(200079, 131),
(200079, 132),
(200079, 133),
(200079, 134),
(200079, 135),
(200079, 136),
(200079, 137),
(200079, 138),
(200079, 139),
(200079, 140),
(200079, 141),
(200080, 142),
(200080, 143),
(200080, 144),
(200080, 145);

select T1.[ZART_Article Code] as ITEMCODE,
       (
       select T2.GERNR_SerialNO as SERIALNO
       from @T as T2
       where T1.[ZART_Article Code] = T2.[ZART_Article Code]
       for xml path(''), type
       )
from @T as T1
group by T1.[ZART_Article Code]
for xml path('SERIALNUMBERDETAILS'), root('SUMMARY')

结果:

<SUMMARY>
  <SERIALNUMBERDETAILS>
    <ITEMCODE>200078</ITEMCODE>
    <SERIALNO>126</SERIALNO>
    <SERIALNO>127</SERIALNO>
    <SERIALNO>128</SERIALNO>
    <SERIALNO>129</SERIALNO>
    <SERIALNO>130</SERIALNO>
  </SERIALNUMBERDETAILS>
  <SERIALNUMBERDETAILS>
    <ITEMCODE>200079</ITEMCODE>
    <SERIALNO>131</SERIALNO>
    <SERIALNO>132</SERIALNO>
    <SERIALNO>133</SERIALNO>
    <SERIALNO>134</SERIALNO>
    <SERIALNO>135</SERIALNO>
    <SERIALNO>136</SERIALNO>
    <SERIALNO>137</SERIALNO>
    <SERIALNO>138</SERIALNO>
    <SERIALNO>139</SERIALNO>
    <SERIALNO>140</SERIALNO>
    <SERIALNO>141</SERIALNO>
  </SERIALNUMBERDETAILS>
  <SERIALNUMBERDETAILS>
    <ITEMCODE>200080</ITEMCODE>
    <SERIALNO>142</SERIALNO>
    <SERIALNO>143</SERIALNO>
    <SERIALNO>144</SERIALNO>
    <SERIALNO>145</SERIALNO>
  </SERIALNUMBERDETAILS>
</SUMMARY>