2014-05-13 46 views
2

我試圖剝離存儲過程到核心從屬列和表。我在那裏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') 

回答

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'在Query Analyzer)

SQL Fiddle

+0

+1'sp_depends'! – flup