DROP PROCEDURE IF EXISTS decount_test;
DELIMITER //
CREATE DEFINER = 'root'@'localhost' PROCEDURE decount_test ( p_id bigint )
DETERMINISTIC MODIFIES SQL DATA
BEGIN
DECLARE EXIT HANDLER FOR SQLSTATE '42000'
SELECT 'Invoiced barcodes may not have accounting removed.';
IF (SELECT invoice_id
FROM accounted_barcodes
WHERE id = p_id
) THEN
CALL raise_error;
END IF;
DELETE FROM accounted_barcodes WHERE id = p_id;
END //
DELIMITER ;
产出:
call decount_test(123456);
+----------------------------------------------------+
| Invoiced barcodes may not have accounting removed. |
+----------------------------------------------------+
| Invoiced barcodes may not have accounting removed. |
+----------------------------------------------------+
DELIMITER $$ DROP FUNCTION IF EXISTS `raise_error` $$
CREATE FUNCTION `raise_error`(MESSAGE VARCHAR(255))
RETURNS INTEGER DETERMINISTIC BEGIN
DECLARE ERROR INTEGER;
set ERROR := MESSAGE;
RETURN 0;
END $$ DELIMITER ;
-- set @foo := raise_error('something failed'); -- or within a query