SQL 拋出異常錯誤信息- RAISERROR (存儲過程)
1、描述:生成錯誤消息並啟動會話的錯誤處理。RAISERROR可以引用存儲在系統消息目錄視圖或動態生成消息。該消息將作為服務器錯誤消息返回到調用應用程序或TRY…CATCH構造的關聯CATCH塊。新應用程序應該改用THROW。
2、作用: raiserror 是用於拋出一個錯誤。
3、語法:
RAISERROR ( { msg_id | msg_str | @local_variable }
{ ,severity ,state }
[ ,argument [ ,...n ] ]
)
[ WITH option [ ,...n ] ]
4、參數說明:
參數1:{ msg_id | msg_str | @local_variable }
msg_id:表示可以是一個sys.messages表中定義的消息代號;
使用 sp_addmessage 存儲在 sys.messages 目錄視圖中的用戶定義錯誤消息號。
用戶定義錯誤消息的錯誤號應當大於 50000。
msg_str:表示也可以是一個用戶定義消息,該錯誤消息最長可以有 2047 個字符;
(如果是常量,請使用N'xxxx',因為是nvarchar的)
當指定 msg_str 時,RAISERROR 將引發一個錯誤號為 5000 的錯誤消息。
@local_variable:表示也可以是按照 msg_str 方式的格式化字符串變量。
參數2:severity
用戶定義的與該消息關聯的嚴重級別。(這個很重要)
任何用戶都可以指定 0 到 18 之間的嚴重級別。
[0,10]的閉區間內,不會跳到catch;
如果是[11,19],則跳到catch;
如果[20,無窮),則直接終止數據庫連接;
參數3:state
如果在多個位置引發相同的用戶定義錯誤,
則針對每個位置使用唯一的狀態號有助於找到引發錯誤的代碼段。
介於 1 至 127 之間的任意整數。(state 默認值為1)
當state 值為 0 或大於 127 時會生成錯誤!
參數4:argument
用於代替 msg_str 或對應於 msg_id 的消息中的定義的變量的參數。
參數5:option
錯誤的自定義選項,可以是下表中的任一值:
LOG :在錯誤日志和應用程序日志中記錄錯誤;
NOWAIT:將消息立即發送給客戶端;
SETERROR:將 @@ERROR 值和 ERROR_NUMBER 值設置為 msg_id 或 50000;
5、SQL代碼示例
5.1
DECLARE @raiseErrorCode nvarchar(50)
SET @raiseErrorCode = CONVERT(nvarchar(50), YOUR UNIQUEIDENTIFIER KEY)
RAISERROR('%s INVALID ID. There is no record in table',16,1, @raiseErrorCode)
5.2
RAISERROR (
N'This is message %s %d.', -- Message text,
10, -- Severity,
1, -- State,
N'number', -- First argument.
5 -- Second argument.
);
-- The message text returned is: This is message number 5.
GO
5.3
RAISERROR (N'<<%*.*s>>', -- Message text.
10, -- Severity,
1, -- State,
7, -- First argument used for width.
3, -- Second argument used for precision.
N'abcde'); -- Third argument supplies the string.
-- The message text returned is: << abc>>.
GO
5.4
RAISERROR (N'<<%7.3s>>', -- Message text.
10, -- Severity,
1, -- State,
N'abcde'); -- First argument supplies the string.
-- The message text returned is: << abc>>.
GO
5.5 從 CATCH 塊返回錯誤消息
以下代碼示例顯示如何在 TRY 塊中使用 RAISERROR 使執行跳至關聯的 CATCH 塊中。
它還顯示如何使用 RAISERROR 返回有關調用 CATCH 塊的錯誤的信息。
BEGIN TRY
RAISERROR ('Error raised in TRY block.', -- Message text.
16, -- Severity.
1 -- State.
);
END TRY
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
RAISERROR (@ErrorMessage, -- Message text.
@ErrorSeverity, -- Severity.
@ErrorState -- State.
);
END CATCH;
5.6 在 sys.messages 中創建即席消息
以下示例顯示如何引發 sys.messages 目錄視圖中存儲的消息。
該消息通過 sp_addmessage 系統存儲過程,以消息號50005添加到 sys.messages 目錄視圖中。
sp_addmessage @msgnum = 50005,
@severity = 10,
@msgtext = N'<<%7.3s>>';
GO
RAISERROR (50005, -- Message id.
10, -- Severity,
1, -- State,
N'abcde'); -- First argument supplies the string.
-- The message text returned is: << abc>>.
GO
sp_dropmessage @msgnum = 50005;
GO
5.7 使用局部變量提供消息文本
以下代碼示例顯示如何使用局部變量為 RAISERROR 語句提供消息文本。
sp_addmessage @msgnum = 50005,
@severity = 10,
@msgtext = N'<<%7.3s>>';
GO
RAISERROR (50005, -- Message id.
10, -- Severity,
1, -- State,
N'abcde'); -- First argument supplies the string.
-- The message text returned is: << abc>>.
GO
sp_dropmessage @msgnum = 50005;
GO
創建時間:2020.10.29 更新時間:2021.02.05