Capturing the first of two errors

  • -SQL Server 2008 SP1-

    I know why this doesn't work. My question is about capturing the first of two errors that result from this sequence of events.

    create database testa

    --database created

    --create a full backup

    backup database testa

    to disk = 'path'

    --try to create a compressed full backup

    backup database testa

    to disk = 'path'

    with compression

    -- two errors returned

    --try again in a try/catch

    --results of @@error and error_message() only show the second error

    begin try

    backup database testa

    to disk = 'path'

    with compression

    end try

    begin catch

    print @@error

    print error_message()

    end catch

    I believe this happens because the system variable @@error gets updated whenever a new error occurrs. Since these errors occurr simultaneously, I cannot catch the first one. I would like to capture the first more meaningful error but I can't find a way to do it.

    Any help would be appreciated.

    ______________________________________________________________________________________________
    Forum posting etiquette.[/url] Get your answers faster.

  • It could be only one error (3012), the errorlog can verify that. The other one (3098) could be a warning, which would not appear in catch block.

  • Need to write separate try catch block for each DML statements. So that no error will be skipped.

  • calvo (5/20/2010)


    Since these errors occurr simultaneously, I cannot catch the first one. I would like to capture the first more meaningful error but I can't find a way to do it.

    Any help would be appreciated.

    Not possible as far as I know. Apparently an error 'stack' is being considered for 'the next major release' of SQL Server.

Viewing 4 posts - 1 through 3 (of 3 total)

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