无法管理从数据库到XML的多行导出

时间:2017-05-19 15:45:27

标签: sql sql-server xml

我有一个有3行的表,1用作行标识符,第2用于与其他表建立关系,第3用于保存内容。我在做一个查询,其中给定的结果是我需要的,但有一点点错误。

表:print

这是我正在使用的查询:

SELECT Plate, tbl_veiculos.ID, Section, Category, Brand, Model, Version, Fuel, Price, B2BPrice, Year, Month, TollClass, Origin, Color, SeatColour, Seats, Kms, Doors, HP, Owners, CC, Obs, TaxDeductible, WarrantyMonth,
(SELECT Name, Street, Locality, Email from tbl_AdStand where tbl_AdStand.ID = tbl_veiculos.ID AND tbl_veiculos.ID = 1 FOR XML PATH('AdStand'), type ), 
(SELECT DISTINCT Site from tbl_ExportSites where tbl_ExportSites.ID = tbl_veiculos.ID AND tbl_veiculos.ID = 1 FOR XML PATH('ExportSites'), type )
FROM tbl_veiculos, tbl_veiculo_spec, tbl_AdStand
WHERE tbl_veiculos.ID = tbl_veiculo_spec.ID AND tbl_AdStand.ID = tbl_veiculos.ID AND tbl_veiculos.ID = 1
FOR XML PATH ('Vehicle'), TYPE, ROOT('VehicleList')

这是我得到的输出:

<VehicleList>
  <Vehicle>
    <Plate>34-23-nd</Plate>
    <ID>1</ID>
    <Section>carros</Section>
    <Category>cabrio</Category>
    <Brand>Mercedes-Benz</Brand>
    <Model>A140</Model>
    <Version>1.4 twin-turbo</Version>
    <Fuel>Gasolina</Fuel>
    <Price>18.000</Price>
    <B2BPrice>0</B2BPrice>
    <Year>2015</Year>
    <Month>03</Month>
    <TollClass>2</TollClass>
    <Origin>Alemanha</Origin>
    <Color>Vermelho</Color>
    <SeatColour>Cinza</SeatColour>
    <Seats>5</Seats>
    <Kms>13000</Kms>
    <Doors>5</Doors>
    <HP>310</HP>
    <Owners>0</Owners>
    <CC>1.389</CC>
    <Obs>Tinta raspada no para-choques dianteiro</Obs>
    <WarrantyMonth>0</WarrantyMonth>
    <AdStand>
      <Name>Stand Veloso</Name>
      <Street>Rua dos Biscainhos</Street>
      <Locality>2450-341</Locality>
      <Email>standbisca@gmail.com</Email>
    </AdStand>
    <ExportSites>
      <Site>Coisas</Site>
    </ExportSites>
    <ExportSites>
      <Site>Custojusto</Site>
    </ExportSites>
    <ExportSites>
      <Site>StandVirtual</Site>
    </ExportSites>
  </Vehicle>
</VehicleList>

“错误”在最后一行

        <ExportSites>
              <Site>Coisas</Site>
            </ExportSites>
            <ExportSites>
              <Site>Custojusto</Site>
            </ExportSites>
            <ExportSites>
              <Site>StandVirtual</Site>
            </ExportSites>

输出显示“分离”,而我需要绝对相反,我需要它看起来像这样[硬编码]:

            <ExportSites>
              <Site>Coisas</Site>
              <Site>Custojusto</Site>
              <Site>StandVirtual</Site>
            </ExportSites>

我需要在查询中更改哪些内容才能获得此信息。此外,我可以稍后使用PHP将此查询导出到XML文件吗?

感谢您的时间。

2 个答案:

答案 0 :(得分:2)

试试这个:

DECLARE @mockup TABLE([Site] VARCHAR(100))
INSERT INTO @mockup VALUES ('Site 1'),('Site 2');

SELECT [Site] FROM @mockup FOR XML PATH('ExportSites'),TYPE;
SELECT [Site] FROM @mockup FOR XML PATH(''),ROOT('ExportSites'),TYPE;

结果

<ExportSites>
  <Site>Site 1</Site>
</ExportSites>
<ExportSites>
  <Site>Site 2</Site>
</ExportSites>

<ExportSites>
  <Site>Site 1</Site>
  <Site>Site 2</Site>
</ExportSites>

你看到了区别吗?

PATH()将指定行的名称,而ROOT将定义其他封闭元素。由于您的行名称为空,因此列名称仅 ...

只是出于好奇:您可能会尝试在上面的模型 -table中再添加一列,并比较差异......

因此,请将您的子查询更改为:

(SELECT DISTINCT Site 
 from tbl_ExportSites 
 where tbl_ExportSites.ID = tbl_veiculos.ID AND tbl_veiculos.ID = 1 
 FOR XML PATH(''),ROOT('ExportSites'), type )

答案 1 :(得分:0)

我认为你需要提供如下的根:

SELECT Plate, tbl_veiculos.ID, Section, Category, Brand, Model, Version, Fuel, Price, B2BPrice, Year, Month, TollClass, Origin, Color, SeatColour, Seats, Kms, Doors, HP, Owners, CC, Obs, TaxDeductible, WarrantyMonth,
(SELECT Name, Street, Locality, Email from tbl_AdStand where tbl_AdStand.ID = tbl_veiculos.ID AND tbl_veiculos.ID = 1 FOR XML PATH('AdStand'), type ), 
(SELECT DISTINCT Site from tbl_ExportSites where tbl_ExportSites.ID = tbl_veiculos.ID AND tbl_veiculos.ID = 1 FOR XML PATH('Site'), type, root('ExportSites') )
FROM tbl_veiculos, tbl_veiculo_spec, tbl_AdStand
WHERE tbl_veiculos.ID = tbl_veiculo_spec.ID AND tbl_AdStand.ID = tbl_veiculos.ID AND tbl_veiculos.ID = 1
FOR XML PATH ('Vehicle'), TYPE, ROOT('VehicleList')