Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何查找数据库存储过程中使用的所有列?_Sql_Sql Server_Stored Procedures - Fatal编程技术网

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

Sql 如何查找数据库存储过程中使用的所有列?,sql,sql-server,stored-procedures,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 我需要查看数据库中任何代码引用的所有列。多谢各位 欢迎评论 cr

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

例如:

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
我需要查看数据库中任何代码引用的所有列。多谢各位

欢迎评论

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
参考文献:

您可以使用
sys.dm\u sql\u引用的\u实体
,有关详细信息,请查看

SELECT  referenced_entity_name + '.' + referenced_minor_name
FROM sys.dm_sql_referenced_entities('dbo.Proc1', 'OBJECT')
WHERE referenced_minor_name IS NOT NULL

您应该知道,如果您的任何存储过程使用动态SQL,那么这是无法可靠完成的。假设我没有使用动态sqlAwesome,工作得很好,我刚刚将WHERE contidon更新为“in('V','P','F'),但它只给出部分结果。我在空数据库中创建了两个存储过程作为测试用例(您可以看到上面),它向我显示了1数据库的两个存储过程,它似乎工作正常。不幸的是,没有显示外部列。这列出了很多结果,但不是全部。不会显示联接和插入。但是,当您跨链接服务器联接时,它不会显示大多数联接。只有在
INSERT
上明确指定了列名时,它才会列出INSERT。