匿名用户PostgreSQL 触发器里面怎么判断update的影响行数?
PostgreSQL 触发器里面怎么判断update的影响行数?
我来答
添加附件
收藏
分享
问题补充
1条回答
默认
最新
回答交流
提交
问题信息
请登录之后查看
邀请回答
暂无人订阅该标签,敬请期待~~
墨值悬赏
匿名用户PostgreSQL 触发器里面怎么判断update的影响行数?
在触发器里可以使用过渡表进行统计,示例如下:
create table test (id serial primary key, info varchar);
insert into test (info) values
('One'),
('Two'),
('Three'),
('Four'),
('Five');
create function test_trigger() returns trigger
as $$
declare
rec record;
begin
for rec in select * from v_old_table LOOP
raise notice 'OLD: %', rec;
end loop;
for rec in select * from v_new_table LOOP
raise notice 'NEW: %', rec;
end loop;
return NEW;
end;
$$ language plpgsql;
create trigger tg_test
after update on test
referencing new table as v_new_table
old table as v_old_table
for each statement
execute procedure test_trigger();
执行update操作
postgres=# update test set info = 'test' where id between 2 and 4;
NOTICE: OLD: (2,Two)
NOTICE: OLD: (3,Three)
NOTICE: OLD: (4,Four)
NOTICE: NEW: (2,test)
NOTICE: NEW: (3,test)
NOTICE: NEW: (4,test)
UPDATE 3
对上面的v_old_table过度表进行处理即可。
评论
有用 0
墨值悬赏