从SQL Server表

时间:2016-05-31 20:28:07

标签: c# sql-server xml tsql webforms

我有一个包含3个表的SQL Server数据库,我需要从这些表中创建一个具有层次结构的XML文件。

一个表包含产品:

ProductID   ProductLanguageID ProductDefaultShopID  ProductNumber ProductName ProductPrice
------------------------------------------------------------------------------------------
100101@@Inc LANG2             Inc                   100101        ABC         159,61
100102@@Inc LANG2             Inc                   100102        BCD         159,61
100105@@Inc LANG2             Inc                   100105        FRE         159,61
100106@@Inc LANG2             Inc                   100106        GDE         159,61

第二个有群组

GroupID     GroupLanguageID   GroupName          GroupNumber
------------------------------------------------------------
1@@Inc      LANG2             AAA                1
1.01@@Inc   LANG2             BBB                1.01
1.02@@Inc   LANG2             CCC                1.02
1.03@@Inc   LANG2             DDD                1.03
1.12@@Inc   LANG2             GGG                1.12

第三语言

LanguageID  LanguageCode2   LanguageName
----------------------------------------
LANG1       ES              Spain
LANG2       EN              English

我还有2个表与另一个表没有任何连接:

集团关系表

GroupRelationsGroupID   GroupRelationsParentID
--------------------------------------------
1.01@@Inc               1@@Inc
1.02@@Inc               1@@Inc
1.03@@Inc               1.02@@Inc
1.12@@Inc               1.03@@Inc

集团产品关系表

GroupProductRelationGroupID    GroupProductRelationProductID
-------------------------------------------------------------
1.01@@Inc                      100101@@Inc
1.03@@Inc                      100102@@Inc
1.12@@Inc                      100105@@Inc

XML输出应具有以下结构:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <productgroup1>
    <groupname>AAA</groupname>
    <productgroup2>
      <groupname>BBB</groupname>
      <productgroup3>
        <groupname>CCC</groupname>
        <products>
          <product>
            <itemName>ABC</itemName>
            <itemNumber>100303</itemNumber>
            <itemPrice>159.61</itemPrice>
            <ExtraProductgroup>
              <extraProductgroupName>GGG</extraProductgroupName>
              <itemName>FRE</itemName>
              <itemNumber>100305</itemNumber>
              <itemPrice>159.61</itemPrice>
          </ExtraProductgroup>
          </product>
        <product>
            <itemName>BCD</itemName>
            <itemNumber>100302</itemNumber>
            <itemPrice>159.61</itemPrice>
          </product>
        </products>
      </productgroup3>
    </productgroup2>
  </productgroup1>
</root> 

我可能只使用T-SQL和C#(ASP.Net Web窗体)。救命!!! : - )

1 个答案:

答案 0 :(得分:0)

我必须承认,我真的怀疑,你会对这个结构感到满意......当产品与level2组相关时,为什么产品低于groupLevel3?为什么ABC和BCD位于同一水平? ExtraGroup是如何找到的?

此解决方案通常不会解决所有数据,但至少会使用给定数据准确回收所需数据。

CREATE TABLE #Products(ProductID VARCHAR(50),ProductLanguageID VARCHAR(50),ProductNumber INT, ProductName VARCHAR(50), ProductPrice DECIMAL(14,4));
INSERT INTO #Products VALUES
 ('100101@@Inc','LANG2',100101,'ABC',159.61)
,('100102@@Inc','LANG2',100102,'BCD',159.61)
,('100105@@Inc','LANG2',100105,'FRE',159.61)
,('100106@@Inc','LANG2',100106,'GDE',159.61)
;

CREATE TABLE #Groups(GroupID VARCHAR(50),GroupLanguageID VARCHAR(50),GroupName VARCHAR(50),GroupNumber DECIMAL(5,2));
INSERT INTO #Groups VALUES
 ('1@@Inc','LANG2','AAA',1)
,('1.01@@Inc','LANG2','BBB',1.01)
,('1.02@@Inc','LANG2','CCC',1.02)
,('1.03@@Inc','LANG2','DDD',1.03)
,('1.12@@Inc','LANG2','GGG',1.12)
;

CREATE TABLE #Languages(LanguageID VARCHAR(50),LanguageCode2 VARCHAR(50),LanguageName VARCHAR(50));
INSERT INTO #Languages VALUES
 ('LANG1','ES','Spain')
,('LANG2','EN','English');

CREATE TABLE #GroupRelations(GroupRelationsGroupID VARCHAR(50),GroupRelationsParentID VARCHAR(50));
INSERT INTO #GroupRelations VALUES
 ('1.01@@Inc','1@@Inc')
,('1.02@@Inc','1.01@@Inc')
,('1.03@@Inc','1.02@@Inc')
,('1.12@@Inc','1.03@@Inc');

CREATE TABLE #GroupProductRelations(GroupProductRelationGroupID VARCHAR(50),GroupProductRelationProductID VARCHAR(50));
INSERT INTO #GroupProductRelations VALUES
 ('1.01@@Inc','100101@@Inc')
,('1.03@@Inc','100102@@Inc')
,('1.12@@Inc','100105@@Inc');


GO

WITH TopGroups AS
(
    SELECT gr.GroupID
    FROM #Groups AS gr
    WHERE NOT EXISTS(SELECT 1 FROM #GroupRelations AS x WHERE gr.GroupID = x.GroupRelationsGroupID)
)
,recGroup AS
(
    SELECT 1 AS GroupLevel
          ,g.GroupID
          ,g.GroupLanguageID
          ,g.GroupName
          ,g.GroupNumber
          ,gr.GroupRelationsParentID AS ParentGroupID
    FROM #Groups AS g
    LEFT JOIN #GroupRelations AS gr ON g.GroupID=gr.GroupRelationsGroupID 
    WHERE gr.GroupRelationsParentID IS NULL
    UNION ALL
    SELECT prev.GroupLevel + 1
          ,gNext.GroupID
          ,gNext.GroupLanguageID
          ,gNext.GroupName
          ,gNext.GroupNumber
          ,grNext.GroupRelationsParentID
    FROM #GroupRelations AS grNext
        INNER JOIN #Groups AS gNext ON grNext.GroupRelationsGroupID=gNext.GroupID
        INNER JOIN recGroup AS prev ON grNext.GroupRelationsParentID=prev.GroupID
)
SELECT *
INTO #tmpFullResult
FROM recGroup
LEFT JOIN #GroupProductRelations AS gp ON recGroup.GroupID=gp.GroupProductRelationGroupID
LEFT JOIN #Products AS p ON gp.GroupProductRelationProductID=p.ProductID
LEFT JOIN #Languages AS l ON p.ProductLanguageID=l.LanguageID;

SELECT * FROM #tmpFullResult;

SELECT lvl1.GroupName AS groupName 
      ,(     
        SELECT lvl2.GroupName AS groupName
              ,(
                SELECT lvl3.GroupName AS groupName
                    ,(
                        SELECT prod.ProductName AS itemName
                              ,prod.ProductNumber AS itemNumber
                              ,prod.ProductPrice AS itemPrice
                              ,(
                                    SELECT prodExtra.GroupName AS extraProductGroupName
                                          ,prodExtra.ProductName AS itemName
                                          ,prodExtra.ProductNumber AS itemNumber
                                          ,prodExtra.ProductPrice AS itemPrice
                                    FROM #tmpFullResult AS prodExtra
                                    WHERE prodExtra.ProductID IS NOT NULL 
                                      AND prodExtra.GroupLevel=5
                                    FOR XML PATH('ExtraProductgroup'),TYPE
                               )
                        FROM #tmpFullResult AS prod
                        WHERE prod.ProductID IS NOT NULL 
                          AND prod.GroupLevel<5
                        FOR XML PATH('product'),ROOT('products'),TYPE
                     )
                FROM #tmpFullResult AS lvl3
                WHERE lvl3.GroupLevel=3 AND lvl3.ParentGroupID=lvl2.GroupID
                FOR XML PATH('productGroup3'),TYPE
               )
        FROM #tmpFullResult AS lvl2
        WHERE lvl2.GroupLevel=2 AND lvl2.ParentGroupID=lvl1.GroupID
        FOR XML PATH('productGroup2'),TYPE
       )
FROM #tmpFullResult AS lvl1
WHERE lvl1.ParentGroupID IS NULL
FOR XML PATH('productgroup1'),ROOT('root')
GO
DROP TABLE #tmpFullResult;
DROP TABLE #GroupProductRelations
DROP TABLE #Groups
DROP TABLE #GroupRelations
DROP TABLE #Languages
DROP TABLE #Products

结果

<root>
  <productgroup1>
    <groupName>AAA</groupName>
    <productGroup2>
      <groupName>BBB</groupName>
      <productGroup3>
        <groupName>CCC</groupName>
        <products>
          <product>
            <itemName>ABC</itemName>
            <itemNumber>100101</itemNumber>
            <itemPrice>159.6100</itemPrice>
            <ExtraProductgroup>
              <extraProductGroupName>GGG</extraProductGroupName>
              <itemName>FRE</itemName>
              <itemNumber>100105</itemNumber>
              <itemPrice>159.6100</itemPrice>
            </ExtraProductgroup>
          </product>
          <product>
            <itemName>BCD</itemName>
            <itemNumber>100102</itemNumber>
            <itemPrice>159.6100</itemPrice>
            <ExtraProductgroup>
              <extraProductGroupName>GGG</extraProductGroupName>
              <itemName>FRE</itemName>
              <itemNumber>100105</itemNumber>
              <itemPrice>159.6100</itemPrice>
            </ExtraProductgroup>
          </product>
        </products>
      </productGroup3>
    </productGroup2>
  </productgroup1>
</root>