免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2708 | 回复: 2
打印 上一主题 下一主题

求绑定变量的使用例子 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-11 11:51 |只看该作者 |倒序浏览
包括绑定变量的声明 和使用 以及在游标中的使用 谢谢

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
2 [报告]
发表于 2011-08-11 17:03 |只看该作者
  1. SQL> var m number
  2. SQL> var n number
  3. SQL> exec :m := 2
复制代码

论坛徽章:
0
3 [报告]
发表于 2011-09-01 20:42 |只看该作者
供参考,批量删除数据
说明:
1 存储过程里绑定变量只能用动态sql 参考例1
2 在CURSOR里可以使用变量; 参考例3

例1
create or replace procedure mouse_update_numservice
is
type ridArray is table of rowid index by binary_integer;
v_rowid ridArray;
exec_sql varchar2(200);
begin
  
  select a.rowid bulk collect into v_rowid
  from numservice a, mouse_subscriberkey b
  where a.subscriberkey=b.subscriberkey
  and SUBSCRIBERCBP=521
  and rownum < 1000001;
  exec_sql := 'update numservice set SUBSCRIBERCBP=527 where rowid=';
  forall i in 1 .. v_rowid.COUNT
       execute immediate  exec_sql using v_rowid(i);
  commit;      
  dbms_output.put_line('update rows: '||v_rowid.COUNT);
end;
/

例2
create or replace procedure mouse_update_numservice_nobind
is
type ridArray is table of rowid index by binary_integer;
v_rowid ridArray;
begin
        select a.rowid bulk collect into v_rowid
        from numservice a, mouse_subscriberkey b
        where a.subscriberkey=b.subscriberkey
        and SUBSCRIBERCBP=521
        and rownum < 1000001;
        forall i in 1 .. v_rowid.COUNT
                        update numservice set SUBSCRIBERCBP=527 where rowid= v_rowid(i);
        commit;
        dbms_output.put_line('update rows: '||v_rowid.COUNT);
end;
/

1,2的效率差不多,没有多少的差异;



SQL> set echo on
SQL> alter system flush shared_pool;

系统已更改。

SQL>
SQL> set timing on
SQL> declare
      type rc is ref cursor;
      l_rc rc;
      l_dummy all_objects.object_name%type;
      l_start number default dbms_utility.get_time;
  begin
      for i in 1 .. 1000
      loop
          open l_rc for
         'select object_name
            from all_objects
           where object_id = ' || i;
         fetch l_rc into l_dummy;
         close l_rc;
     end loop;
     dbms_output.put_line
     ( round( (dbms_utility.get_time-l_start)/100, 2 ) ||
       ' seconds...' );
end;
/

PL/SQL 过程已成功完成。

已用时间:  00: 01: 13.03



例2
SQL> declare
      type rc is ref cursor;
      l_rc rc;
      l_dummy all_objects.object_name%type;
      l_start number default dbms_utility.get_time;
  begin
      for i in 1 .. 1000
      loop
          open l_rc for
         'select object_name
            from all_objects
           where object_id = '
         using i;
         fetch l_rc into l_dummy;
         close l_rc;
     end loop;
     dbms_output.put_line
     ( round( (dbms_utility.get_time-l_start)/100, 2 ) ||
       ' seconds...' );
end;
/

PL/SQL 过程已成功完成。

已用时间:  00: 00: 02.03
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP