IF DATABASE EXISTS ???

  • How can I do this?

    IF DATABASE MyDataBase EXISTS

    SELECT * FROM MyDataBase..MyTable

    END

     

  • Hi

    If Exists (SELECT name from sysdatabases where name = 'MyDataBase')

    BEGIN

    USE MyDataBase

    select * from MyDataBase..MyTable

    END

    "Keep Trying"

  • That's still going to give an error, from the parsing of the USE statement 

    Could not locate entry in sysdatabases for database 'MyDB'. No entry found with that name. Make sure that the name is entered correctly.

    If you remove the use statement however, it works

    If

    Exists (SELECT name from sysdatabases where name = 'MyDB')

    begin

    select * from MyDB..sysobjects

    end

    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 3 posts - 1 through 2 (of 2 total)

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