How to determine if database exists on linked server?

浪尽此生 提交于 2019-12-24 15:35:45

问题


I know you can do something like:

select count(*) as Qty from sys.databases where name like '%mydatabase%'

but how could you do something like:

select count(*) as Qty from linkedServer.sys.databases where name like '%mydatabases%'

I guess I could put a stored procedure on the linked server and execute the first select, but is there a way to query a linked server for what databases it holds?


回答1:


Assuming your linked server login has read permissions on the master.sys.databases table, you can use the following:

select * from linkedserver.master.sys.databases

In the past, I've used this very query on SQL Server 2008 R2.




回答2:


Listed below is a link to a cursor that works: http://jasonbrimhall.info/2012/03/05/are-my-linked-servers-being-used/

The query will need some rework to include all functions and triggers though.




回答3:


I think its just a matter of your syntax that is stopping you, try using single quotes instead of %% around your database name:

SELECT COUNT(*) as Qty FROM LinkedServer.master.sys.databases where name like 'mydatabase'

The correct formatting for selecting a Linked Server has already been answered here:

SQL Server Linked Server Example Query



来源:https://stackoverflow.com/questions/5806854/how-to-determine-if-database-exists-on-linked-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!