Home All Groups Group Topic Archive Search About

Checking to see if a database exists...

Author
29 Jun 2005 7:08 PM
RSH
I'm creating databases programatically in .Net and I want to verify that the
database doesn't exist before creating it.  i found the SQL code:

IF NOT EXISTS(SELECT * FROM <databasename>)

But when I run the code against a known database, even in SQL Management
Studio I get a "Incorrect syntax near ')' error.

What am i doing wrong?

Thanks
RSH

Author
29 Jun 2005 7:44 PM
Mike Labosh
> I'm creating databases programatically in .Net and I want to verify that
> the
> database doesn't exist before creating it.  i found the SQL code:

IF EXISTS (
    SELECT *
    FROM Information_Schema.Schemata
    WHERE Catalog_Name = 'YourDatabaseName'
)
    /* Database Exists */
ELSE
    /* Database does not exist */

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"