【发布时间】:2017-11-01 12:35:41
【问题描述】:
我写了这个触发器函数来计算新插入的行数并将其存储在一个表中:
CREATE OR REPLACE FUNCTION public.count_inserted()
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE
n int;
BEGIN
n = 0;
IF(TG_OP = 'INSERT') THEN
IF (NEW.date = OLD.date) THEN
update ouf set crt = crt+1 where date = new.date;
return null;
ELSE
insert into ouf (crt,date) values (n+1,now());
return null;
END IF;
END IF;
return null;
END;
$BODY$;
CREATE TRIGGER count_insrt
AFTER INSERT
ON public.test
FOR EACH ROW
EXECUTE PROCEDURE public.count_inserted();
但是当我尝试插入一个新行时,我得到了这个错误:
尚未分配旧记录
如果您对此错误的来源有任何想法,请告诉我。
【问题讨论】:
-
在插入时,没有旧版本的行 - 只有新的 - 如果在插入之前行不存在,您可以比较
IF (NEW.date = OLD.date) THEN
标签: postgresql