记录" old"尚未分配

时间:2016-11-22 21:45:51

标签: sql postgresql

我创建了这个触发器:

CREATE TRIGGER trig_update 
AFTER INSERT
ON "delivery" FOR EACH ROW
EXECUTE PROCEDURE premium1();

使用此功能:

CREATE OR REPLACE FUNCTION premium1() 
RETURNS trigger AS $$
BEGIN
IF (TG_OP='INSERT') THEN
IF(select sum(amount) from delivery where cli_num=OLD.cli_num group by cli_num) > 500.00 THEN
    UPDATE client SET Type ='Gold' WHERE cli_num=OLD.cli_num;
END IF;
END IF;
RETURN NULL;
END;
$$LANGUAGE plpgsql;

在表格传递中插入新值时,我收到以下错误:

  

错误:记录"旧"尚未分配Stan SQL:55000

对此有何帮助?

2 个答案:

答案 0 :(得分:2)

AFTER触发器中OLD无法访问/可见,因为该记录尚未存在于数据库中,因此无法将其与之进行比较。您必须将其更改为NEW

来自fine manual:

  

对于行级触发器,输入数据还包括NEW行   INSERT和UPDATE触发器,和/或UPDATE和DELETE的OLD行   触发器。

答案 1 :(得分:1)

CREATE OR REPLACE FUNCTION premium1() 
RETURNS trigger AS $$
BEGIN
IF (TG_OP='INSERT') THEN
IF(select sum(amount) from delivery where cli_num=NEW.cli_num group by cli_num) > 500.00 THEN
    UPDATE client SET Type ='Gold' WHERE cli_num=NEW.cli_num;
END IF;
END IF;
RETURN NULL;
END;
$$LANGUAGE plpgsql;