Oracle动态SQL返回值传到变量的写法

网上找到很多写法,有些对有些不对,自己摸索,找到一些可以通过测试的写法,总结如下(均以hr schema举例)。

1. 普通的select返回

 1 declare
 2   v_name       employees.last_name%type;
 3   v_salary     number;
 4   p_id         employees.employee_id%type;
 5 begin
 6   p_id := 102;
 7   execute immediate 'select last_name,salary from employees where employee_id=:1' 
 8   into v_name, v_salary using p_id; --动态SQL为查询语句
 9   dbms_output.put_line(v_name ||'的收入为:'||to_char(v_salary));
10   exception when others then 
11     dbms_output.put_line('找不到相应数据');
12 end;

 

2. 动态游标举例 

1 open deputy_cur for 
2    'select d.owner
3       from apr_role_user_deputy d 
4      where d.apr_code = :1
5        and d.user_number = :2'
6   using l_rol, l_user;
7 loop
8   ......;
9 end loop;

 

3. 调用存储过程举例

1 lc_temp :=  'BEGIN PROC_ABC(:1, :2, :3, :4, :5); END;';
2 execute immediate lc_temp 
3 using out lc, out l_err_msg, in l_xml_str, in l_step_seq, in out l_serial;

这个用法的要点在于:using后面的参数顺序要和procedure的一样,默认参数类别是in,最好in、out都手动加上。

 

 

 

想不起来还有其他什么用法了,想起来的话再补充吧

参考:http://www.cnblogs.com/gaolonglong/archive/2011/05/31/2064790.html

posted @ 2013-04-07 11:46  valleylord  阅读(3569)  评论(0编辑  收藏  举报