[转载]pl/sql动态调用带参数的存储过程

以下内容来自:http://blog.itpub.net/658202/viewspace-1032467/

说明:proc_one、proc_two、proc_three用来模拟三个procedure,proc_main用来通过动态调用前面三个过

程的过程。在proc_main中输入一个参数,在proc_one/two/three中处理后返回到proc_main中。下面是code

和测试过程。

SQL> create or replace procedure proc_one(p_arg in out varchar2) as
  2  begin  
  3    dbms_output.put_line('one proc call, input arg is '||p_arg);
  4    p_arg:=p_arg||' return from proc_one';
  5  end;
  6   /

过程已创建。

SQL> 
SQL> create or replace procedure proc_two(p_arg in out varchar2) as
  2  begin
  3   dbms_output.put_line('two proc call, input arg is '||p_arg);
  4   p_arg:=p_arg||' return from proc_two';
  5  end;
  6  /

过程已创建。

SQL> 
SQL> create or replace procedure proc_three(p_arg in out varchar2) as
  2  begin
  3   dbms_output.put_line('three proc call, input arg is '||p_arg);
  4   p_arg:=p_arg||' return from proc_three';
  5  end;
  6  /

过程已创建。

SQL> create or replace procedure proc_main(
  2   v_procname in varchar2,
  3   p_arg in varchar2 default null)
  4  as
  5  
  6   v_sql varchar2(255);
  7   v_ret varchar2(255); 
  8  begin
  9   v_ret:=p_arg; 
 10   v_sql:='begin '||v_procname||'(:v1); end;';
 11   execute immediate v_sql using in out v_ret;
 12   dbms_output.put_line('main proc call completed!'); 
 13   dbms_output.put_line('main input arg return is '||v_ret);
 14  end;
 15  /

过程已创建。

SQL> 
SQL> 
备注:v_ret变量是在proc_main中动态传入到要调用的过程中的参数,然后接受被调用过程处理
后的结果并在proc_main中显示。
SQL> set serveroutput on
SQL> exec proc_main('proc_one','call one');
one proc call, input arg is call one
main proc call completed!
main input arg return is call one return from proc_one

PL/SQL 过程已成功完成。

SQL> exec proc_main('proc_two','call two');
two proc call, input arg is call two
main proc call completed!
main input arg return is call two return from proc_two

PL/SQL 过程已成功完成。

SQL> exec proc_main('proc_three','call three');
three proc call, input arg is call three
main proc call completed!
main input arg return is call three return from proc_three

PL/SQL 过程已成功完成。

SQL> 
thomas zhang 的杂货铺
[@more@]

你可能感兴趣的:(数据库)