Bug #14739 DECLARE Handlers Not Recognized in Triggers
Submitted: 8 Nov 2005 10:55 Modified: 10 Nov 2005 8:53
Reporter: Abraham Kauffman Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:5.0.15 OS:Solaris (Solaris 10)
Assigned to: CPU Architecture:Any

[8 Nov 2005 10:55] Abraham Kauffman
Description:
I have a front end srcipt that dynamically generates the trigger statement, depending on the existance of a varying number of tables/columns for any particular time. The data structure changes frequently, with the addition or deletion of some tables/columns.

This script is very good at dynamically capturing a snapshot of what everything that needs to cascade down to the subtables at the time the trigger is created. Since it is a fluid structure, tables that once existed when the trigger was built, may no longer exist and spring errors, like 1146.

The DECLARE CONTINUE HANDLER for these errors is not being recognized, which would make the trigger much more stable if it worked. The only fully reliable workaround is constantly dropping and recreating the trigger before performing any inserts or updates.

One may wonder why I break the insert/update statements up like that, and while it's irrelevant to the issue, this is the result of server-side scripting evaluating the existance of available fields to update at the time. If we can get the HANDLERS working properly, I intend to handle the exception of any column that may disappear between the time the trigger is created and when it is executed.

This bug report looks very similar to #14197 but mine is for a different OS and I would consider this to be a little more severe than was reported there.

How to repeat:
CREATE TABLE db.parenttable (with three fields: f1,f2,f3);
CREATE TABLE db.subtable2 (with three fields: f1,f2,f3);
CREATE TABLE db.subtable3 (with three fields: f1,f2,f3);

CREATE TRIGGER trg BEFORE INSERT ON parenttable FOR EACH ROW 
BEGIN 
DECLARE CONTINUE HANDLER FOR 1146 SET @x2 = 1; 
SET @x2 = 0; 
INSERT INTO subtable1 SET f1=NEW.f1;
IF @x2 = 0 THEN 
UPDATE subtable1 SET f2=NEW.f2 WHERE f1=NEW.f1; 
UPDATE subtable1 SET f3=NEW.f3 WHERE f1=NEW.f1; 
END IF;
SET @x2 = 0;
INSERT INTO subtable2 SET f1=NEW.f1;
IF @x2 = 0 THEN 
UPDATE subtable2 SET f2=NEW.f2 WHERE f1=NEW.f1;
UPDATE subtable SET f3=NEW.f3 WHERE f1=NEW.f1;
END IF;
END;

DROP TABLE db.subtable1

Suggested fix:
Make DECLARE Handlers work for triggers like they work for Stored Procedures.
[9 Nov 2005 6:03] Abraham Kauffman
I may have overstated the problem. It appears that DECLARE Handler IS working in triggers for some errors like Error: 1054 SQLSTATE: 42S22 (ER_BAD_FIELD_ERROR). Just not others like Error: 1146 SQLSTATE: 42S02 (ER_NO_SUCH_TABLE).
[10 Nov 2005 8:53] Valeriy Kravchuk
Thank you for a bug report. Looks like it is a duplicate of http://bugs.mysql.com/bug.php?id=8407, really. The reason for the bug is the same, at least. I'll add a comment with a link to this report there.