简体   繁体   English

在 SQL Server 中使用 RAISERROR 引发自定义错误消息

[英]Raise custom error message with RAISERROR in SQL Server

In previous versions we raised errors in t-sql like:在以前的版本中,我们在 t-sql 中引发了错误,例如:

RAISERROR 50000 'My Error Message'

In the latest SQL Server this syntax has been discontinued and replace with the RaiseError () syntax.在最新的 SQL Server 中,此语法已停止使用并替换为 RaiseError() 语法。

I would like to have a generic method of raising errors, and the best I could come up so far is:我想要一个引发错误的通用方法,到目前为止我能想到的最好的方法是:

sp_addmessage @msgnum = 50001,
              @severity = 10,
              @msgtext = N'My Error Message', @replace = 'REPLACE';
RAISERROR (50001, 10, 1, 'This error message is not displayed')

But I can't go and create a error message with sp_addmessage for every message, because there are 1000's.但是我无法为每条消息创建带有 sp_addmessage 的错误消息,因为有 1000 条。

What is the better way to raise messages with a custom message?使用自定义消息引发消息的更好方法是什么?

这似乎有效:

RAISERROR('My Error Message',0,1)

Actually, RAISERROR has been deprecated in favour of THROW since SQL Server 2012. Go here for more information.实际上,自 SQL Server 2012 以来, RAISERROR已被弃用而支持THROW 。请访问此处了解更多信息。 One of the more amusing aspects is that it is Rais e rror and not Rais eE rror leading to it being called "raise ror" in some circles.其中一个更有趣的方面是,它是赖斯ËRROR,而不是赖斯EE RROR导致它在某些圈子里被称为“养ROR”。

Sample from BOL:来自 BOL 的示例:

USE tempdb;
GO
CREATE TABLE dbo.TestRethrow
(    ID INT PRIMARY KEY
);
BEGIN TRY
    INSERT dbo.TestRethrow(ID) VALUES(1);
--  Force error 2627, Violation of PRIMARY KEY constraint to be raised.
    INSERT dbo.TestRethrow(ID) VALUES(1);
END TRY
BEGIN CATCH

    PRINT 'In catch block.';
    THROW;
END CATCH;

Use the s% wild card so that you can pass in any message you like from any of your stored procs:使用s%通配符,以便您可以从任何存储的过程中传递您喜欢的任何消息:

IF NOT EXISTS (SELECT * FROM sys.messages m WHERE m.message_id = 62000)
EXEC sys.sp_addmessage @msgnum = 62000, 
                       @severity = 16, 
                       @msgtext = N'%s', 
                       @lang = 'us_english'

Then in your sp you can raise the error like this:然后在您的 sp 中,您可以像这样引发错误:

RAISERROR (62000, 16, 1, 'Error and/or Business Error Text goes here')

Alternatively use the state attribute of raise error或者使用 raise error 的 state 属性

Step 1: In Proc第 1 步:在进程中

RAISERROR ('Error: bla bla business check 1 failed', 16,5) RAISERROR ('错误:bla bla 业务检查 1 失败', 16,5)

RAISERROR ('Error: bla bla business check 1 failed', 16,6) RAISERROR ('错误:bla bla 业务检查 1 失败', 16,6)

5 = state. 5 = 状态。 can use 1 to 255 for your purpose.可以为您的目的使用 1 到 255。 1 is used by system. 1 被系统使用。 Anything other than 1 needs to be custom handled除了 1 之外的任何东西都需要自定义处理

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/raiserror-transact-sql?redirectedfrom=MSDN&view=sql-server-ver15 state Is an integer from 0 through 255. Negative values default to 1. Values larger than 255 should not be used. https://docs.microsoft.com/en-us/sql/t-sql/language-elements/raiserror-transact-sql?redirectedfrom=MSDN&view=sql-server-ver15 state 是从 0 到 255 的整数。负值默认为 1。不应使用大于 255 的值。

step b) in c# use catch (sqlException e) e.state can be used to check步骤b)在c#中使用catch(sqlException e)e.state可以用来检查

Above worked for me.以上为我工作。 Thanks to the different articles in different places感谢不同地方的不同文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM