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

前端 未结 10 1810
终归单人心
终归单人心 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:02

    Here is an example to find list of tables used in a procedure

    ;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
    

    Also, this query returns all the table names of all dependent tables in a Stored procedure.

    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
    

提交回复
热议问题