开发者

RAISERROR and returning error message to the calling application

开发者 https://www.devze.com 2022-12-13 16:51 出处:网络
Assuming Asp.Net app calls procedure dbo.ApprovePost and@@ERROR contains a value greater than 0, then the following codewill be executed:

Assuming Asp.Net app calls procedure dbo.ApprovePost and @@ERROR contains a value greater than 0, then the following code will be executed:

CREATE PROCEDURE dbo.ApprovePost
      …
IF @@ERROR > 0
 BEGIN
     RAISERROR(‘Approval of post failed’, 16, 1)
     ROLLBACK TRANSACTION ApprovePost
     RETURN 99
 END

COMMIT TRANSACTION ApprovePost

a) I don’t know much about stored procedures, but I thought that when RAISERROR raises an error, then no code after RAISERROR will be executed, since procedure will exit the moment error was raised?!

b) I assume RAISERROR function will return the err开发者_Python百科or message back to calling Asp.Net application?

c) If so, then why does procedure also need to return value 99 to indicate failure ( BTW – I assume this value is returned to the calling Asp.Net application )?

d) And why value 99? Why not 100 or any other value?

thanx


Calling RAISERROR with a severity level higher than 10, it is 16 in your case, causes a SqlException in ADO.NET (severity levels 10 or lower are informational messages only), so returning 99 is pointless unless the exception is caught and you still can access the return parameter on the SqlCommand object (not sure it is accessible - haven't tested it).

Why 99? It's clearly some 'magic' value for the calling code.

0

精彩评论

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