【发布时间】:2019-05-31 22:16:11
【问题描述】:
我正在尝试为触发器编写一个函数,以检查关系中新条目中的日期是否大于其他关系中的条目。如果是这种情况,我想将新关系中的日期值更新为其他关系中的日期值:
create or replace function curDate()
returns trigger as $$
Begin
if (new.date >= (select date from other where new.name = other.name )) then
set new.date = (select date from playlist where new.name = other.name );
end if;
end; $$ language plpgsql;
我收到语法错误:set new.date = (select date from playlist where new.name = other.name )
但是,这很好用:
create or replace function curDate()
returns trigger as $$
declare dateVar date;
Begin
dateVar := (select date from other where new.name = other.name);
if (new.datum >= dateVar) then
new.datum = dateVar;
end if;
end; $$ language plpgsql;
这是为什么呢?
【问题讨论】:
-
关键字
set在 plpgsql 的赋值中只是多余的。顺便说一句,函数应该return new;。 -
我们需要查看触发器才能完全正确地获取触发器功能。并且始终是您的 Postgres 版本。
date还是datum?是否所有涉及的列都定义了NOT NULL和other.nameUNIQUE? -
那么你有答案了吗?
标签: sql postgresql subquery plpgsql