oracle PL/SQL 中变量绑定用法

从Oracle的共享池的设计、和Oracle推荐的 PL/SQL 写法中,可以看出,变量绑定对性能有比较大的影响,那么,如何在PL/SQL 中使用变量绑定呢?

首先看看不使用变量绑定的用法:

declare

    cursor cur_temp(id number) is
            select * from table_a where a=id;

     c_temp cur_temp%rowtype;

beign

  open cur_temp(1);

  loop

    fetch cur_temp into c_temp;

   exit when cur_temp%notfound;

  insert into b values (c_temp.a);

    end loop;

close cur_temp;

commit;

end;

上面是没有使用变量绑定的用法,包括游标和操作语句。

再看下面使用变量绑定的用法:

先要

type cursorType is ref cursor;

然后:

declare

    cur_temp cursortype;

   c_temp table_a%rowtype;

begin

  open cur_temp for 'select * from table_a where a=:1' using 91;

   loop

       fetch cur_temp into c_temp;

       exit when cur_temp%notfound;

      execute immediate 'insert into b values (:1)' using c_temp.a;

  end loop;

close cur_temp;

commit;

end;

上面是在PL/SQL 块中的写法,这个写法也同样适用于存储过程,触发器,函数,包等可以用PL/SQL 的地方。

对于需要用 INTO 的地方,可以如下使用:

  i number(6);

execute immediate 'select count(*) from table_a where a =:1' into i using 89;

执行后,取出的值存放在了变量 i 中。

在PL/SQL 中,变量绑定的常见用法基本如上所示,建议全部使用变量绑定。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值