postgreSQL中的触发器功能?

时间:2016-11-23 20:35:45

标签: postgresql

我正在尝试创建一个触发器函数,用于检查事件日期记录的插入/更新是否有效:

create or replace function trigf() returns trigger as $$
declare bad_date record;
begin
     select festival.title into bad_date
     from festival
     where new.title = festival.title and (new.edate < festival.sdate or new.edate > festival.edate);

     if exists bad_date then
                 raise notice 'Error: Event Date does not Correlate with the Festival.';
                if T = 'insert' then return null;
                    else return old;
                        end if;
         else
             return new;
         end if;
 end;
 $$language plpgsql;

 create trigger T
 before insert or update on event
 for each row
 execute procedure trigf();

编译通过正常,但在尝试插入值时,我收到bad_date不存在的错误。

1 个答案:

答案 0 :(得分:0)

而不是:

declare bad_date record;

if exists bad_date then

应该是:

declare bad_date text; -- or same type of festival.title 

if bad_date IS NOT NULL then