![](/img/trans.png)
[英]SQL Server Select from a table returned by a query as list of table names
[英]Select SQL table names with no dependencies
我见过可以显示SQL表依赖项的脚本,但是没有可以选择没有依赖关系表的表名的脚本。
理想情况下,我想选择所有以Q开头的表名(如下所示),并且不选择MSSQL中的依赖项,即
SELECT t.NAME AS TableName
FROM sys.Tables t
where t.Name LIKE 'Q%' and no dependencies!
您应该在sys.sql_expression_dependencies上保留join并获取未引用的表,例如:
SELECT t.NAME AS TableName
FROM sys.Tables t
LEFT JOIN sys.sql_expression_dependencies d ON d.referenced_id = t.object_id
WHERE t.Name LIKE 'Q%'
AND d.referenced_id IS NULL
这个怎么样:
select * from INFORMATION_SCHEMA.TABLES T
where T.TABLE_NAME like 'Q%'
and not exists (
select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
where TC.TABLE_NAME = T.TABLE_NAME
and TC.CONSTRAINT_TYPE = 'FOREIGN KEY' -- add conditions as needed
)
order by TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.