我想创建一个触发器,该触发器将执行一个存储过程,该过程将更新几行。当我使用Call方法测试该过程时,该过程有效,但触发器找不到该函数,并且错误提示该函数不存在。这是我的程序
create or replace PROCEDURE update_reg_location(latitude text, longitude text,userid int)
LANGUAGE SQL
AS $$
update users set reg_lat=latitude , reg_lon=longitude where id =userid
$$;
我使用时
call update_reg_location('123','234',1)
从IDE可以正常工作,并且记录已更新,但是当我在触发器中使用它时,它将无法编译。
CREATE TRIGGER update_reg_location
after INSERT ON users
FOR EACH ROW
EXECUTE PROCEDURE update_reg_location('123','234',1);
当我想这样获得插入行的新值时也会出错
CREATE TRIGGER update_reg_location
after INSERT ON users
FOR EACH ROW
EXECUTE PROCEDURE update_reg_location(new.lat,new.lon,1);
获取错误为新时或附近的语法错误。(new.lat,new.lon,1);在这一行
如果您使用BEFORE触发器,则不需要将参数传递给过程,也不需要使用UPDATE
,只需分配值即可。
触发函数还需要使用PL / pgSQL,您不能使用SQL编写它们。
create or replace function update_reg_location()
returns trigger
LANGUAGE plpgsql
AS $$
begin
new.reg_lat = new.lat;
new.reg_lon = new.lon;
return new;
end;
$$;
然后定义BEFORE
触发器:
CREATE TRIGGER update_reg_location
BEFORE INSERT ON users
FOR EACH ROW
EXECUTE PROCEDURE update_reg_location();