2012-10-05 40 views
0

希望有人能幫助解決這個問題。我是觸發器的新手,我試圖創建一個觸發器來檢查被修改的記錄是否具有特定的值。如何在同一張表上查詢和執行觸發器

示例 我有一個表稱爲歸檔具有filing_id和filing_status,我想阻止某人更新或刪除該表中的任何記錄具有filing_status =「FILED」。 所以,如果我有以下

Filing_id Filing_status Val 
--------- ------------- --- 
0    Filed   X 

如果有人試圖修改瓦爾觸發器應停止 我創建了以下觸發:

CREATE or replace TRIGGER TRG_PREV_FILING 
    BEFORE DELETE or UPDATE 
    ON PF.FILING 
    FOR EACH ROW 
    declare 
    rowcnt number; 
    BEGIN 
    SELECT COUNT(filing_id) INTO rowcnt FROM PF.FILING 
     where status = 'FILED' 
     and filing_id = :new.filing_id; 
    if (rowcnt > 0) 
    then 
     raise_application_error (-20100, 'You can not delete Or Update initial record'); 
    end if; 
    END; 

我面對的是我得到的問題: ORA-04091這是「表格歸檔突變,觸發器/函數可能看不到它」

所以基本上我不能查詢在同一張表上,我正在執行觸發器?這是我的情況的問題,有沒有人知道這方面的工作?

我感謝所有幫助

回答

3

您不必查詢表觸發射擊上能夠做這種檢查。您可以使用:old獲得正在修改的列的值。這裏有一個例子:

SQL> create table filing(
    2 status varchar2(31), 
    3 val number 
    4 ); 

Table created 


SQL> create or replace trigger TRG_FILING before delete or update on FILING 
    2 for each row 
    3 begin 
    4 if lower(:old.status) = 'filed' 
    5 then 
    6  raise_application_error(-20000, 'You cannot delete or modify this record'); 
    7 end if; 
    8 end; 

SQL>/

Trigger created 


SQL> insert into FILING values('FILED', null); 

1 row inserted 

SQL> insert into filing values('OK', 1); 

1 row inserted 

SQL> commit; 

Commit complete 

SQL> select * 
    2 from filing; 

STATUS         VAL 
------------------------------- ---------- 
FILED       
OK          1 

SQL> delete 
    2 from filing 
    3 where val is null; 


ORA-20000: You cannot delete or modify this record 
ORA-06512: at "HR.TRG_FILING", line 4 
ORA-04088: error during execution of trigger 'HR.TRG_FILING' 
+0

非常感謝。我必須學習更多關於觸發器的知識。 – user955165

0

最基本的一點是,你應該設計你的數據庫的方式,在不觸發基於更新/刪除的行了驗證。如果你有幾行同一個filing_id,那麼你可以過度勞累你的數據庫設計。也許你真的只是檢查自己的桌子,在這種情況下,你可以使用:old。但是,當你有幾行檢查(我假設你是做一個計數),那麼你必須使用兩個表。這是一個建議。

create table filing_status (filing_id number, status varchar2(10)); 
create table filing_content (filing_id number, content_id number, content varchar2(200)); 
CREATE or replace TRIGGER TRG_PREV_FILING 
    BEFORE DELETE or UPDATE 
    ON FILING_content 
    FOR EACH ROW 
    declare 
    rowcnt number; 
    BEGIN 
    SELECT COUNT(filing_id) INTO rowcnt FROM FILING_status 
     where status = 'FILED' 
     and filing_id = :new.filing_id; 
    if (rowcnt > 0) 
    then 
     raise_application_error (-20100, 'You can not delete Or update filed record'); 
    end if; 
    END; 
/
insert into filing_status values (1, 'FILING'); 
insert into filing_content values (1, 1, 'foo'); 
insert into filing_content values (1, 2, 'bar'); 
insert into filing_status values (1, 'FILED'); 
update filing_content set content = 'bahr' where filing_id = 1 and content_id = 2; 

ERROR at line 1: 
ORA-20100: You can not delete Or update filed record 
ORA-06512: at "DEMO.TRG_PREV_FILING", line 9 
ORA-04088: error during execution of trigger 'DEMO.TRG_PREV_FILING' 
相關問題