我有3个要查询的SQL服务器,但我只想查询某些数据库。这是可能的吗?我已经将它们设置为注册服务器,并且我可以跨它们查询系统类型的查询,但是,如果我希望查询类似于
SELECT * FROM SJOB
我不能,因为不是每个数据库都有这个表,所以它失败了。任何帮助都将不胜感激。谢谢!
发布于 2014-01-08 00:54:22
您可以查询sys.tables
视图以检查该表是否存在:
if exists(select * from sys.tables where type = 'U' and name = 'SJOB')
select * from SJOB
要在已注册的servers组上使用该表,还需要为不存在该表的服务器编写一个select
查询。模式需要匹配,但您可以使用top 0
不返回任何记录。但是,您需要指定所有列,并且虚拟值需要与列的类型匹配:
if exists(select * from sys.tables where type = 'U' and name = 'SJOB')
select * from SJOB
else
select top 0 1 as col1name, '' as col2name
https://stackoverflow.com/questions/20985026
复制相似问题