我是否可以使用脚本来识别存储过程中未使用的所有表。
非常感谢!
答案 0 :(得分:3)
试试这个。
SELECT name AS Tables_Not_Used_By_SP
FROM sys.tables
EXCEPT
SELECT DISTINCT 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
推荐此 answer 以查找从属表
答案 1 :(得分:1)
我将2个查询合并为一个
1)从数据库中获取所有表格 2)NOT IN所有依赖表
SELECT
*
FROM
sys.tables
WHERE
object_id NOT IN (
SELECT d.referenced_major_id
FROM sys.sql_dependencies d
INNER JOIN sys.procedures p
ON p.object_id = d.object_id
)
ORDER BY name