天天看點

[轉載]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 的雜貨鋪      

[@[email protected]]