我正在对特定的表MyTable进行一些测试,我需要删除与MyTable无关的数据库的所有表/属性。
如何使用脚本和/或SQL Server Management Studio(2012)?
免责声明:这只是在本地测试环境中,不需要任何人担心产品问题。
答案 0 :(得分:0)
你可以写一个快速光标来删除这个方便脚本结果中的所有内容:
WITH DepTree (referenced_id, referenced_name, referencing_id, referencing_name, NestLevel)
AS
(
SELECT o.[object_id] AS referenced_id ,
o.name AS referenced_name,
o.[object_id] AS referencing_id,
o.name AS referencing_name,
0 AS NestLevel
FROM sys.objects o
WHERE o.name = 'dim_table_name'
UNION ALL
SELECT d1.referenced_id,
OBJECT_NAME( d1.referenced_id) ,
d1.referencing_id,
OBJECT_NAME( d1.referencing_id) ,
NestLevel + 1
FROM sys.sql_expression_dependencies d1
JOIN DepTree r ON d1.referenced_id = r.referencing_id
)
SELECT DISTINCT referenced_id, referenced_name, referencing_id, referencing_name, NestLevel
FROM DepTree WHERE NestLevel > 0
ORDER BY NestLevel, referencing_id;