存储过程探索

存储过程的优势

  • 减少应用和数据库之间的网络传输。所有的SQL语句都存储在数据库服务器中,应用程序只需要发送函数调用并获取了结果,避免了发送多个SQL语句并等待结果
  • 提高应用的性能。因为自定义函数和存储过程进行了预编译并存储在数据库服务器中
  • 可重用性。存储过程和函数的功能可以被多个应用同时使用

存储过程的劣势

  • 导致软件开发缓慢。因为存储过程需要单独学习,而且很多开发人员并不具备这种技能
  • 不易进行版本管理和代码调试
  • 不同数据库管理系统之间无法移植,语法存在较大的差异

1.有参存储过程:

create procedure delete_ums_histroy (p_updated_date character varying) 
as $body$ 
declare 
  v_p_updated_date timestamp := to_timestamp(p_updated_date, 'yyyy-mm-dd');
  commit_counter integer := 0;
  conclusion_histroy record;
 begin
   for conclusion_histroy in (select id from ims_cx_test t where t.updated_date < v_p_updated_date)loop
   delete from ims_cx_test n where n.id = conclusion_histroy.id;
   commit_counter := commit_counter + 1;
   if mod(commit_counter,100) = 0 then
       commit;
     end if;
   end loop;
   commit;
 end;
 $body$;
 language 'plpgsql';
 
//授权
grant execute on procedure public.delete_ums_histroy(character varying) to public;

2.无参存储过程:

create procedure copy_ums_histroy () 
as $body$ 
declare 
  commit_counter integer := 0;
  conclusion_histroy record;
 begin
   for conclusion_histroy in (select id,previewDate,deptCode from ims_cx_test t where t.previewDate >= '2022-05-01')loop
   insert into ims_cx_test_mgr(ids,preview_date,dept_code) values(conclusion_histroy.id,conclusion_histroy.previewDate,conclusion_histroy.deptCode);
   commit_counter := commit_counter + 1;
   if mod(commit_counter,100) = 0 then
       commit;
     end if;
   end loop;
   commit;
 end;
 $body$;
 language 'plpgsql';
 
//授权
grant execute on procedure public.delete_ums_histroy(character varying) to public;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值