(First post. Apologize if I breach any customs or protocol.)
I want to define an exit handler for any SQLWARNING thrown in my stored procedure. But the handler is being ignored, it never fires. And I am very confused.
I am using MariaDB v. 10.0.23. However I have tested this on MariaDB v. 10.1.14 and get the same result.
Below is the SQL to create a test database.
CREATE DATABASE testerrorhandling;
USE testerrorhandling;
CREATE TABLE test
(
int_notnull INT NOT NULL
);
DELIMITER //
CREATE DEFINER=CURRENT_USER PROCEDURE create_record
(
IN p INT
)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
SELECT 'Handler for SQLEXCEPTION fired.';
DECLARE EXIT HANDLER FOR SQLWARNING
SELECT 'Handler for SQLWARNING fired.';
INSERT INTO test (int_notnull) VALUES (p);
END;
//
DELIMITER ;
If I attempt to add a null value, the procedure invokes the correct handler.
MariaDB [testerrorhandling]> call create_record(null);
+---------------------------------+
| Handler for SQLEXCEPTION fired. |
+---------------------------------+
| Handler for SQLEXCEPTION fired. |
+---------------------------------+
1 row in set (0.00 sec)
But now if I attempt to add a non-integer value, a warning is thrown but the SQLWARNING error handler never fires. A value of 0 is stored in the test table.
MariaDB [testerrorhandling]> call create_record('this is not an integer');
Query OK, 1 row affected, 1 warning (0.04 sec)
MariaDB [testerrorhandling]> show warnings;
+---------+------+---------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: 'this is not an integer' for column 'p' at row 1 |
+---------+------+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
This is not what I expect or desire. I have to be doing something wrong. What am I missing here?
The warning occurs in the stored procedure call, not within the stored procedure, so the warning (within stored procedure) is not fired.
In the following example you can see that the parameter p has a value of zero (0) within the stored procedure, so there is no warning:
MariaDB [_]> DROP TABLE IF EXISTS test;
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DROP PROCEDURE IF EXISTS create_record;
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> CREATE TABLE IF NOT EXISTS test (
-> int_notnull INT NOT NULL
-> );
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER //
MariaDB [_]> CREATE PROCEDURE create_record (
-> IN p INT
-> )
-> BEGIN
-> DECLARE EXIT HANDLER FOR SQLEXCEPTION
-> SELECT 'Handler for SQLEXCEPTION fired.';
->
-> DECLARE EXIT HANDLER FOR SQLWARNING
-> SELECT 'Handler for SQLWARNING fired.';
->
-> SELECT CONCAT('VALUE OF p: ', p);
-> INSERT INTO test (int_notnull) VALUES (p);
-> END//
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER ;
MariaDB [_]> call create_record('this is not an integer');
+---------------------------+
| CONCAT('VALUE OF p: ', p) |
+---------------------------+
| VALUE OF p: 0 |
+---------------------------+
1 row in set (0.00 sec)
Query OK, 1 row affected, 1 warning (0.00 sec)
MariaDB [_]> SHOW WARNINGS;
+---------+------+---------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: 'this is not an integer' for column 'p' at row 1 |
+---------+------+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
In the following example, the warning is fired in the call to and within the stored procedure:
MariaDB [_]> DROP TABLE IF EXISTS test;
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DROP PROCEDURE IF EXISTS create_record;
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> CREATE TABLE IF NOT EXISTS test (
-> int_notnull INT NOT NULL
-> );
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER //
MariaDB [_]> CREATE PROCEDURE create_record (
-> IN p INT
-> )
-> BEGIN
-> DECLARE EXIT HANDLER FOR SQLEXCEPTION
-> SELECT 'Handler for SQLEXCEPTION fired.';
->
-> DECLARE EXIT HANDLER FOR SQLWARNING
-> SELECT 'Handler for SQLWARNING fired.';
->
-> SELECT CONCAT('VALUE OF p: ', p);
-> SET p := 'this is not an integer';
-> INSERT INTO test (int_notnull) VALUES (p);
-> END//
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER ;
MariaDB [_]> call create_record('this is not an integer');
+---------------------------+
| CONCAT('VALUE OF p: ', p) |
+---------------------------+
| VALUE OF p: 0 |
+---------------------------+
1 row in set (0.00 sec)
+-------------------------------+
| Handler for SQLWARNING fired. |
+-------------------------------+
| Handler for SQLWARNING fired. |
+-------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaDB [_]> SHOW WARNINGS;
+---------+------+---------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: 'this is not an integer' for column 'p' at row 1 |
+---------+------+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
Related
I'm trying to call a procedure inside procedure, however the calling procedure is in a different db schema so it's a variable and isn't translating.
DECLARE client_database varchar(255);
SET client_database = get_database(_client_id); --this gets the schema the procedure is in
CALL client_database.client_procedure(); --then trying to call the procedure with the db schema variable however it's not translating
Question: Why isn't client_database translating?
One option is to use a 13.5 Prepared SQL Statement Syntax:
mysql> DROP PROCEDURE IF EXISTS `client_procedure`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP PROCEDURE IF EXISTS `server_procedure`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP DATABASE IF EXISTS `client_db`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP DATABASE IF EXISTS `server_db`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CREATE DATABASE IF NOT EXISTS `client_db`;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE DATABASE IF NOT EXISTS `server_db`;
Query OK, 1 row affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `client_db`.`client_procedure`()
-> BEGIN
-> SELECT CONCAT('FROM `', DATABASE(), '`');
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE PROCEDURE `server_db`.`controller_procedure`()
-> BEGIN
-> DECLARE `client_database` VARCHAR(64);
->
-> -- This gets the schema the procedure is in
-> -- SET `client_database` := `get_database`(`client_id`);
-> SET `client_database` := 'client_db';
->
-> SET #`call` := CONCAT('CALL `', `client_database`, '`.`client_procedure`');
-> PREPARE `stmt` FROM #`call`;
-> EXECUTE `stmt`;
-> DEALLOCATE PREPARE `stmt`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `server_db`.`controller_procedure`;
+-----------------------------------+
| CONCAT('FROM `', DATABASE(), '`') |
+-----------------------------------+
| FROM `client_db` |
+-----------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
See db-fiddle.
CREATE DEFINER=`root`#`localhost` PROCEDURE `PrcCopyQuestion_Admin`(in Param1,in Param2 varchar(45))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
Select 'Fail' as 'Status' ;
END;
DECLARE EXIT HANDLER FOR sqlwarning
BEGIN
ROLLBACK;
Select 'Fail' as 'Status' ;
END;
Start transaction;
Insert statement 1;
Insert statement 2;
SELECT 'Success' AS 'Status';
call PrcGetQuestionAndOption_Admin(#variable);
Commit;
END
I am using Mysql 5.7. When in the commit block if the second (Insert statement 2) fails. It will go in the Rollback part and gives me output as 'Failed'. But when i am getting the output it still executes the Select 'Success' as Status in commit block.
So my question is when the second insert statement fails. It should go directly in rollback and give me status as fail. It should not execute the status as 'success' in commit block.
Eg: On rollback I am getting two result set:
Select 'Fail'..1st result set
Select 'Success'....2nd result set
I need output as only
Select 'fail'
Any help appreciated!!
I can't reproduce the problem.
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.12 |
+-----------+
1 row in set (0.00 sec)
mysql> DROP TABLE IF EXISTS `t1`, `t2`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `t1` (
-> `c0` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `t2` (
-> `c0` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER //
mysql> DROP PROCEDURE IF EXISTS `PrcCopyQuestion_Admin`//
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE PROCEDURE `PrcCopyQuestion_Admin`(Param1 INT, Param2 VARCHAR(45))
-> BEGIN
-> DECLARE EXIT HANDLER FOR SQLEXCEPTION
-> BEGIN
-> ROLLBACK;
-> SELECT 'Fail' Status;
-> END;
-> DECLARE EXIT HANDLER FOR SQLWARNING
-> BEGIN
-> ROLLBACK;
-> SELECT 'Fail' Status;
-> END;
-> START TRANSACTION;
-> INSERT INTO `t1` (`c0`) VALUES (0);
-> IF Param1 = 0 THEN
-> INSERT INTO `ERR_t2` (`c0`) VALUES (0);
-> ELSE
-> INSERT INTO `t2` (`c0`) VALUES (0);
-> END IF;
-> SELECT 'Success' Status;
-> COMMIT;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `PrcCopyQuestion_Admin`(0, NULL);
+--------+
| Status |
+--------+
| Fail |
+--------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT
-> `c0`
-> FROM
-> `t1`;
Empty set (0.00 sec)
mysql> SELECT
-> `c0`
-> FROM
-> `t2`;
Empty set (0.00 sec)
mysql> CALL `PrcCopyQuestion_Admin`(1, NULL);
+---------+
| Status |
+---------+
| Success |
+---------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT
-> `c0`
-> FROM
-> `t1`;
+----+
| c0 |
+----+
| 2 |
+----+
1 row in set (0.00 sec)
mysql> SELECT
-> `c0`
-> FROM
-> `t2`;
+----+
| c0 |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
I have a problem with this trigger. When I use the INSERT and the SIGNAL statements inside the IF condition, the insertion is not performed. However, without SIGNAL, the insertion is done. Anyone have an explanation for that? My main concern is that I need both the insertion and the SIGNAL statement to cancel the main insertion (the insertion that throws the trigger)
DELIMITER //
CREATE TRIGGER log_venta BEFORE INSERT ON `venta_producto`
FOR EACH ROW BEGIN
DECLARE value int;
DECLARE valor_venta int;
DECLARE saldo_cliente int;
DECLARE cliente_id int;
SELECT cliente INTO cliente_id FROM venta WHERE id=NEW.ventaID;
SELECT credito INTO saldo_cliente FROM cliente WHERE no_cliente=cliente_id;
SELECT precio INTO valor_producto FROM producto WHERE id=NEW.producto;
SET valor_venta = NEW.cantidad*valor_producto;
IF valor_venta > saldo_cliente THEN
INSERT INTO log(cliente) VALUES (cliente_id);
SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'ERROR';
END IF;
END
//
DELIMITER ;
Thanks
14.6.7.5 SIGNAL Syntax
. . .
SIGNAL is the
way to “return” an error.
. . .
21.3.1 Trigger Syntax and Examples
. . .
For transactional tables, failure of a statement should cause rollback of all changes performed by the statement. Failure of a
trigger causes the statement to fail, so trigger failure also causes
rollback. For nontransactional tables, such rollback cannot be done,
so although the statement fails, any changes performed prior to the
point of the error remain in effect.
. . .
Mentioned above can be demonstrated in the following example:
mysql> DROP TABLE IF EXISTS `venta_producto`;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TABLE IF EXISTS `log`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `venta_producto` (
-> `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
-> `cliente_id` INT UNSIGNED
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE IF NOT EXISTS `log` (
-> `cliente_id` INT UNSIGNED
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE TRIGGER `log_venta` BEFORE INSERT ON `venta_producto`
-> FOR EACH ROW
-> BEGIN
-> INSERT INTO `log` (`cliente_id`) VALUES (NEW.`cliente_id`);
-> SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'ERROR';
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> INSERT INTO `venta_producto`
-> (`cliente_id`)
-> VALUES
-> (1);
ERROR 1643 (02000): ERROR
mysql> SELECT
-> `id`,
-> `cliente_id`
-> FROM
-> `venta_producto`;
Empty set (0.00 sec)
mysql> SELECT
-> `cliente_id`
-> FROM
-> `log`;
Empty set (0.00 sec)
mysql> ALTER TABLE `log` ENGINE=MyISAM;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> INSERT INTO `venta_producto`
-> (`cliente_id`)
-> VALUES
-> (2);
ERROR 1643 (02000): ERROR
mysql> SELECT
-> `id`,
-> `cliente_id`
-> FROM
-> `venta_producto`;
Empty set (0.00 sec)
mysql> SELECT
-> `cliente_id`
-> FROM
-> `log`;
+------------+
| cliente_id |
+------------+
| 2 |
+------------+
1 row in set (0.00 sec)
I have a stored procedure which will set my customized error as well as set an out variable to some value . I tried the following procedure .
***delimiter //
drop procedure if exists test_dalpu //
create procedure test_dalpu(out out_value int)
begin
DECLARE EXIT HANDLER FOR SQLSTATE '22001'
BEGIN
set out_value = 1;
SIGNAL SQLSTATE '22001' SET
MYSQL_ERRNO = 2,
MESSAGE_TEXT = 'Too long ';
END;
insert into abc_test values ( 5,"eerrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");***
end//
delimiter ;
Then after executing
**call test_dalpu(#out_value);**
I got the correct output as Error code 2 Too long as expected, But after that If I do
select #out_value;
Am getting the value as null instead of 1 .
Is it the correct way?
Try:
mysql> DROP PROCEDURE IF EXISTS `test_dalpu`;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TABLE IF EXISTS `abc_test`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `abc_test` (
-> `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
-> `description` CHAR(10)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `test_dalpu`(`out_value` VARCHAR(64))
-> BEGIN
-> DECLARE EXIT HANDLER FOR SQLSTATE '22001'
-> BEGIN
-> ROLLBACK;
-> SET #`set_variable` := CONCAT('SET #`', `out_value`, '` := 1');
-> PREPARE `stmt` FROM #`set_variable`;
-> EXECUTE `stmt`;
-> DEALLOCATE PREPARE `stmt`;
-> SIGNAL SQLSTATE '22001' SET
-> MYSQL_ERRNO = 2,
-> MESSAGE_TEXT = 'Too long';
-> END;
-> START TRANSACTION;
-> INSERT INTO `abc_test`
-> (`description`)
-> VALUES
-> ('ABCDEFGHIJK');
-> COMMIT;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> SET #`out_value` := NULL;
Query OK, 0 rows affected (0.00 sec)
mysql> CALL `test_dalpu`('out_value');
ERROR 2 (22001): Too long
mysql> SHOW WARNINGS;
+-------+------+----------+
| Level | Code | Message |
+-------+------+----------+
| Error | 2 | Too long |
+-------+------+----------+
1 row in set (0.00 sec)
mysql> SELECT #`out_value`;
+--------------+
| #`out_value` |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)
I am unable to run a simple script in MySQL. I have reduced the script to just one line.
DELIMITER $$
DECLARE varLocalityName VARCHAR(50);
$$
DELIMITER ;
The error is:
ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE varLocalityName VARCHAR(50)' at line 1
$ mysql --version
mysql Ver 14.14 Distrib 5.1.63, for debian-linux-gnu (x86_64) using readline 6.2
Your code block does not define the scope for the declared variables. If within a procedure, they must be between BEGIN and END. Without them, the statement DECLARE varLocalityName VARCHAR(50); becomes an invalid statement to be executed. This statement is equivalent to the statements shown below:
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2012-10-22 |
+----------------+
1 row in set (0.00 sec)
mysql> declare varLocalityName varchar(50);
ERROR 1064 (42000): You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near 'declare varLocalityName varchar(50)' at line 1
mysql>
Either you should declare session variables without keyword DECLARE or follow a syntax defined for a stored procedure to use scoped variables.
Example 1: Using session variables:
mysql> set #x = null;
Query OK, 0 rows affected (0.00 sec)
mysql> select #x;
+------+
| #x |
+------+
| NULL |
+------+
1 row in set (0.00 sec)
mysql> delimiter $$
mysql> select current_date() into #x;
-> $$
Query OK, 1 row affected (0.02 sec)
mysql> select #x;
-> $$
+------------+
| #x |
+------------+
| 2012-10-22 |
+------------+
1 row in set (0.00 sec)
Note that you can set/define session variables within a procedure but not DECLARE.
Example 2: Using procedure scoped variables:
mysql>
mysql> delimiter $$
mysql> create procedure some_x()
-> begin
-> declare varLocalityName varchar(50);
->
-> set #sessionDate = null;
-> select #sessionDate;
-> set #sessionDate = current_date();
-> select #sessionDate;
->
-> select varLocalityName;
-> end;
-> $$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql>
mysql> call some_x();
+--------------+
| #sessionDate |
+--------------+
| NULL |
+--------------+
1 row in set (0.00 sec)
+--------------+
| #sessionDate |
+--------------+
| 2012-10-22 |
+--------------+
1 row in set (0.00 sec)
+-----------------+
| varLocalityName |
+-----------------+
| NULL |
+-----------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> select #sessionDate;
+--------------+
| #sessionDate |
+--------------+
| 2012-10-22 |
+--------------+
1 row in set (0.00 sec)
mysql> select varLocalityName;
ERROR 1054 (42S22): Unknown column 'varLocalityName' in 'field list'
mysql>
Also refer to Variables declaration and scope.
I think the problem is that you define $$ as delimiter and then you still try to use ; as the delimiter
I believe this should work:
DELIMITER $$
DECLARE varLocalityName VARCHAR(50)$$
DELIMITER ;
As default MySQL delimiter is ;. You can change it with DELIMITER call and you must use it in the new syntax from then on.
DELIMITER $$
DECLARE varLocalityName VARCHAR(50)$$
SET varLocalityName="xx"$$
SELECT varLocalityName$$
DELIMITER ;
So basically $$ replaces every occurrence of ; (as long as it is not inside the String). If you set DELIMITER **, you would use double stars instead of ;.