How can i get the list of tables in the stored procedure

前端 未结 10 1811
终归单人心
终归单人心 2020-12-01 05:16

There are lot of tables and sp in the db. I find the tables name which are used in the specific sp (stored procedure).

sp_depends %sp_name% not give the

10条回答
  •  心在旅途
    2020-12-01 06:13

    Looks like there is no complete answer for OP question. Most of the answers above don't have either schema name or include other objects (e.g. functions) used in stored procedures.

    Full list of tables/views used in stored procedures with schema name and object 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')
    

    Note that there is a filter on dependent object types which can be changed (depends on what you want in output results). Full list of type abbreviations is here.

提交回复
热议问题