如何在MySQL存储过程中发出警告?

时间:2017-04-03 18:11:34

标签: mysql stored-procedures signals warnings

目前我有退出处理程序:

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;

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

1 个答案:

答案 0 :(得分:2)

以下过程表示错误或警告,具体取决于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;