28

数据库中有很多表和sp。我找到了在特定 sp(存储过程)中使用的表名。

sp_depends%sp_name%没有给出期望的结果。我也习惯了INFORMATION_SCHEMA.TABLESINFORMATION_SCHEMA.ROUTINES桌子。

但结果并没有完全满足我的要求。

4

11 回答 11

21

投票率最高的两个答案使用了很多应该避免的弃用表。
这是一种更清洁的方法。

获取存储过程所依赖的所有表:

SELECT DISTINCT p.name AS proc_name, t.name AS table_name
FROM sys.sql_dependencies d 
INNER JOIN sys.procedures p ON p.object_id = d.object_id
INNER JOIN sys.tables     t ON t.object_id = d.referenced_major_id
ORDER BY proc_name, table_name

适用于 MS SQL SERVER 2005+

变更清单:

  • sysdepends应该替换为sys.sql_dependencies
    • 新表使用object_id而不是id
    • 新表使用referenced_major_id而不是depid
  • sysobjects应该用更集中的系统目录视图替换 使用
  • 此外,真的不需要一个 CTE,它只是为了确保我们只返回每个记录集中的一个。这就是它的用途!ROW_NUMBER() DISTINCT

    • 事实上,SQL 足够聪明,可以在幕后使用 DISTINCT。
    • 我提交证据: 证据A。以下查询具有相同的执行计划

      -- Complex
      WITH MyPeople AS (
        SELECT id, name,
        ROW_NUMBER() OVER(PARTITION BY id, name ORDER BY id, name) AS row
        FROM People)
      SELECT id, name
      FROM MyPeople
      WHERE row = 1
      
      -- Better
      SELECT DISTINCT id, name
      FROM People
      
于 2014-05-16T14:47:29.233 回答
21

尝试更优雅的方式(但是,它的解决方案仅适用于 MS SQL 2008 或更高版本) -

SELECT DISTINCT 
      [object_name] = SCHEMA_NAME(o.[schema_id]) + '.' + o.name
    , o.type_desc
FROM sys.dm_sql_referenced_entities ('dbo.usp_test1', 'OBJECT') d
JOIN sys.objects o ON d.referenced_id = o.[object_id]
WHERE o.[type] IN ('U', 'V')
于 2013-04-26T05:56:29.460 回答
16
;WITH stored_procedures AS (
SELECT 
o.name AS proc_name, oo.name AS table_name,
ROW_NUMBER() OVER(partition by o.name,oo.name ORDER BY o.name,oo.name) AS row
FROM sysdepends d 
INNER JOIN sysobjects o ON o.id=d.id
INNER JOIN sysobjects oo ON oo.id=d.depid
WHERE o.xtype = 'P')
SELECT proc_name, table_name FROM stored_procedures
WHERE row = 1
ORDER BY proc_name,table_name
于 2013-04-26T05:48:54.550 回答
10

这是这个的sql代码

获取存储过程中使用的表的列表

;WITH stored_procedures AS (
SELECT 
o.name AS proc_name, oo.name AS table_name,
ROW_NUMBER() OVER(partition by o.name,oo.name ORDER BY o.name,oo.name) AS row
FROM sysdepends d 
INNER JOIN sysobjects o ON o.id=d.id
INNER JOIN sysobjects oo ON oo.id=d.depid
WHERE o.xtype = 'P')
SELECT proc_name, table_name FROM stored_procedures
WHERE row = 1
ORDER BY proc_name,table_name

.

反向- 查找与数据库中的表相关的存储过程 - 在所有存储过程中搜索

有两种方法可以做到这一点

----Option 1
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%tablename%'

----Option 2
SELECT DISTINCT o.name, o.xtype
FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%tablename%'

PS:sp_help并不sp_depends总是返回准确的结果。

参考:

  1. Sql Server Central - 获取存储过程中使用的表列表
  2. SqlAuthority - 在数据库中查找与表相关的存储过程 - 在所有存储过程中搜索
于 2013-04-26T05:50:12.650 回答
6

KyleMit 的答案是使用一个很快就会被弃用的表。建议使用 sys.sql_experssion_dependencies 而不是 sys.sql_dependencies,例如,

SELECT DISTINCT p.name AS proc_name, t.name AS table_name
FROM sys.sql_expression_dependencies d
INNER JOIN sys.procedures p ON p.object_id = d.referencing_id
INNER JOIN sys.tables t     ON t.object_id = d.referenced_id
ORDER BY proc_name, table_name

这应该适用于 SQL Server 2008+。

我没有足够高的声誉来直接评论引用的答案。

于 2018-06-12T15:29:09.343 回答
5

看起来 OP 问题没有完整的答案。上面的大多数答案都没有架构名称或包含存储过程中使用的其他对象(例如函数)。

具有模式名称和对象 ID 的存储过程中使用的表/视图的完整列表

SELECT  DISTINCT
        procObj.[object_id]     AS [ProcObjectId],
        procSchema.[name]       AS [ProcSchema],
        procObj.[Name]          AS [ProcName], 
        tableObj.[object_id]    AS [TableObjectId],
        tableSchema.[name]      AS [TableSchema],
        tableObj.[Name]         AS [TableName]
FROM sys.sql_dependencies AS dep
INNER JOIN sys.objects AS procObj
ON procObj.[object_id] = dep.[object_id]
INNER JOIN sys.schemas AS procSchema 
ON procSchema.[schema_id] = procObj.[schema_id]
INNER JOIN sys.objects AS tableObj
ON tableObj.[object_id] = dep.[referenced_major_id]
INNER JOIN sys.schemas AS tableSchema 
ON tableSchema.[schema_id] = tableObj.[schema_id]
WHERE   procObj.[type] = 'P' 
    -- using this filter we can control dependent object types
    -- e.g. tableObj.[type] IN ('U') - returns tables only
    AND tableObj.[type] IN ('V', 'U')

请注意,有一个可以更改的依赖对象类型的过滤器(取决于您想要的输出结果)。类型缩写的完整列表在 这里

于 2017-10-10T00:20:55.140 回答
3
SELECT 
NAME 
FROM SYSOBJECTS 
WHERE ID IN (   SELECT SD.DEPID 
                FROM SYSOBJECTS SO, 
                SYSDEPENDS SD
                WHERE SO.NAME = 'SP_NAME' 
                AND SD.ID = SO.ID
            )
于 2016-04-15T12:50:36.987 回答
2

但请注意,sysdepends如果它们在动态 sql中使用,则不会给出表名。我的建议是反向搜索,即:创建一个循环搜索syscomments. 下面的存储过程可能会有所帮助

CREATE PROCEDURE dbo.sp_getObjects
(
    @ObjName VARCHAR(255)
)
AS
BEGIN
    SET NOCOUNT ON
    DECLARE @Idkeyst INTEGER
    DECLARE @Idkeyed INTEGER
    DECLARE @tblName VARCHAR(255)
    DECLARE @Objects VARCHAR(MAX)
    IF NOT EXISTS(SELECT 1 FROM sys.objects where NAME = @ObjName AND type in ('P', 'FN','TR'))
    BEGIN
        PRINT 'NO Text Available for the Parameter'
        RETURN(0)
    END
    CREATE TABLE #ProcStr
    (
        Idkey       INT IDENTITY(1,1),
        ScriptStr   VARCHAR(MAX)
    )
    CREATE TABLE #Depends
    (
        Idkey           INT IDENTITY(1,1),
        Depends         VARCHAR(255)
    )
    CREATE TABLE #Objects
    (
        Idkey           INT IDENTITY(1,1),
        ObjectName      VARCHAR(255)
    )
    INSERT INTO #ProcStr
    (ScriptStr)
    EXEC sp_helptext @ObjName
    DELETE #ProcStr WHERE LTRIM(ScriptStr) LIKE '--%'
    DELETE #ProcStr WHERE LTRIM(REPLACE(ScriptStr,CHAR(9),'')) LIKE '--%'

    SET @Idkeyst = 0
    SET @Idkeyed = 0
    WHILE 1=1
    BEGIN
        SELECT @Idkeyst = MIN(idKey) FROM #ProcStr WHERE ScriptStr like '%/*%' and Idkey > @Idkeyst
        IF @Idkeyst IS NULL
            BREAK
        SELECT @Idkeyed = MIN(idKey) FROM #ProcStr WHERE ScriptStr like '%*/%' and Idkey >= @Idkeyst
        DELETE #ProcStr WHERE Idkey >= @Idkeyst and Idkey <=@Idkeyed
    END
    DELETE #ProcStr WHERE ISNULL(LTRIM(REPLACE(ScriptStr,CHAR(9),'')),'')=''    
    INSERT INTO #Depends
    (Depends)
    SELECT DISTINCT t.name 
    FROM sys.sql_dependencies d 
    INNER JOIN sys.procedures p ON p.object_id = d.object_id
    INNER JOIN sys.tables     t ON t.object_id = d.referenced_major_id
    where p.name = @ObjName

    INSERT INTO #Objects
    SELECT name from sys.objects o WHERE Type = 'U' AND NOT EXISTS
    (SELECT 1 FROM #Depends WHERE Depends = o.name)

    SET @Objects = ''
    SELECT @Objects = CASE WHEN ISNULL(@Objects,'') = '' THEN '' ELSE @Objects+', ' END+ Depends
    FROM #Depends

    UPDATE #ProcStr
    SET ScriptStr = LTRIM(RTRIM(ScriptStr))
    UPDATE #ProcStr
    SET ScriptStr = REPLACE(ScriptStr,CHAR(9),'')
    UPDATE #ProcStr
    SET ScriptStr = REPLACE(ScriptStr,CHAR(13),'')
    UPDATE #ProcStr
    SET ScriptStr = REPLACE(ScriptStr,CHAR(10),'')
    SET @tblName = ''
    SET @Idkeyst = 0
    WHILE 1=1
    BEGIN
        SELECT @Idkeyst = MIN(idKey) FROM #Objects WHERE Idkey > @Idkeyst
        IF @Idkeyst IS NULL
            BREAK
        SELECT @tblName = ObjectName FROM #Objects WHERE Idkey = @Idkeyst
        IF Exists (SELECT 1 FROM #ProcStr WHERE (ScriptStr LIKE '% '+@tblName+' %' 
                        OR ScriptStr LIKE '%.'+@tblName+' %' OR ScriptStr LIKE @tblName+' %' OR ScriptStr LIKE @tblName
                        --OR ScriptStr LIKE '%'+@tblName 
                        OR ScriptStr LIKE '% '+@tblName+'''%' OR ScriptStr LIKE @tblName+'''%'))
        BEGIN
            SET @Objects = CASE WHEN ISNULL(@Objects,'')<>'' THEN @Objects+', '+@tblName ELSE @tblName END
        END
    END

    IF ISNULL(@Objects,'') = ''
    BEGIN
        PRINT 'NO Tables are reffered in the stored procedures'
        RETURN(0)
    END
    PRINT @Objects
    SET NOCOUNT OFF
END
于 2015-11-25T14:21:20.297 回答
1

这是一个查找过程中使用的表列表的示例

;WITH procs
AS
(
SELECT o1.name AS proc_name,
o2.name AS table_name,
ROW_NUMBER() OVER(PARTITION BY o1.name,o2.name ORDER BY o1.name,o2.name) AS row
FROM sysdepends d
INNER JOIN sysobjects o1
ON o1.id=d.id
INNER JOIN sysobjects o2
ON o2.id=d.depid
WHERE o1.xtype = 'P'
--AND o2.name = 'tabname1' OR o2.name = 'tblname2'
)
SELECT proc_name, table_name
FROM procs
WHERE row = 1
ORDER BY proc_name, table_name

此外,此查询返回存储过程中所有从属表的所有表名。

SELECT DISTINCT o.id, o.name as 'Procedure_Name' , oo.name as 'Table_Name'
FROM sysdepends d, sysobjects o, sysobjects oo
WHERE o.id=d.id 
and oo.id=d.depid and depnumber=1
ORDER BY o.name,oo.name
于 2013-04-26T05:48:59.700 回答
0
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Tablename%'
于 2021-08-04T14:26:00.953 回答
-1

此代码递归查看存储过程中的每个存储过程,并为您提供所有使用的表的完整列表。



    declare @sp_name varchar(100)
    declare @curSpName varchar(100)
    declare @curObjName varchar(255)
    declare @curXType varchar(1)
    ​
    create table #tmpTables
    (
    proc_name varchar(255),
    table_name varchar(255)
    )
    ​
    set @sp_name = 'STORED_PROCEDURE_NAME'
    ​
    ;WITH stored_procedures AS (
    SELECT 
    o.name AS proc_name, oo.name AS table_name, oo.type AS xType,
    ROW_NUMBER() OVER(partition by o.name,oo.name, oo.Type ORDER BY o.name,oo.name, oo.Type) AS row
    FROM sysdepends d 
    INNER JOIN sysobjects o ON o.id=d.id
    INNER JOIN sysobjects oo ON oo.id=d.depid
    WHERE o.xtype = 'P'
    --and oo.type = 'U' --Tables
    and o.name = @sp_name)
    SELECT proc_name, table_name, xType, 'N' Processed into ##tmpSP
    FROM stored_procedures 
    WHERE row = 1
    --ORDER BY proc_name,table_name
    ​
    ​
    While (Select count(*) from ##tmpSP where Processed = 'N')  0
    Begin
    ​
        Select top  1 @curSpName = proc_name, @curObjName = table_name, @curXType = xType
        from ##tmpSP
        where Processed = 'N'
    ​
    ​
        if @curXType = 'U'
        Begin
    ​
            insert into #tmpTables
            values (@curSpName, @curObjName)
    ​
        End
    ​
        if @curXType = 'P'
        Begin
    ​
            ;WITH stored_procedures AS (
            SELECT 
            o.name AS proc_name, oo.name AS table_name, oo.type AS xType,
            ROW_NUMBER() OVER(partition by o.name,oo.name, oo.Type ORDER BY o.name,oo.name, oo.Type) AS row
            FROM sysdepends d 
            INNER JOIN sysobjects o ON o.id=d.id
            INNER JOIN sysobjects oo ON oo.id=d.depid
            WHERE o.xtype = 'P'
            and oo.type = 'U' --Tables
            and o.name = @curObjName)
            insert into #tmpTables
            SELECT @curSpName, table_name 
            FROM stored_procedures 
            WHERE row = 1
            ORDER BY proc_name,table_name
    ​
            ;WITH stored_procedures AS (
            SELECT 
            o.name AS proc_name, oo.name AS table_name, oo.type AS xType,
            ROW_NUMBER() OVER(partition by o.name,oo.name, oo.Type ORDER BY o.name,oo.name, oo.Type) AS row
            FROM sysdepends d 
            INNER JOIN sysobjects o ON o.id=d.id
            INNER JOIN sysobjects oo ON oo.id=d.depid
            WHERE o.xtype = 'P'
            and oo.type = 'P' --SP's
            and o.name = @curObjName)
            insert into ##tmpSP
            SELECT proc_name, table_name, xType, 'N'  
            FROM stored_procedures 
            WHERE row = 1
            and proc_name not in 
            (
                Select proc_name
                from ##tmpSP
            )
            ORDER BY proc_name,table_name
    ​
        End
    ​
        if @curXType = 'v'
        Begin
    ​
            ;WITH stored_procedures AS (
            SELECT 
            o.name AS proc_name, oo.name AS table_name, oo.type AS xType,
            ROW_NUMBER() OVER(partition by o.name,oo.name, oo.Type ORDER BY o.name,oo.name, oo.Type) AS row
            FROM sysdepends d 
            INNER JOIN sysobjects o ON o.id=d.id
            INNER JOIN sysobjects oo ON oo.id=d.depid
            WHERE o.xtype = 'v'
            and oo.type = 'U' --Tables
            and o.name = @curObjName)
            insert into #tmpTables
            SELECT proc_name, table_name 
            FROM stored_procedures 
            WHERE row = 1
            ORDER BY proc_name,table_name
    ​
        End
    ​
        update ##tmpSP
        set Processed = 'Y'
        where table_name = @curObjName
    ​
    ​
    End
    ​
    ​
    Select distinct table_name
    from #tmpTables
    ​
    drop table #tmpTables
    drop table ##tmpSP

于 2016-03-30T15:25:13.803 回答