2016-11-03 100 views
0

當一行更新觸發器將更改該行時,我創建了一個觸發器。當我更新一列時,我收到一個錯誤。我該如何解決?Postgresql觸發器示例

ERROR: stack depth limit exceeded HINT: Increase the configuration parameter "max_stack_depth" (currently 2048kB), after ensuring the platform's stack depth limit is adequate.

CREATE OR REPLACE FUNCTION ynt_call() 
RETURNS TRIGGER AS $$ 
BEGIN 
    IF TG_OP = 'UPDATE' THEN 
     UPDATE ynt.a_test SET date_time = now(); 
    END IF; 
    RETURN NEW; 
END; 
$$ language 'plpgsql'; 
CREATE TRIGGER update_call AFTER UPDATE ON ynt.a_test FOR EACH ROW EXECUTE PROCEDURE ynt_call(); 
+0

看起來您處於無限循環狀態。要更改觸發器中的值,請使用** new **和** old **關鍵字。 – ChrisB

+0

您應該仔細查看手冊中的示例:https://www.postgresql.org/docs/current/static/plpgsql-trigger.html#PLPGSQL-TRIGGER-EXAMPLE –

回答

1

,當然你會循環,直到時間的盡頭,因爲你正在更新行時更新一行。您應該使用特殊變量new設置字段date_time,如下所示:

CREATE OR REPLACE FUNCTION ynt_call() 
RETURNS TRIGGER AS $$ 
BEGIN 
    IF TG_OP = 'UPDATE' THEN 
     new.date_time = now(); 
    END IF; 
    RETURN NEW; 
END; 
$$ language 'plpgsql'; 
CREATE TRIGGER update_call AFTER UPDATE ON ynt.a_test FOR EACH ROW EXECUTE  PROCEDURE ynt_call();