Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check Weather a database exists

Status
Not open for further replies.

jluost1

Programmer
Jun 8, 2001
78
US
I would like to create a database via a store procedure by using RESTORE DATABASE and give the new database a name.

How do I find out the database with the new name already exists (or the new database name is used already)?

Thanks.
 
Oooops. Please pardon my spelling error on the subject line. I wish I could erase it on your screen.
 
I'm not sure I completely understand the question, but if I'm not mistaken the RESTORE command automatically checks to see if the database exists and does not overwrite it if it does. (unless you specify the REPLACE keyword).

So the restore would generate an error that you could trap if you tried to restore a database with a name that already exists.

J. Jones
jjones@cybrtyme.com
 
Hi jluost1,
You can use
IF NOT EXISTS(SELECT * FROM MASTER..SYSDATABASES WHERE NAME='myDataBaseName')
....


Hope it works!
 
Thank you both for your input. I tried it in Query Analyzer and it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top