简体   繁体   English

使用触发器根据Postgresql中另一列的年份更新列

[英]Update column based on year of another column in postgresql using a trigger

I am trying to create a trigger to update the column reminder(datatype date) using the data of the column date_of_join(datatype date) of same table. 我正在尝试创建一个触发器,以使用同一表的date_of_join(数据类型日期)列的数据更新列提醒(数据类型日期)。 Reminder should be 1st of January, next year. 提醒应该是明年1月1日。

My function is : 我的职能是:

CREATE OR REPLACE FUNCTION update_reminder()
RETURNS TRIGGER AS
$$
BEGIN
NEW."reminder" := make_date(CAST (extract(year from timestamp NEW."date_of_join") AS INTEGER) ,2,3);
RETURN NEW;
END 
$$ LANGUAGE 
plpgsql;

My trigger is : 我的触发器是:

CREATE TRIGGER "Trigger1"
BEFORE INSERT ON faculty
for each row
EXECUTE PROCEDURE update_reminder();

It is giving me error : syntax error at or near "NEW" 它给我错误:“ NEW”或附近的语法错误

How to do this? 这个怎么做?

date_trunc can be used to round to the beginning of the year; date_trunc可以四舍五入到年初。 then you can add 1 year to arrive at January first of the following year: 那么您可以加1年才能到达次年1月1日:

NEW.reminder := date_trunc('year', NEW.date_of_join) + INTERVAL '1 year';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM