Postgresql触发函数带参数

前端之家收集整理的这篇文章主要介绍了Postgresql触发函数带参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在一个名为takegresql的表上创建一个触发器来更新另一个称为student的表中的值
我试图通过以下方式做到这一点。但是我收到一个错误,在“OLD”附近有语法错误。我不明白这是什么问题。这是我的代码
  1. CREATE OR REPLACE FUNCTION upd8_cred_func
  2. (id1 VARCHAR,gr1 VARCHAR,id2 VARCHAR,gr2 VARCHAR)
  3. RETURNS void AS $$
  4. BEGIN
  5. IF (id1=id2 and gr1 is null and gr2 is not null) THEN
  6. update student set tot_cred = tot_cred + 6 where id = id1;
  7. END IF;
  8. RETURN;
  9. END;
  10. $$ LANGUAGE plpgsql;
  11.  
  12. CREATE TRIGGER upd8_cred
  13. AFTER UPDATE ON takes
  14. FOR EACH ROW
  15. EXECUTE PROCEDURE upd8_cred_func(OLD.id,OLD.grade,NEW.id,NEW.grade);
您不需要将NEW和OLD作为参数传递给触发器函数。它们是自动可用的:

http://www.postgresql.org/docs/9.1/interactive/trigger-definition.html

The trigger function must be declared as a function taking no arguments and returning type trigger. (The trigger function receives its input through a specially-passed TriggerData structure,not in the form of ordinary function arguments.)

关于传递给触发程序的记录,请参阅http://www.postgresql.org/docs/9.1/interactive/plpgsql-trigger.html

When a PL/pgsql function is called as a trigger,several special variables are created automatically in the top-level block. They are: […] NEW,[…] OLD […]

由于SeldomNeedy指出了下面的注释,您仍然可以传递并使用参数来触发函数。您将该函数声明为不使用参数,但是在定义触发器(通过CREATE TRIGGER)时,可以添加一些参数。

它们将作为TG_NARG(此类参数的数量)和TG_ARGV [](文本值数组)可用于触发器。

猜你在找的Postgre SQL相关文章