简体   繁体   中英

Check database access (SQL Server 2005 / 2008)

What is the best way to check database access from remote machine.

Here is the situation:

  • SQL Server 2005 (or 2008) is running on MACHINE1 (Windows 2003 or 2008). There is a database called ENTERPRISEDB .

  • Another server, called MACHINE2 (Windows 2003 or 2008). It has ODBC connection to MACHINE1 .

Now, what is the best and simplest SQL query to check if MACHINE2 can connect and access the ENTERPRISEDB in MACHINE1 ?

Thanks.

I always use

Select @@servername, @@SERVICENAME

Returns Server Name, Instance Name to check basic server connection. To check database connectivity itself is correct you can't beat sys.objects.

select DB_NAME(), * from sys.objects

Returns a table with current database name as the first column and a list of object names as the general data list.

If your ODBC connection is to database ENTERPRISEDB then you could run

SELECT top 1 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE CATALOG_NAME='ENTERPRISEDB'

which will return "1" if the database exists or nothing if it doesn't.

If you need a boolean/int return then try

SELECT case when COUNT(*) >0 then 1 else 0 end 
FROM INFORMATION_SCHEMA.SCHEMATA WHERE CATALOG_NAME='ENTERPRISEDB'

which will return "1" if it exists, "0" if it doesn't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM