如何将唯一更新的行移至Hist_table Postgres?
在更新或插入的情况下,我创建了下一个将数据从主表移动到历史表的触发器。
CREATE OR REPLACE FUNCTION process_employee_hist() RETURNS TRIGGER AS $employee_hist$
BEGIN
IF (TG_OP = 'UPDATE') THEN
INSERT INTO employee_hist SELECT 'U', now(), user, NEW.*;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO employee_hist SELECT 'I', now(), user, NEW.*;
RETURN NEW;
END IF;
RETURN NULL;
END;
$employee_hist$ LANGUAGE plpgsql;
CREATE TRIGGER employee_hist
AFTER INSERT OR UPDATE ON employee
FOR EACH ROW EXECUTE PROCEDURE process_employee_hist();
这个触发器将所有数据从主表移动到历史。 我只需要将更新的一个移至历史表。
I Created the next Trigger to move the data from a main table to an historical table in case of an update or insert.
CREATE OR REPLACE FUNCTION process_employee_hist() RETURNS TRIGGER AS $employee_hist$
BEGIN
IF (TG_OP = 'UPDATE') THEN
INSERT INTO employee_hist SELECT 'U', now(), user, NEW.*;
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO employee_hist SELECT 'I', now(), user, NEW.*;
RETURN NEW;
END IF;
RETURN NULL;
END;
$employee_hist$ LANGUAGE plpgsql;
CREATE TRIGGER employee_hist
AFTER INSERT OR UPDATE ON employee
FOR EACH ROW EXECUTE PROCEDURE process_employee_hist();
This Trigger moves all data from the main table to the historical.
I need only to move the updated one to the historical table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
Try: