2014-03-24 52 views
0

如果不存在,我必須編寫觸發器以將數據插入到數據庫中。 我寫了,直到插入記錄。我正在做信號,如果數據存在哪些 是失敗我的整批記錄。任何關鍵字跳過DB2觸發器中的當前行

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 

回答

0

您不需要爲此使用觸發器。只需在兩個字段中添加唯一索引:

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

更簡單的防止重複的解決方案。

+0

非常感謝Gordon !!!它爲我工作。還有,是否有任何選項可以跟蹤被拒絕的重複條目。請建議我... – Naresh

+0

@ user3454735。 。 。我其實不知道。許多數據庫都包含「INSERT」的日誌記錄或「返回」功能,但我不確定DB2如何執行此操作。 –