如何更改递归查询工作?

时间:2014-05-13 14:15:21

标签: sql sql-server

我试图将存储过程删除到核心相关列和表。我在那里50%,需要一只手才能达到100%的标记。

这就是我所拥有的,它为我提供了存储过程中的核心表对象依赖项。我需要的还有核心列对象依赖项。

CREATE FUNCTION GetDependents
     (@ObjectName AS SYSNAME)
RETURNS @result TABLE (Seq INT IDENTITY, 
                       ObjectName SYSNAME, 
                       Hierarchy VARCHAR(128)
                      )
AS
BEGIN
    ;WITH Obj AS 
    (
        SELECT DISTINCT
            s.id  AS ParentID,
            s.DepID AS ObjectID,
            o1.Name AS ParentName,
            o2.Name AS ChildName,
            QUOTENAME(sch1.name) + '.' + QUOTENAME(o1.Name) + '(' + RTRIM(o1.type) + ')' COLLATE SQL_Latin1_General_CP1_CI_AS AS ParentObject, 
            QUOTENAME(sch2.name) + '.' + QUOTENAME(o2.Name) + '(' + RTRIM(o2.type) + ')' COLLATE SQL_Latin1_General_CP1_CI_AS AS ObjectName
        FROM 
            sys.sysdepends s
        INNER JOIN 
            sys.all_objects o1 ON s.id = o1.object_id
        INNER JOIN 
            sys.schemas sch1 ON sch1.schema_id = o1.schema_id
        INNER JOIN 
            sys.all_objects o2 on s.DepID = o2.object_id
        INNER JOIN 
            sys.schemas sch2 ON sch2.schema_id = o2.schema_id
        WHERE 
            o1.object_id <> o2.object_id
    ), cte AS 
    (
        SELECT 
            0 AS lvl, 
            ParentID,
            ObjectId,
            ParentObject,
            ObjectName,
            CAST(ObjectID AS VARBINARY(512)) AS Sort
        FROM 
            obj 
        WHERE 
            ParentName = @ObjectName

        UNION ALL 

        SELECT
            p.lvl + 1, 
            c.ParentID, c.ObjectId, c.ParentObject, c.ObjectName,
            CAST(p.sort + CAST(c.ObjectID AS VARBINARY(16)) AS VARBINARY(512))
        FROM 
            cte p 
        INNER JOIN 
            obj c ON p.ObjectID = c.ParentID
    )
    INSERT INTO @result (ObjectName, Hierarchy)
        SELECT 
            ObjectName, '|-' + REPLICATE('-',(lvl * 4)) + ObjectName 
        FROM 
            cte
        ORDER BY 
            Sort

    RETURN 
END

CREATE PROCEDURE test1
AS
SELECT A.BusinessEntityID, 
       A.NationalIDNumber, 
       A.LoginID, 
       A.OrganizationNode, 
       A.OrganizationLevel, 
       A.JobTitle, 
       A.BirthDate, 
       A.Gender, 
       A.HireDate, 
       A.SalariedFlag, 
       A.VacationHours, 
       A.SickLeaveHours, 
       A.CurrentFlag, 
       A.rowguid, 
       A.ModifiedDate
FROM  HumanResources.Employee AS A INNER JOIN
      HumanResources.Employee AS B ON A.NationalIDNumber = B.NationalIDNumber

GO

SELECT * FROM dbo.GetDependents('test1') 

1 个答案:

答案 0 :(得分:5)

你去了:你需要做的就是将col_name(s.depid, s.depnumber) AS 'column'添加到你的CTE:

declare @ObjectName AS SYSNAME
set @ObjectName = 'test1'

    ;WITH Obj AS (
        SELECT DISTINCT
            s.id  AS ParentID,
            s.DepID AS ObjectID,
            o1.Name AS ParentName,
            o2.Name AS ChildName,
            QUOTENAME(sch1.name) + '.' + QUOTENAME(o1.Name) 
                     + '(' + RTRIM(o1.type) + ')' 
                COLLATE SQL_Latin1_General_CP1_CI_AS 
                     AS ParentObject, 
            QUOTENAME(sch2.name) + '.' + QUOTENAME(o2.Name) 
                     + '(' + RTRIM(o2.type) + ')' 
                COLLATE SQL_Latin1_General_CP1_CI_AS AS ObjectName,
            col_name(s.depid, s.depnumber) AS 'column'
        FROM sys.sysdepends s

        INNER JOIN sys.all_objects o1 ON s.id = o1.object_id
        INNER JOIN sys.schemas sch1 ON sch1.schema_id = o1.schema_id
        INNER JOIN sys.all_objects o2 on s.DepID = o2.object_id
        INNER JOIN sys.schemas sch2 ON sch2.schema_id = o2.schema_id
              WHERE o1.object_id <> o2.object_id
    ), cte AS (
        SELECT 
            0 AS lvl, 
            ParentID,
            ObjectId,
            ParentObject,
            ObjectName,
            [column],
            CAST(ObjectID AS VARBINARY(512)) AS Sort
        FROM obj WHERE ParentName = @ObjectName
        UNION ALL 
        SELECT
            p.lvl+ 1, 
            c.ParentID,
            c.ObjectId,
            c.ParentObject,
            c.ObjectName,
            c.[column],
            CAST(p.sort + CAST(c.ObjectID AS VARBINARY(16)) 
              AS VARBINARY(512))
        FROM cte p 
        INNER JOIN obj c ON p.ObjectID = c.ParentID
    )
       SELECT 
           ObjectName,         [column],

           '|-' + REPLICATE('-',(lvl * 4)) + ObjectName 
       FROM cte
       ORDER BY Sort

<强>结果:

|      OBJECTNAME | COLUMN |          COLUMN_2 |
|-----------------|--------|-------------------|
| [dbo].[test](U) |      t | |-[dbo].[test](U) |
| [dbo].[test](U) |     t1 | |-[dbo].[test](U) |
| [dbo].[test](U) |     t2 | |-[dbo].[test](U) |
顺便说一句,你知道sp_depends吗?它做了同样的事情(提示:在查询分析器中输入sp_helptext N'sys.sp_depends'

SQL Fiddle