2011-12-20 130 views
2

我想寫一個觸發器就像下面這個簡單的例子:如何在informix中創建觸發器?

create trigger adr_trg update of fname, lname on adr 
REFERENCING OLD AS o NEW AS n 
FOR EACH ROW 
(
    IF o.fname <> n.fname THEN 
    insert into adrlog (old_value, new_value) 
    values (o.fname, n.fname); 
    END IF; 

    IF o.lname <> n.lname THEN 
    insert into adrlog (old_value, new_value) 
    values (o.lname, n.lname); 
    END IF; 
) 

這種失敗!

只有這樣工作的:

create trigger adr_trg update of fname, lname on adr 
REFERENCING OLD AS o NEW AS n 
FOR EACH ROW 
(
    insert into adrlog (old_value, new_value) 
    values (o.fname, n.fname); 
) 

我在做什麼錯?

回答

3

我想你應該用IF-THEN邏輯編寫一個存儲過程,並從這個觸發器中調用它。我認爲觸發器僅支持簡單的SQL語句,而不是SPL語句

編輯:

CREATE PROCEDURE proc1() 
REFERENCING OLD AS o NEW AS n FOR tab1; 
....your logic 


CREATE TRIGGER adr_trg update of fname, lname on adr 
REFERENCING OLD AS o NEW AS n 
    FOR EACH ROW(EXECUTE PROCEDURE proc1() WITH TRIGGER REFERENCES); 
+0

是啊,我想你是對的。可悲的是,Informix s * cks ... – PeterP

+0

一個好的匠人從來不會責怪他的工具。在我看來,你所需要的條件邏輯最好放在一個過程中。它可能被證明具有足夠的通用性,可用於多個表格,以OO方式使用。你的簡單例子當然表明這一點。 – RET

+0

[參考,這裏是相關的手冊頁](http://publib.boulder.ibm.com/infocenter/idshelp/v111/index.jsp?topic=/com.ibm.sqls.doc/sqls521.htm) –

2

老問題,但沒有得到很好的回答,萬一有人在這裏像我一樣的土地了。

Informix的處理請求的功能,但語法是不同的:

create trigger adr_trg update of fname,lname on adr 
referencing old as o new as n 
    for each row 
    when (o.fname != n.fname) 
    (
     insert into adrlog (old_value,new_value) 
     values (o.fname,n.fname) 
    ) , 
    when (o.lname != n.lname) 
    (
     insert into adrlog (old_value,new_value) 
     values (o.lname,n.lname) 
    ) 
;