在存儲過程中進程會處理一些邏輯性的錯誤,如:將RMB轉換為USD時,沒有查詢到想要的匯率 這個時候最好在存儲過程中拋個異常,方便自己查找錯誤信息。。。
其語法如下:
RAISERROR
( { msg_id
|
msg_str
|
@local_variable
}
{ ,severity ,state }
[ ,argument [ ,...n ] ]
)
[ WITH option [ ,...n ] ]
{ ,severity ,state }
[ ,argument [ ,...n ] ]
)
[ WITH option [ ,...n ] ]
簡要說明一下:
第一個參數
:{ 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 方式的格式化字符串變量。
第二個參數:severity
用戶定義的與該消息關聯的嚴重級別。(這個很重要)
任何用戶都可以指定 0 到 18 之間的嚴重級別。
[0,10]的閉區間內,不會跳到catch;
如果是[11,19],則跳到catch;
如果[20,無窮),則直接終止數據庫連接;
第三個參數:state
如果在多個位置引發相同的用戶定義錯誤,
則針對每個位置使用唯一的狀態號有助於找到引發錯誤的代碼段。
介於 1 至 127 之間的任意整數。(state 默認值為1)
當state 值為 0 或大於 127 時會生成錯誤!
第四個參數:argument
用於代替 msg_str 或對應於 msg_id 的消息中的定義的變量的參數。
第五個參數:option
錯誤的自定義選項,可以是下表中的任一值:
LOG :在錯誤日志和應用程序日志中記錄錯誤;
NOWAIT:將消息立即發送給客戶端;
SETERROR:將 @@ERROR 值和 ERROR_NUMBER 值設置為 msg_id 或 50000;
msg_id:表示可以是一個sys.messages表中定義的消息代號;
使用 sp_addmessage 存儲在 sys.messages 目錄視圖中的用戶定義錯誤消息號。
用戶定義錯誤消息的錯誤號應當大於 50000。
msg_str:表示也可以是一個用戶定義消息,該錯誤消息最長可以有 2047 個字符;
(如果是常量,請使用N'xxxx',因為是nvarchar的)
當指定 msg_str 時,RAISERROR 將引發一個錯誤號為 5000 的錯誤消息。
@local_variable:表示也可以是按照 msg_str 方式的格式化字符串變量。
第二個參數:severity
用戶定義的與該消息關聯的嚴重級別。(這個很重要)
任何用戶都可以指定 0 到 18 之間的嚴重級別。
[0,10]的閉區間內,不會跳到catch;
如果是[11,19],則跳到catch;
如果[20,無窮),則直接終止數據庫連接;
第三個參數:state
如果在多個位置引發相同的用戶定義錯誤,
則針對每個位置使用唯一的狀態號有助於找到引發錯誤的代碼段。
介於 1 至 127 之間的任意整數。(state 默認值為1)
當state 值為 0 或大於 127 時會生成錯誤!
第四個參數:argument
用於代替 msg_str 或對應於 msg_id 的消息中的定義的變量的參數。
第五個參數:option
錯誤的自定義選項,可以是下表中的任一值:
LOG :在錯誤日志和應用程序日志中記錄錯誤;
NOWAIT:將消息立即發送給客戶端;
SETERROR:將 @@ERROR 值和 ERROR_NUMBER 值設置為 msg_id 或 50000;
[SQL]代碼示例
--示例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 )
SET @raiseErrorCode = CONVERT ( nvarchar ( 50 ), YOUR UNIQUEIDENTIFIER KEY )
RAISERROR ( ' %s INVALID ID. There is no record in table ' , 16 , 1 , @raiseErrorCode )
--示例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
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
--示例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
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
--示例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
10 , -- Severity,
1 , -- State,
N ' abcde ' ); -- First argument supplies the string.
-- The message text returned is: << abc>>.
GO
--示例5
--A. 從 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;
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;
--示例6
--B. 在 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
@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
--示例7
--C. 使用局部變量提供消息文本
以下代碼示例顯示如何使用局部變量為 RAISERROR 語句提供消息文本。
--C. 使用局部變量提供消息文本
以下代碼示例顯示如何使用局部變量為 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
@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