开发者

T-SQL Procedure Custom RAISERROR Always Shows As Number 18054 In VS2010 When Using EF4

I have a stored procedure on a hosted SQL 2008 instance. In these stored procedures is logic checking for error. I then return an error code using RAISERROR(#####, ##, ###) if there is a problem. This works well enough, but there is a problem with catching such an error.

In Visual Studio 2010 I can catch the error with the correct Class and State, but never the correct Number. I will get a Message like the following in the exception:

Error #####, severity ##, state ###, was raised, but no message with that error number was found in sys.message. If error is larger than 50000, make sure the user-defined mess开发者_运维问答age is added using sp_addmessage.

So clearly the error is being raised correctly from the procedure, but the Number returned is always 18054. What am I doing wrong? I don't care what the text of the message is, and I don't have permission to run sp_addmessage anyway. However, I would like Number to be correct so I can properly catch the error in my application and handle it properly.

In case it matters, I'm calling the procedure using EF4.


Your error isn't being raised correctly. The error you're actually getting, 18054, is a system error which pretty much says,

"Hey, you called raiserror() with an error number which doesn't appear in [sys].[messages]!"

If you don't have access to add records to [sys].[messages] just specify your error number in the message text (msdn):

RAISERROR (N'321433', -- Message text. Parse as int in exception handling.
           10, -- Severity
           1) -- State
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜