FOR XML PATH有3个表

时间:2018-03-29 02:44:58

标签: sql sql-server xml

我有3张桌子:

CREATE TABLE TEST_PRODUCT 
(  
    PRODUCT_ID int,   
    PRODUCT_NAME varchar(255)
);

CREATE  TABLE TEST_PROD_VALUES 
(
    ID int, 
    PRODUCT_ID  int,
    PROD_VALUES_VALUE varchar(255)
);

CREATE TABLE TEST_VENDOR 
(
    ID int,  
    PRODUCT_ID  int,
    VENDOR_NAME varchar(255)
);

INSERT INTO TEST_PRODUCT (PRODUCT_ID, PRODUCT_NAME) 
VALUES  (1,'PRODUCT 1'), (2,'PRODUCT 2')

INSERT INTO TEST_PROD_VALUES (ID,PRODUCT_ID,PROD_VALUES_VALUE) 
VALUES  (1,1,'PROD_VALUES 1'), (2,2,'PROD_VALUES 2')

INSERT INTO TEST_VENDOR (ID,PRODUCT_ID, VENDOR_NAME) 
VALUES  (1,1,'VENDOR 1'), (2,2,'VENDOR 2')

现在,我想在SQL Server中使用查询FOR XML PATH导出XML。

我该怎么做? - 谢谢。

Image 1

Image 2

2 个答案:

答案 0 :(得分:2)

您可以使用以下脚本:

DEMO

DECLARE @CMD nvarchar(max) = ''

DECLARE @AllTablesXML table (XMLData XML)
SELECT @CMD += 
';SELECT 
    (
        SELECT * 
        FROM ' + QUOTENAME(T.TABLE_SCHEMA) + '.' + QUOTENAME(T.TABLE_NAME) + CHAR(10) +
        'FOR XML PATH ('''+TABLE_NAME+''') ,type ' + CHAR(10) +'
    )
 FOR XML PATH (''ROOT''),  type'
FROM INFORMATION_SCHEMA.TABLES T
WHERE TABLE_NAME in ('TEST_PROD_VALUES', 'TEST_PRODUCT', 'TEST_VENDOR')

INSERT INTO @AllTablesXML
EXEC sp_executesql @CMD

/*Xml Structure of the table data (Just to display the xml format)*/
 SELECT XMLData AS [FILE]
 FROM @AllTablesXML
 FOR XML PATH (''), ROOT('LIST') , TYPE

答案 1 :(得分:2)

您可以使用相关的子查询解决此问题......

我必须承认,我不认为,这种设计会让你开心。并且 - 仅举几例:您的XML格式不正确。它缺少一般的<root>节点。这适用于SQL-Server,但可能会在外面造成麻烦......

DECLARE @TEST_PRODUCT TABLE 
(  
    PRODUCT_ID int,   
    PRODUCT_NAME varchar(255)
);

DECLARE @TEST_PROD_VALUES TABLE
(
    ID int, 
    PRODUCT_ID  int,
    PROD_VALUES_VALUE varchar(255)
);

DECLARE @TEST_VENDOR TABLE
(
    ID int,  
    PRODUCT_ID  int,
    VENDOR_NAME varchar(255)
);

INSERT INTO @TEST_PRODUCT (PRODUCT_ID, PRODUCT_NAME) 
VALUES  (1,'PRODUCT 1'), (2,'PRODUCT 2')

INSERT INTO @TEST_PROD_VALUES (ID,PRODUCT_ID,PROD_VALUES_VALUE) 
VALUES  (1,1,'PROD_VALUES 1'), (2,2,'PROD_VALUES 2')

INSERT INTO @TEST_VENDOR (ID,PRODUCT_ID, VENDOR_NAME) 
VALUES  (1,1,'VENDOR 1'), (2,2,'VENDOR 2');

- 查询

SELECT (
        SELECT p.PRODUCT_ID AS [HEADER_TEST_PRDUCT/PRODUCT_ID]
              ,p.PRODUCT_NAME AS [HEADER_TEST_PRDUCT/PRODUCT_NAME]
        FOR XML PATH('ROOT'),TYPE
       ) AS [FILE]
      ,'' --neede to separate `<FILE>` nodes
      ,(
        SELECT v.ID AS [HEADER_TEST_PROD_VALUES/ID]
              ,v.PRODUCT_ID AS [HEADER_TEST_PROD_VALUES/PRODUCT_ID]
              ,v.PROD_VALUES_VALUE AS [HEADER_TEST_PROD_VALUES/PROD_VALUES_VALUE]
        FROM  @TEST_PROD_VALUES AS v
        WHERE v.PRODUCT_ID=p.PRODUCT_ID
        FOR XML PATH('LIST_DETAILS_PROD_VALUES'),ROOT('ROOT'),TYPE
       ) AS [FILE]
      ,'' --neede to separate `<FILE>` nodes
      ,(
        SELECT vd.ID AS [HEADER_TEST_VENDOR/ID]
              ,vd.PRODUCT_ID AS [HEADER_TEST_VENDOR/PRODUCT_ID]
              ,vd.VENDOR_NAME AS [HEADER_TEST_VENDOR/VENDOR_NAME]
        FROM  @TEST_VENDOR AS vd
        WHERE vd.PRODUCT_ID=p.PRODUCT_ID
        FOR XML PATH('LIST_DETAILS_VENDOR'),ROOT('ROOT'),TYPE
       ) AS [FILE]
FROM  @TEST_PRODUCT AS p
FOR XML PATH('LIST');

结果

<LIST>
  <FILE>
    <ROOT>
      <HEADER_TEST_PRDUCT>
        <PRODUCT_ID>1</PRODUCT_ID>
        <PRODUCT_NAME>PRODUCT 1</PRODUCT_NAME>
      </HEADER_TEST_PRDUCT>
    </ROOT>
  </FILE>
  <FILE>
    <ROOT>
      <LIST_DETAILS_PROD_VALUES>
        <HEADER_TEST_PROD_VALUES>
          <ID>1</ID>
          <PRODUCT_ID>1</PRODUCT_ID>
          <PROD_VALUES_VALUE>PROD_VALUES 1</PROD_VALUES_VALUE>
        </HEADER_TEST_PROD_VALUES>
      </LIST_DETAILS_PROD_VALUES>
    </ROOT>
  </FILE>
  <FILE>
    <ROOT>
      <LIST_DETAILS_VENDOR>
        <HEADER_TEST_VENDOR>
          <ID>1</ID>
          <PRODUCT_ID>1</PRODUCT_ID>
          <VENDOR_NAME>VENDOR 1</VENDOR_NAME>
        </HEADER_TEST_VENDOR>
      </LIST_DETAILS_VENDOR>
    </ROOT>
  </FILE>
</LIST>
<LIST>
  <FILE>
    <ROOT>
      <HEADER_TEST_PRDUCT>
        <PRODUCT_ID>2</PRODUCT_ID>
        <PRODUCT_NAME>PRODUCT 2</PRODUCT_NAME>
      </HEADER_TEST_PRDUCT>
    </ROOT>
  </FILE>
  <FILE>
    <ROOT>
      <LIST_DETAILS_PROD_VALUES>
        <HEADER_TEST_PROD_VALUES>
          <ID>2</ID>
          <PRODUCT_ID>2</PRODUCT_ID>
          <PROD_VALUES_VALUE>PROD_VALUES 2</PROD_VALUES_VALUE>
        </HEADER_TEST_PROD_VALUES>
      </LIST_DETAILS_PROD_VALUES>
    </ROOT>
  </FILE>
  <FILE>
    <ROOT>
      <LIST_DETAILS_VENDOR>
        <HEADER_TEST_VENDOR>
          <ID>2</ID>
          <PRODUCT_ID>2</PRODUCT_ID>
          <VENDOR_NAME>VENDOR 2</VENDOR_NAME>
        </HEADER_TEST_VENDOR>
      </LIST_DETAILS_VENDOR>
    </ROOT>
  </FILE>
</LIST>