Home Forums SQL Server 2008 T-SQL (SS2K8) Trying to bypass via tsql a database when it is offline RE: Trying to bypass via tsql a database when it is offline

  • It does not help how many conditions you add to the IF statement. The USE statement is interpreted at compile time, why you always get the error. Here is a quick example:

    IF 1 = 9

    BEGIN

    USE nosuchdb

    END

    Output:

    Msg 911, Level 16, State 1, Line 3

    Database 'nosuchdb' does not exist. Make sure that the name is entered correctly.

    One workaround is to wrap it all in EXEC(), but that would make the code impossible to read and maintain. Another is put this in a stored procedure, which you then can call with:

    IF databasepropertyex(...) etc

    BEGIN

    EXEC archer.dbo.do_some_reindexingstuff

    END

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]