SQLServer 查询某张表是否存在于数据库
本查询在SqlServer 2016 上均执行成功
此例中数据库为testdb1,testdb2,表名为TestTable
查询
declare @tablename as varchar(64)
set @tablename = ‘TestTable’
select
case when exists(select * from testdb1.sys.tables where name = @tablename) then ‘Existence’ end as ‘testdb1’,
case when exists(select * from testdb2.sys.tables where name = @tablename) then ‘Existence’ end as ‘testdb2’;
若查询多个库,则直接在后面追加:case when exists(select * from testdb.sys.tables where name = @tablename) then ‘Existence’ end as ‘testdb’;即可。
结果
testdb1 | testdb2 |
---|---|
Existence | NULL |