postgresql触发NEW行作为json

时间:2017-03-30 10:18:57

标签: postgresql stored-procedures triggers

我正在编写一个触发器,我需要将NEW行转换为json并插入到另一个表中。我说得不对。

     CREATE OR REPLACE FUNCTION public.eventlogs_add_user()
      RETURNS trigger AS

    $BODY$
    DECLARE newRecord JSON;
    BEGIN

    newRecord :=  row_to_json(row)
        from (NEW) row;


// Insert newRecord into another table


    RETURN NEW;
    END$BODY$
      LANGUAGE plpgsql VOLATILE
      COST 100;
    ALTER FUNCTION public.eventlogs_add_user()
      OWNER TO postgres;

    -----------------------------------------------------------

1 个答案:

答案 0 :(得分:1)

更改

newRecord :=  row_to_json(row)
        from (NEW) row;

newRecord :=  row_to_json(NEW.*);

应该帮助