PG语句级别触发器使用示例
原创
©著作权归作者所有:来自51CTO博客作者岳麓丹枫的原创作品,请联系作者获取转载授权,否则将追究法律责任
目录
drop table if exists ta;
drop table if exists tb;
create table ta(id int, name varchar);
create table tb(id int);
insert into ta select n, n || 'name' from generate_series(1, 100, 1) as t(n);
CREATE OR REPLACE FUNCTION func_sum_max_id_to_tb()
RETURNS TRIGGER
AS
$BODY$
DECLARE
BEGIN
insert into tb select max(id) from ta;
raise notice 'hello ';
RETURN NULL;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
create trigger trg_ta after insert or delete or update on ta for each STATEMENT EXECUTE PROCEDURE func_sum_max_id_to_tb();
select * from ta where id<11;
- 结果:
- 删除之后
delete from ta where id<11;
- 结果
- 可见,在使用语句级别触发器的时候,即使删除了10条记录,但是只触发了一次触发器