2012-12-06 6 views
0

applications 테이블과 application_history 테이블이 있습니다. 나는 응용 프로그램의 이력을 유지하고 행이 applications 테이블에서 업데이트되어 해당 행을 실제로 업데이트되기 전에 application_history으로 복사 할 때마다 트리거를 사용할 생각을 갖고 있습니다.테이블 A에서 행을 업데이트하기 전에 테이블 B에 행을 삽입하십시오.

는 순간, 나는 또 다른 SO 게시물에서이 코드를 기입 한 :

create or replace 
trigger APPLICATION_UPDATE_TRG 
BEFORE UPDATE ON TBL_APPLICATIONS 
FOR EACH ROW 
DECLARE 
    CURSOR curAppHistory IS 
    SELECT record_number, job_id, submitted_date, status_id, id 
     FROM tbl_application 
     WHERE id = :old.id; 
    vRowAppHistory curAppHistory%ROWTYPE; 
BEGIN 
    OPEN curAppHistory; 
    FETCH curAppHistory INTO vRowAppHistory; 
    CLOSE curAppHistory; 
    INSERT INTO tbl_application_history 
    (record_number, job_id, submitted_date, status_id, application_id) 
    VALUES (vRowAppHistory.record_number, vRowAppHistory.job_id, vRowAppHistory.submitted_date, 
    vRowAppHistory.status_id, vRowAppHistory.id); 
END; 

그러나, 제대로 컴파일 아닙니다. SQL Developer는 명령이 제대로 종료되지 않고 명령문이 무시된다는 3 가지 오류를 발생시킵니다.

이 작업을 수행하는 올바른 방법은 무엇입니까?

편집 : 오류 :

Error(2,10): PLS-00341: declaration of cursor 'CURAPPHISTORY' is incomplete or malformed 
Error(3,5): PL/SQL: SQL Statement ignored 
Error(4,12): PL/SQL: ORA-00942: table or view does not exist 
Error(6,18): PL/SQL: Item ignored 
Error(9,3): PL/SQL: SQL Statement ignored 
Error(9,28): PLS-00320: the declaration of the type of this expression is incomplete or malformed 
Error(11,3): PL/SQL: SQL Statement ignored 
Error(14,29): PLS-00320: the declaration of the type of this expression is incomplete or malformed 
Error(14,44): PL/SQL: ORA-00984: column not allowed here 
+0

있습니다

INSERT INTO tbl_application_history (record_number, job_id, submitted_date, status_id, application_id) VALUES (:old.record_number, :old.job_id, :old.submitted_date, :old.status_id, :old.id); 

전체 트리거가이 경우에 하나의 삽입 문이 될 것입니다 너 사용하고있어? 예 : MySQL, Oracle, Microsoft – redolent

+0

또한 오류의 일부를 게시 할 수 있습니까? – redolent

+0

오라클 SQL과 오류를 사용하여 업데이트로 게시합니다. –

답변

2

Error(4,12): PL/SQL: ORA-00942: table or view does not exist

오타? 여기에 두 개의 다른 테이블을 지정했습니다. 어쨌든

FROM tbl_application

BEFORE UPDATE ON TBL_APPLICATIONS

,이 트리거 ORA-04091를 얻을 수 있습니다. 비슷한 테스트 케이스 :

[email protected]> create table t (key number primary key, value varchar2(10)); 

Table created. 

[email protected]> insert into t values (1, 'abcdef'); 

1 row created. 

[email protected]> insert into t values (2, 'ghijkl'); 

1 row created. 

[email protected]> commit; 

Commit complete 

[email protected]> ed 
Wrote file S:\\tools\buffer.sql 

    1 create or replace trigger tt 
    2 before update on t for each row 
    3 declare 
    4 cursor c is 
    5 select key, value 
    6  from t 
    7  where key = :old.key; 
    8 v c%rowtype; 
    9 begin 
10 open c; 
11 fetch c into v; 
12 close c; 
13 dbms_output.put_line(v.value); 
14* end; 
09:58:51 [email protected]>/

Trigger created. 


[email protected]> update t set value = '123'; 
update t set value = '123' 
     * 
ERROR at line 1: 
ORA-04091: table SYSTEM.T is mutating, trigger/function may not see it 
ORA-06512: at "SYSTEM.TT", line 3 
ORA-06512: at "SYSTEM.TT", line 8 
ORA-04088: error during execution of trigger 'SYSTEM.TT' 

이 같은 그것을 아마해야합니다 : SQL 어떤 맛

[email protected]> create table t_log (key number, value varchar2(10)); 

Table created. 

[email protected]> ed 
Wrote file S:\\tools\buffer.sql 

    1 create or replace trigger tt 
    2 before update on t for each row 
    3 begin 
    4 insert into t_log values (:old.key, :old.value); 
    5* end; 
[email protected]>/

Trigger created. 

[email protected]> update t set value = '123'; 

2 rows updated. 

[email protected]> commit; 

Commit complete. 

[email protected]> select * from t_log; 

     KEY VALUE 
---------- ---------- 
     1 abcdef 
     2 ghijkl 


[email protected]> select * from t; 

     KEY VALUE 
---------- ---------- 
     1 123 
     2 123 
+0

완벽하게 컴파일되지만 여전히주의해야 할 ORA-04091 오류가 발생합니다. –

+0

@JamesDawson 나는 커서를 열어서 테이블을 참조하여 업데이트하지 말아야한다는 것을 암시했다. 모든 트리거는 기본적으로 단일 삽입 문이어야합니다. OPEN curAppHistory; 등을 트리거에서 제거 했습니까? –

+0

@JamesDawson이 내 업데이트 된 답변을 확인하십시오. –

관련 문제