动态分配触发器名称postgresql

时间:2016-08-06 05:57:02

标签: postgresql plpgsql database-trigger

您好我有一个Registra_cambios()函数;想要分配给我的数据库中的所有表,我想知道你是否可以将触发器名称与记录(表名)连接,我的光标在所有表上没有相同的触发器名称

创建触发器示例t_log_"记录" ()

CREATE OR REPLACE FUNCTION ActiveTriggers() returns  void as $$
DECLARE

 r record;
c CURSOR FOR SELECT table_name as tab FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE';


BEGIN

  FOR r IN c LOOP
        create trigger t_log_r before insert or update or delete
        on r.tab
        for each row
        execute procedure Registra_cambios();
  END LOOP;




END;
$$ LANGUAGE plpgsql;

1 个答案:

答案 0 :(得分:0)

soemthing like?..

do
$$
declare
  r record;
begin
  for r in (SELECT table_name as tab FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE';) loop
    execute 'create trigger t_log_r_'||r.tab||' before insert or update or delete
    on '||r.tab||'
    for each row
    execute procedure Registra_cambios()';
  end loop;
end;
$$
;