Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2341471
  • 博文数量: 2110
  • 博客积分: 18861
  • 博客等级: 上将
  • 技术积分: 24420
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-05 18:23
文章分类

全部博文(2110)

文章存档

2011年(139)

2010年(1971)

我的朋友

分类: Oracle

2010-02-26 16:38:21

1.让Oracle自己绑定变量
  set serverout on;
  set timing on;
  declare
  l_sql varchar2(2000);
  l_count number;
  l_param1 varchar2(100);
  l_param2 varchar2(100);
  begin
  l_param1:=’a';
  l_param2:=’b';
  select count(*) into l_count from table1 where col_1=l_param1 and col_2=l_param2;
  dbms_output.put_line(l_count);
  end;
  /
  在上面的情况,Oracle会自己绑定变量,即,如果参数保存在一个数组中,select语句放在一个循环中,
  select 语句只会编译一次。
  像这样
  for i in 1..3
  loop
  select count(*) into l_count from table1 where col_1=l_param1 and col_2=l_param2 and col_3=i;
  dbms_output.put_line(l_count);
  end loop
  2.不绑定变量
  set serverout on;
  set timing on;
  declare
  l_sql varchar2(2000);
  l_count number;
  l_param1 varchar2(100);
  l_param2 varchar2(100);
  begin
  l_param1:=’a';
  l_param2:=’b';
  l_sql:=’select count(*) into :x from table1 where col_1=’||l_param1||’ and col_2=’||l_param2;
  Execute Immediate l_sql into l_count;
  dbms_output.put_line(l_count);
  end;
  /
  3.动态绑定变量
  set serverout on;
  set timing on;
  declare
  l_sql varchar2(2000);
  l_count number;
  l_param1 varchar2(100);
  l_param2 varchar2(100);
  begin
  l_param1:=’a';
  l_param2:=’b';
  l_sql:=’select count(*) into :x from table1 where col_1=:y and col_2=:z ’;
  Execute Immediate l_sql into l_count using l_param1,l_param2;
  dbms_output.put_line(l_count);
  end;
  /
  :x,:y,:z相当于占位符,即
  1.用:p1,:p2,:p3是一样的。
  2.用:x,:x,:x也是一样的
  需要的绑定变量按顺序排在执行语句后面就可以了,into的除外。
  不过还是用p1,p2好些,至少可以看出绑定了多少个变量。
阅读(1301) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~