我在PostgreSQL中创建了这个PL / pgSQL触发器函数:
CREATE TRIGGER question31
BEFORE INSERT OR UPDATE
ON public."Semester"
FOR EACH ROW
EXECUTE PROCEDURE public.trigger31();
CREATE or replace FUNCTION public.trigger31()
RETURNS trigger
AS
$$
begin
if(TG_OP='UPDATE')then
if (new.start_date>(select p.end_date from "Semester" p where p.semester_id in(select s.semester_id-1 from "Semester" s where s.end_date=old.end_date))
or new.start_date>=new.end_date) then
return old;
else
if new.semester_status='past'
if old.semester_status='present' then
return new;
elsif old.semester_status='future' then
return old;
else
return new;
end if;
end if;
end if;
end if;
RETURN new;
end;
$$
language 'plpgsql'
Update
之前的我的桌子:
________________________________________________________
| semester_id | start_date | end_date | semester_status |
-------------- ------------ ------------ -----------------
| 1 | 2010-1-10 | 2010-12-10 | past |
-------------- ------------ ------------ -----------------
| 2 | 2011-1-10 | 2011-12-10 | present |
-------------- ------------ ------------ -----------------
| 3 | 2012-1-10 | 2012-12-10 | future |
-------------- ------------ ------------ -----------------
| 4 | 2013-1-10 | 2013-12-10 | future |
-------------- ------------ ------------ -----------------
我希望Update semester_id=2
像这样:
Update "Semester"
set semester_status = 'past'
where semester_id = 2
我等着将'semester_status'
从'present'
更改为'past'
我们得到了相同的结果并尝试Update start_date
。
我的意思是update
无法正常工作。
我该如何解决这个问题?