如何在触发器中使用复合主键(Oracle DB)

时间:2013-04-03 05:12:06

标签: oracle plsql triggers

我创建了下表

创建表格标签(id NUMBER,val VARCHAR2(50),last_date DATE,PRIMARY KEY(id,val))

我正在使用以下PL / SQL块来创建触发器

create or replace TRIGGER TRIG AFTER INSERT OR UPDATE OR DELETE ON tab 
    FOR EACH ROW 
    DECLARE
           l_enqueue_options DBMS_AQ.enqueue_options_t;
           l_message_properties DBMS_AQ.message_properties_t;
           l_txn_event_msg tab_PAYLOAD;
           l_message_handle RAW(16);
           ERR_MSG VARCHAR2(600);
           primaryKey VARCHAR2(4000);
           opnCode VARCHAR2(10);
    BEGIN
           IF INSERTING THEN
               opnCode := 'INSERT';
               primaryKey := :new.id;
           ELSIF UPDATING THEN
               opnCode := 'UPDATE';
               primaryKey := :new.id;
           ELSIF DELETING THEN
               opnCode := 'DELETE';
               primaryKey := :old.id;
           END IF;

           l_txn_event_msg := tab_PAYLOAD(
                primaryKey, opnCode, sysdate,:new.id,'DUMMY'
           );

           DBMS_AQ.enqueue(
              queue_name => 'tab_Q',
              enqueue_options => l_enqueue_options,
              message_properties => l_message_properties,
              payload => l_txn_event_msg,
              msgid => l_message_handle
           );

      EXCEPTION
           WHEN NO_DATA_FOUND THEN NULL;
           WHEN OTHERS THEN
                ERR_MSG := sqlerrm || ' Error while executing trigger TRIG_UB.';
                DBMS_OUTPUT.PUT_LINE('Some exception occured in tab ==>' || ERR_MSG);
      END;

现在上面的触发器是否正确?我想在触发器中使用复合主键,类似于primaryKey:=:new.id ||||| old.id;如果我错了,请纠正我?

0 个答案:

没有答案