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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check if database exists

Status
Not open for further replies.

AnNguyen

Programmer
Joined
Jul 18, 2002
Messages
33
Location
US
Please help.

How can i check the existence of a database prior to droping it.
i tried this the following statement but it didn't work

if EXISTS ( select * from sysobjects where name like 'database name')


Thanks for any help.
 
AnNguyen,

Here you go, just change the name of the DB from TekTips to the name you want:

IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'TekTips')
DROP DATABASE [TekTips]


Logicalman
 
Thank you!
It's works and i appreciate your help.
 
AnNguyen,

No problem. The error in your code was two-fold.

1. Databases are stored in the sysDatabases table of the Master Database (the sysobjects table stores details of Views ans SProcs)

2. You need to use the full three part naming protocol, (database.owner.objectname) then you can call it from any database (as long as the login as priveledges).

Happy coding,

Logicalman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top