在postgresql中插入新行时触发更新表列?

时间:2016-11-09 05:03:21

标签: postgresql database-trigger

我想在插入新行时写入更新表的触发器。我正在使用updatea Query

UPDATE table SET
  geom = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326) 

2 个答案:

答案 0 :(得分:1)

您需要使用BEFORE触发器,然后分配新值:

CREATE OR REPLACE FUNCTION function_update()
  RETURNS trigger AS
$BODY$
BEGIN
    new.geom := ST_SetSRID(ST_MakePoint(new.longitude, new.latitude), 4326);
    RETURN new;
END;
$BODY$
LANGUAGE plpgsql;

这只能在BEFORE触发器中完成:

CREATE TRIGGER triggerinsert
  BEFORE INSERT
  ON rdpr
  FOR EACH ROW
  EXECUTE PROCEDURE function_update();

答案 1 :(得分:0)

我通过编写像

这样的触发函数来解决
(gdb) define pp
Type commands for definition of "pp".
End with a line saying just "end".
>set $i = 0       
>while ($i < $arg1)
 >p *$arg0[$i++] 
 >end           
>end           
(gdb) pp queue num_queues

我写了触发器来更新

CREATE OR REPLACE FUNCTION function_update()
  RETURNS trigger AS
$BODY$
BEGIN
   new.geom := ST_SetSRID(ST_MakePoint(new.longitude, new.latitude), 4326);

RETURN new;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION function_update()
  OWNER TO postgres;