PostgreSQL日期触发语法错误

时间:2018-04-12 10:40:09

标签: postgresql date triggers syntax-error

我是SQL的新手,我不知道在PostgreSQL中执行该代码时我做错了什么(我在这里添加数字来标记这些行):

create function annouc_dates()
returns trigger as '
begin
    if new.creation_date is NULL then
        new.creation_date := select current_timestamp;
    end if;
    if new.removal_date is NULL then
        new.removal_date := select new.creation_date + interval '2 week';
    end if;
    return new;
end';

我收到一条消息: 错误:语法错误在“2”或附近 第8行:...... emoval_date:= new.creation_date + interval'2 week';

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

该问题涉及嵌套引用。使用Postgres美元引用,如4.1.2.4. Dollar-quoted String Constants.

中所述
create function annouc_dates()
returns trigger language plpgsql as $$
begin
    if new.creation_date is NULL then
        new.creation_date := current_timestamp;
    end if;
    if new.removal_date is NULL then
        new.removal_date := new.creation_date + interval '2 week';
    end if;
    return new;
end
$$;