如何查找数据库存储过程中使用的所有列?

时间:2016-02-24 13:22:13

标签: sql sql-server stored-procedures

如何查看数据库中所有存储过程(+视图和函数)使用的所有列和表的列表。

实施例

create procedure proc as
   select tab1.a,tab2.d,tab2.e
   from tab1
   join tab2 on tab1.b = tab2.b
   where tab1.c = 'filter'
end

输出:

tab1.a
tab1.b
tab1.c
tab2.b
tab2.d
tab2.e

我需要查看数据库中任何代码引用的所有列。谢谢。

欢迎提出意见。

2 个答案:

答案 0 :(得分:3)

create proc usp_test1
as
begin
select name,object_id from test
end

create proc usp_test2
as
begin
select * from test1
end

执行下面的代码给我

SELECT DISTINCT 
O.name SP_Name,T.name Table_Name,c.name Field_Name
FROM sys.sysdepends D 
JOIN sys.sysobjects O ON O.id = D.id
JOIN sys.sysobjects T ON T.id = D.depid
JOIN sys.columns C ON C.column_id=d.depnumber
and C.object_id=D.depID
WHERE O.xtype = 'P'


SP_Name Table_Name  Field_Name
usp_test1   test    name
usp_test1   test    object_id
usp_test2   test1   create_date
usp_test2   test1   is_ms_shipped
usp_test2   test1   is_published
usp_test2   test1   is_schema_published
usp_test2   test1   modify_date
usp_test2   test1   name
usp_test2   test1   object_id
usp_test2   test1   parent_object_id
usp_test2   test1   principal_id
usp_test2   test1   schema_id
usp_test2   test1   type
usp_test2   test1   type_desc

<强>参考文献: http://www.sqlservercentral.com/Forums/Topic1060325-149-1.aspx

答案 1 :(得分:2)

您可以使用THEN INSERT INTO queuedcourses(idnumber, code) VALUES (new.idnumber, new.code); RAISE EXCEPTION 'Course % is full, you are put in the waiting list', new.code; END IF; END IF; INSERT INTO registratedcourses(idNumber, code) VALUES (new.idnumber, new.code); RAISE NOTICE 'You are now successfully registered to course %', new.code; RETURN NULL; ,查看documentation了解更多详情

sys.dm_sql_referenced_entities