what does the second @@error return?

  • Comments posted to this topic are about the item what does the second @@error return?

  • hi,

    the below given set of statements given the same error...


    select 'def'

    raiserror('oh no, another error!', 16,1)

    select 'xyz'

    select @@error

    (1 row(s) affected)

    Msg 50000, Level 16, State 1, Line 2

    oh no, another error!

    (1 row(s) affected)

    (1 row(s) affected)

  • Did this without query analyser.. and got it right, nice.

    Hi wall str33t,

    Nice question!

    Hi Charles,

    Both queries will give the same error, but the question was asking what was returned in the results, not the messages.

    The first query when ran by itself will error (you get messages), and the results panel will show two result sets, the first 'abc' , the second the error result set with the error number.

    The second query will have three result sets:



    and 0

    Because as wall str33t says the second query executes fine, clears @@error, therefore no error is held in the @@error variable when selected.

  • hi,

    yes correct

    i missunderstood the question

    means i haven't read the question correctly

  • A question about SQL today! Well done, and thanks.

    @@error is a bit less relevant these days (TRY/CATCH) but it's still a good general-knowledge question.

  • While I see that the answer is correct, I found the question to be confusing, therefore I feel that it was not a good question.

  • I didn't have to run the code to know the answer, but, what if I did? It still would be a learning experience which is the point of the QotD. I find the negativity against running code to be irritating.

    Good question, though. Nicely thought out and very clear as to its intent. 🙂

  • Good question!

    I purposely did not run this in Query Editor, and tried to answer on my own. (I got it right!)

    I had to keep in mind that @@ERROR really only applies to the most recent line. From what I saw, SELECT 'XYZ' would not return an error!

    Check out my blog at https://pianorayk.wordpress.com/

  • Good question. The phrasing could have been more precise, such as what does the second 'select @@error' statement return, but I think that is beside the point and should not be grounds for criticizing the question. I just plain missed that the select 'xyz' statement resets the @@error value to 0, so it was a good exercise in reading code more carefully.



    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • I like the question. Nice job.

    Jason...AKA CirqueDeSQLeil
    I have given a name to my pain...MCM SQL Server, MVP
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Good question, I liked it. Starting from SQL Server 2005, getting the error information from the @@error is certainly becoming less popular due to availability of the structured error handling via try/catch blocks and functions like error_number(), error_message() and error_line(), but it is still good to know and understand the behavior of the good old friend @@error.


  • Nice question.

    Tricky though. Makes you read closely.

  • Mohamed-401375 (4/26/2010)

    Nice question.

    Tricky though. Makes you read closely.

    I agree. My first reaction was that the two "raiseerror' invocations were identical. But then I did look more closely and realized that the question was not about "raiseerror" but rather was on "@@error". Once I saw that, the answer was obvious. Perhaps anyone saying this was a confusing, and therefore sub-standard, QOD should consider whether their confusion was borne of their own haste.

  • What is interesting is that approximately a third of the respondents have gotten the question wrong.

  • (1 row(s) affected)

    Msg 50000, Level 16, State 1, Line 2

    oh no, error!

    (1 row(s) affected)

    (1 row(s) affected)

    Msg 50000, Level 16, State 1, Line 6

    oh no, another error!

    (1 row(s) affected)

Viewing 15 posts - 1 through 15 (of 22 total)

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