Any keyword to skip the current row in DB2 trigger

2019-09-01 08:46发布

问题:

I have to write a trigger to insert the data into the DB if doesn't exists. I wrote till inserting the records. I am doing signaling if the data is exists which is failing my entire batch of records.

   My questions: Is there any keyword which will skip/ignore/discard the current row? 
   My trigger is below. I want to remove signal and keep something else which will 
   discard/ignore/skip the current row insertion. For example : Continue keyword in JAVA  .

   CREATE OR REPLACE TRIGGER tri_books_edit 
   NO CASCADE BEFORE INSERT ON books 
   REFERENCING NEW AS N
   FOR EACH ROW 
   WHEN ((select count(*) from books where book_name = N.book_name and author = N.Author) > 0)
   SIGNAL SQLSTATE '75000' SET MESSAGE_TEXT = 'Duplicate row with same name and author' 

   Thanks for your help

回答1:

You don't need to use a trigger for this. Just add a unique index on the two fields:

create unique index books_name_author on books(book_name, author);

Much simpler solution for preventing duplicates.



标签: sql database db2