SELECT fname, lname, pcode FROM cust WHERE id = 234;
SELECT fname, lname, pcode FROM cust WHERE id = 332;
as
begin
update emp
set sal=sal*2
where empno = p_empno;
commit;
end;
/
以下介绍几种情况:
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好些,至少可以看出绑定了多少个变量。