0

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?

3
  • 5
    Which SQL tech are you using? Different engines have different capabilities. Commented Jul 22, 2014 at 13:42
  • I use mySQL if this is what you mean Commented Jul 22, 2014 at 13:44
  • 1
    Yes, MySQL is what I was referring to. Not exactly what you're looking for, but MySQL has INSERT ON DUPLICATE KEY UPDATE syntax you might be interested in. It lets you silently (without errors, that is) update an existing entry if you try to insert it again. Commented Jul 22, 2014 at 13:53

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

mySql doesn't have try/catch at least not as far as I know but if you check out my edit answer it should get you the same result.
@ZeRaTuL_jF You should show an appropriated example instead of providing just a link. Links may break here your answer will be saved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.