By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,905 Members | 879 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,905 IT Pros & Developers. It's quick & easy.

how to check does some database exist ?

P: n/a
hello

What query shoul I send to SQL serwer ( in transact SQL language ) to check
does some database exist on serwer ? It similar to problem "does some table
exist in database" - resolve to it is query:

use db_silnik
IF EXISTS (SELECT * FROM prad)
PRINT 'table exist'

but what is the query to check does some database exist on serwer ?

best regards
Adam

Aug 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
There are (at least) a couple of ways:

select db_id('SomeDatabase') -- if NULL, the database doesn't exist

if not exists (
select *
from master.dbo.sysdatabases
where name = 'SomeDatabase')
print 'Database does not exist'
Note that these queries won't tell you if the database is accessible or
not - it may be offline or loading, or your login may not have been
granted access to it; see DATABASEPROPERTYEX() and HAS_DBACCESS() in
Books Online.

Simon

Aug 15 '05 #2

P: n/a
adam (er******@wp.pl) writes:
What query shoul I send to SQL serwer ( in transact SQL language ) to
check does some database exist on serwer ?
IF db_name(@db) IS NOT NULL
PRINT 'Database exists'
It similar to problem "does some table exist in database" - resolve to
it is query:

use db_silnik
IF EXISTS (SELECT * FROM prad)
PRINT 'table exist'


Ehum, this query will fail if the table does not exist. This query checks
whether there is any data in the table.

To check whether a table exists, use:

IF object_id(@tbl, 'U') IS NOT NULL
PRINT 'Table exists'

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for
SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Aug 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.