0

再会。

我试图在从分层查询中获取结果后生成一个临时表,然后在同一个查询中查看这些结果(因此递归查询从表中获取我想要的结果,然后CREATE TABLE查询显示这些结果)。

递归让我得到我想要的结果,我遇到的问题是让查询显示这些结果。我正在尝试在递归之后构建临时表查询。但是,数据返回仅向我显示递归的结果。

感谢您的任何帮助!

查询:

WITH orgCode_hierarchy (ParentOrgCodeID, OrgCodeID, OrgCode, OrgName, OrgCodeSortOrder, LEVEL) AS
(
-- Initializing:
      SELECT h_base.ParentOrgCodeID, h_base.OrgCodeID, h_base.OrgCode, h_base.OrgName, cast('::' + h_base.OrgCode  + '::' AS VARCHAR (200)) AS OrgCodeSortOrder, 0 AS LEVEL
      FROM L_OrgCode h_base
      WHERE h_base.OrgCode is not null

      UNION all

-- Executing recursive: 
      SELECT h_child.ParentOrgCodeID, h_child.OrgCodeID, h_child.OrgCode, h_child.OrgName,  cast (ho.OrgCodeSortOrder + CASE WHEN left(h_child.OrgCode, 2) = '::' THEN '::' ELSE '' END  + h_child.OrgCode + '::' AS VARCHAR (200)) AS OrgCodeSortOrder, LEVEL + 1 AS LEVEL
      FROM L_OrgCode h_child
      inner join orgCode_hierarchy ho on h_child.ParentOrgCodeID = ho.OrgCodeID
)     

-- CTE:
SELECT DISTINCT ParentOrgCodeID, OrgCodeID, OrgCode, OrgName, OrgCodeSortOrder, LEVEL
FROM orgCode_hierarchy
WHERE OrgCode LIKE '2.2.1.1%'  
-- ORDER BY 1  
ORDER BY OrgCode, 1 

--Create temp tbl for the name/org code search
    CREATE TABLE #OrgCodesHier
    (
        OrgCodeID int, 
        OrgCode_Level int,
        OrgName varchar(255),
        OrgCode varchar(50),
        ParentOrgCodeID varchar(50)

        PRIMARY KEY (OrgCode) 
    )

    INSERT INTO #OrgCodesHier (OrgCodeID, OrgCode_Level, OrgName, OrgCode, ParentOrgCodeID)

    SELECT * FROM #OrgCodesHier
4

1 回答 1

1

如果我理解正确...

    --Create temp tbl for the name/org code search
        CREATE TABLE #OrgCodesHier
        (
            OrgCodeID int, 
            OrgCode_Level int,
            OrgName varchar(255),
            OrgCode varchar(50),
            ParentOrgCodeID varchar(50)

            PRIMARY KEY (OrgCode) 
        )

    ;WITH orgCode_hierarchy (ParentOrgCodeID, OrgCodeID, OrgCode, OrgName, OrgCodeSortOrder, LEVEL) AS
    (
    -- Initializing:
          SELECT h_base.ParentOrgCodeID, h_base.OrgCodeID, h_base.OrgCode, h_base.OrgName, cast('::' + h_base.OrgCode  + '::' AS VARCHAR (200)) AS OrgCodeSortOrder, 0 AS LEVEL
          FROM L_OrgCode h_base
          WHERE h_base.OrgCode is not null

          UNION all

    -- Executing recursive: 
          SELECT h_child.ParentOrgCodeID, h_child.OrgCodeID, h_child.OrgCode, h_child.OrgName,  cast (ho.OrgCodeSortOrder + CASE WHEN left(h_child.OrgCode, 2) = '::' THEN '::' ELSE '' END  + h_child.OrgCode + '::' AS VARCHAR (200)) AS OrgCodeSortOrder, LEVEL + 1 AS LEVEL
          FROM L_OrgCode h_child
          inner join orgCode_hierarchy ho on h_child.ParentOrgCodeID = ho.OrgCodeID
    )     

    -- CTE:
INSERT INTO #OrgCodesHier (ParentOrgCodeID,OrgCodeID,OrgCode,OrgName,OrgCode_Level)
    SELECT DISTINCT ParentOrgCodeID, OrgCodeID, OrgCode, OrgName, LEVEL
    FROM orgCode_hierarchy
    WHERE OrgCode LIKE '2.2.1.1%'  
    -- ORDER BY 1  
    ORDER BY OrgCode, 1 


    SELECT * FROM #OrgCodesHier
于 2013-06-12T18:52:36.053 回答