在 PostgreSQL 中,我尝试创建一个将参数传递给函数的触发器,但此操作似乎会查找签名具有零参数的函数:
ERROR: function create_rec_if_needed() does not exist:
CREATE TRIGGER after_update_winks AFTER UPDATE ON winks
FOR EACH ROW
WHEN (NEW.counter > 3)
EXECUTE PROCEDURE create_rec_if_needed(NEW.id, 'Wink')
当我列出现有函数时,我可以看到我的 2 参数函数。这是 \df
的输出:
List of functions
Schema | Name | Result data type | Argument data types
--------+-------------------------+---------------------------+-------------------------------------------------
public | create_rec_if_needed | void | rec_id integer, rec_type text
任何人都可以解释一下为什么我的触发器没有利用现有功能吗?我应该使用替代设计来实现我的目标吗?我的目标是让多个表拥有一个调用函数 create_rec_if_needed
的触发器,并且在每种情况下,触发器必须提供记录的 ID 和特定于表的字符串。
最佳答案
A trigger procedure is created with the CREATE FUNCTION command, declaring it as a function with no arguments and a return type of trigger.
您可以使用通过TG_ARGV
传递给触发函数的参数,例如TG_TABLE_NAME
- 引发触发器调用的表的名称。 40.9. Trigger Procedures .
关于PostgreSQL:触发器调用带有参数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30982038/