Stored-Procedures

聲明處理程序時的邏輯條件

  • May 25, 2016

我在 MySQL 中有一個過程,它執行一個處理程序,SQLEXCEPTION用於讀取 errno 程式碼並將其插入到表中。

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
BEGIN
GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE,
@errno = MYSQL_ERRNO, @text = MESSAGE_TEXT;

我期待這樣的事情

DECLARE EXIT HANDLER FOR SQLEXCEPTION OR SQLWARNING
BEGIN
GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE,
@errno = MYSQL_ERRNO, @text = MESSAGE_TEXT;

是否有機會在 sql 處理程序中使用OR或邏輯運算符?AND

或者,我可以得到一些建議以在一行中獲取WARNINGERROR消息DECLARE嗎?

根據 Mysql 文件https://dev.mysql.com/doc/refman/5.6/en/handler-scope.html您可以創建一個具有兩個處理程序和相同範圍的處理程序,請注意優先級

例子

CREATE PROCEDURE p1()
BEGIN
 DECLARE CONTINUE HANDLER FOR SQLSTATE '42S02'
   SELECT 'SQLSTATE handler was activated' AS msg;
 DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
   SELECT 'SQLEXCEPTION handler was activated' AS msg;

 DROP TABLE test.t;
END;

引用自:https://dba.stackexchange.com/questions/138945