user1679941
user1679941

Reputation:

How can I find the error code thrown in SQL Server?

I have this code:

IF (@@ROWCOUNT != 1)
    THROW 50004,'mark_test - Problem setting test status to Marked',1

I know I can get the error message like this:

SET @ErrorMessage = ERROR_MESSAGE()

But how can I get the number 50004 ?

Upvotes: 1

Views: 538

Answers (2)

nnunes10
nnunes10

Reputation: 550

You can use @@ERROR variable. See the documentation

Upvotes: 1

Pரதீப்
Pரதீப்

Reputation: 93724

Use ERROR_NUMBER function

Returns the error number of the error that caused the CATCH block of a TRY…CATCH construct to be run.

DEMO

BEGIN TRY
    SELECT 1
    UNION ALL
    SELECT 1

    IF ( @@ROWCOUNT != 1 )
      THROW 50004, 'mark_test - Problem setting test status to Marked', 1
END TRY

BEGIN CATCH
    SELECT Error_number=Error_number(),
           Error_message=Error_message()
END CATCH 

Result

Error_number    Error_message
50004           mark_test - Problem setting test status to Marked

Upvotes: 1

Related Questions