问题
I'm having an issue on settin up SqlException.Number
On my Stored Proc i'm raising an error
--@number = 50001
RAISERROR(@number, 16, 1) -
I should expect that the Error_Number()
should be @number
but I always get 18054
Is there something wrong with my RAISERROR
?
回答1:
Check the sys.messages table for error code 74601. if this is a user defined error, it shouold be added in the table.
for any error that is greater than 50000 should give you this output if not found.
Msg 18054, Level 16, State 1, Line 1
Error XXXXX, severity 16, state 1 was raised, but no message with that error number was found in sys.messages. If error is larger than 50000, make sure the user-defined message is added using sp_addmessage.
回答2:
There is one small caveat: You can't supply a message on your own in this case. But this can be circumvented by adding an additional %s in the sp_addmessage call or by changing all mapped messages to your own pattern and supplying the right parameters in the raiseerror call.
Check there for more information: SQL Server: Rethrow exception with the original exception number
RAISERROR can either reference a user-defined message stored in the sys.messages catalog view or build a message dynamically.
Check for your error message exists or not using this:
select * from sys.messages
If it does not exists then Use sp_addmessage
to add user-defined error messages and sp_dropmessage
to delete user-defined error messages.
for more information follow RaiseError documentation.
来源:https://stackoverflow.com/questions/8411480/how-to-set-sqlexception-number