How to check a duplicate database through tsql in sql server 2000 or 2005

  • Dear Sir,

    I have written a Store Procedure for creating a database but before creating the database I want to check the database is already exist in server. If the system get true then I will give a warning message else it will create the database.

    Therefore How to check the database before creating please help me with sample code.

    Thanking You

    Debasis Bag

  • SQL 2005/2008

    IF EXISTS (SELECT 1 FROM sys.databases WHERE name = <database name>)

    -- database exists

    SQL 2000

    IF EXISTS (SELECT 1 FROM sysdatabases WHERE name = <database name>)

    -- database exists

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply