Reputation: 9567
Imagine I have a table Employee
with columns id, name, company
and a table EmployeeErrorLog
with the same structure. The column names of the table Employee
will have a unique constraint on the column name. I want to run inserts in the table Employee
using SQL. The question is if there is an error triggered during an insert in table Employee
, is it possible to make the insert in the table EmployeeErrorLog
?
Upvotes: 0
Views: 57
Reputation: 592
I would look into declare handlers for MySql they basically serve the same purpose as try and catch block.
For more information on these you can check them out here.
http://dev.mysql.com/doc/refman/5.1/en/declare-handler.html
Here is an example of how handlers are used
DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET @x2 = 1;
-> SET @x = 1;
-> INSERT INTO test.t VALUES (1);
-> SET @x = 2;
-> INSERT INTO test.t VALUES (1);
-> SET @x = 3;
Upvotes: 2