目前我有退出处理程序:

DECLARE c_invalidParam CONDITION FOR SQLSTATE '48000';
DECLARE EXIT HANDLER FOR c_invalidParam
BEGIN
    ROLLBACK;
    SELECT 'I' INTO out_resultCode;
    SELECT '0' INTO out_accountId;
    RESIGNAL;
END;

IF in_username = ''
THEN
    SIGNAL c_invalidParam
        SET MESSAGE_TEXT = 'sp_test: username may not be empty';
END IF;

这使得我的存储过程退出。如何发出警告并退出存储过程?

最佳答案

以下过程根据输入参数pval的值发出错误或警告信号:

CREATE PROCEDURE p (pval INT)
BEGIN
  DECLARE specialty CONDITION FOR SQLSTATE '45000';
  IF pval = 0 THEN
    SIGNAL SQLSTATE '01000';
  ELSEIF pval = 1 THEN
    SIGNAL SQLSTATE '45000'
      SET MESSAGE_TEXT = 'An error occurred';
  ELSEIF pval = 2 THEN
    SIGNAL specialty
      SET MESSAGE_TEXT = 'An error occurred';
  ELSE
    SIGNAL SQLSTATE '01000'
      SET MESSAGE_TEXT = 'A warning occurred', MYSQL_ERRNO = 1000;
    SIGNAL SQLSTATE '45000'
      SET MESSAGE_TEXT = 'An error occurred', MYSQL_ERRNO = 1001;
  END IF;
END;

关于mysql - 如何在MySQL存储过程中发出警告?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43191540/

10-13 01:33