Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass the NEW and the OLD tables from a trigger into a procedure in MySQL?

Tags:

mysql

triggers

Is it possible to pass the NEW and the OLD tables from a trigger into a procedure in MySQL? I suspect no, since there is no such a datatype as table that a procedure accepts. Any workarounds possible?

Ideally it would look like this:

CREATE TRIGGER Product_log AFTER UPDATE ON Product
  FOR EACH ROW BEGIN
    call logChanges(OLD, NEW);
  END;
like image 876
user19878 Avatar asked Jun 04 '12 17:06

user19878


1 Answers

You can explicitly pass each field:

CALL logChanges(OLD.colA, OLD.colB, NEW.colA, NEW.colB);

Or if logChanges must be sufficiently generic that it can handle such calls from different tables, one could concatenate the field values into a single string using a suitable delimiter (e.g. the unit separator):

CALL logChanges(CONCAT_WS(CHAR(31), OLD.colA, old.colB),
                CONCAT_WS(CHAR(31), NEW.colA, NEW.colB));

Or if data types must be preserved, one could insert the records into a temporary from which logChanges reads.

like image 132
eggyal Avatar answered Oct 21 '22 08:10

eggyal