目录

  • 构造表与数据
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;
  • 结果:
    PG语句级别触发器使用示例
  • 删除之后
delete from ta where id<11;

PG语句级别触发器使用示例

  • 查看表tb
select * from tb;
  • 结果
    PG语句级别触发器使用示例
  • 可见,在使用语句级别触发器的时候,即使删除了10条记录,但是只触发了一次触发器

相关文章:

  • 2021-12-17
  • 2021-10-06
  • 2021-05-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-03-01
  • 2021-05-20
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
相关资源
相似解决方案