postgresql - postgres trigger pass input to function
问题描述
I am trying to make a trigger that when a value is put into table1 it is then copied into table2. I have the following function which works correctly:
Create function func1(a integer) returns void
Language SQL as $$
insert into table2 values (a);$$;
and here is the trigger (it doesn't work):
Create trigger trig1
Before update on table1
For each row
Execute function func1(old.attr1)
I am trying to follow this documentation. Here is the part in particular I am following:
CREATE TRIGGER log_update
AFTER UPDATE ON accounts
FOR EACH ROW
WHEN (OLD.* IS DISTINCT FROM NEW.*)
EXECUTE PROCEDURE log_account_update();
I figured that old.attribute gives a particular attribute of the old row before it is changed. If I'm wrong on this let me know. Thanks
解决方案
文档说明了触发器中调用的函数:
用户提供的函数,声明为不带参数并返回类型触发器,当触发器触发时执行。
此函数可以访问OLD
和NEW
对象。您可以修改现有功能:
Create function func1() RETURNS trigger
as $$
BEGIN
insert into table2 values (OLD.attr1);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
Create trigger trig1
Before update on table1
For each row
Execute function func1();
推荐阅读
- amazon-web-services - 无法将文件上传到 Amazon Glacier
- javascript - Flow 不允许我将 `Array` 传递给 `Array `(子类型数组到超类型数组)
- c# - C# Windows 窗体如何清空所有其他监视器
- php - PDO - 如果不存在则创建数据库
- rust - 在具有相同生命周期的嵌套引用的情况下,借用检查器的逻辑到底是什么?
- java - 无法解析类型 freemarker.core.Configurable。它是从所需的 .class 文件中间接引用的
- kdb - 如何加入kdb中的表列表
- bash - 如何在bash中找到字符串中的最后一个分组数字
- xml - 避免在 XML 文件保存期间转义特殊字符
- matlab - 使用 bsxfun 时出错 不支持混合整数类输入