oracle中execute immediate的使用(select/insert/update/delete)(轉)


execute immediate的語法如下:

execute immediate 'sql';
execute immediate 'sql_select' into var_1, var_2;
execute immediate 'sql' using [in|out|in out] bind_var_1, [in|out|in out] bind_var_2;
execute immediate 'sql_select' into var_1, var_2 using [in|out|in out] bind_var_1, [in|out|in out] bind_var_2;
execute immediate 'sql' returning into var_1;
execute immediate 'sql' bulk collect into indexed_var;

 

--動態命令的實用參考一( select ..into)
--Execute immediate 命令在使用into關鍵字時與靜態的plsql塊中
--into關鍵字使用方法上的區別。

declare
v_result varchar2(20);
begin
execute immediate 'select dname from scott.dept d where d.deptno=:1' Into v_result --:1這是一個綁定變量
Using 70;
commit;
dbms_output.put_line('結果為:'||v_result);
end;
--綁定變量,通俗的講就是一個占位的參數,而后用using中的常量或者變量去找它的相應

Declare
v_result Varchar2(20);
Begin
Select dname Into v_result From scott.dept d Where d.deptno=10;
dbms_output.put_line(v_result);
End;

declare
v_result varchar2(20);
begin
execute immediate 'select dname from scott.dept d where d.deptno=:1' Into v_result
Using 70;
commit;
dbms_output.put_line('結果為:'||v_result);
end;


--動態命令的實用參考一( insert ..into)
--
Begin
Execute Immediate 'insert into scott.dept values (:1,:2,:3) '
Using 70,'IT','Beijing';
Commit;
End;


declare
l_depnam varchar2(20) := 'testing';
l_loc varchar2(10) := 'Dubai';
begin
execute immediate 'insert into scott.dept values (:1, :2, :3)'
using 60, l_depnam, l_loc;
commit;
end;


---盡享動態執行sql的execute immediate的命令使用的魅力。
--對比靜態的測試,和動態測試。
Declare
v_result Varchar2(20);
Begin
v_result:=mypackage.myfun1(10);
dbms_output.put_line(v_result);
mypackage.mypro1(20020101,v_result);
dbms_output.put_line(v_result);

End;
--Select * From student s Where s.student_id=20020101;
---Select * From testtable Where recordnumber=10;


Declare
v_result Varchar2(20);
Begin

--動態執行函數
Execute Immediate 'begin :1:=mypackage.myfun1(:2); end;'
Using Out v_result,In 10;
Commit;
dbms_output.put_line(v_result);

--動態執行存儲過程
Execute Immediate 'begin mypro1(:1,:2); end;'
Using In 20020101,Out v_result;
Commit;
dbms_output.put_line(v_result);
End;


--動態執行update語句實例:
create or replace function f_testweekday
return integer
is
v_sql varchar(2000);
i_sendcount integer;
begin
--_' || f_getweekday() || '
v_sql := 'update t_push_smstemp_inform set sendcount=sendcount+1 returning sendcount into :1';

execute immediate v_sql using out i_sendcount;

return 0;
exception
when others then
rollback;
return 0;
end f_testweekday;

http://blog.csdn.net/tanshi/article/details/7083964

EXECUTE IMMEDIATE -- 用法例子

1. 在PL/SQL運行DDL語句
begin
   execute immediate 'set role all';
end;
2. 給動態語句傳值(USING 子句)
declare
   l_depnam varchar2(20) := 'testing';
   l_loc     varchar2(10) := 'Dubai';
   begin
   execute immediate 'insert into dept values   (:1, :2, :3)'
     using 50, l_depnam, l_loc;
   commit;
end;

3. 從動態語句檢索值(INTO子句)
declare
   l_cnt     varchar2(20);
begin
   execute immediate 'select count(1) from emp'
     into l_cnt;
   dbms_output.put_line(l_cnt);
end;

4. 動態調用例程.例程中用到的綁定變量參數必須指定參數類型.
黓認為IN類型,其它類型必須顯式指定

declare
   l_routin    varchar2(100) := 'gen2161.get_rowcnt';
   l_tblnam    varchar2(20) := 'emp';
   l_cnt       number;
   l_status    varchar2(200);
begin
   execute immediate 'begin ' || l_routin || '(:2, :3, :4); end;'
     using in l_tblnam, out l_cnt, in out l_status;

   if l_status != 'OK' then
      dbms_output.put_line('error');
   end if;
end;

5. 將返回值傳遞到PL/SQL記錄類型;同樣也可用%rowtype變量
declare
   type empdtlrec is record (empno   number(4),
                            ename   varchar2(20),
                            deptno   number(2));
   empdtl empdtlrec;
begin
   execute immediate 'select empno, ename, deptno ' ||
                    'from emp where empno = 7934'
     into empdtl;
end;

6. 傳遞並檢索值.INTO子句用在USING子句前

declare
   l_dept     pls_integer := 20;
   l_nam      varchar2(20);
   l_loc      varchar2(20);
begin
   execute immediate 'select dname, loc from dept where deptno = :1'
     into l_nam, l_loc
     using l_dept ;
end;

7. 多行查詢選項.對此選項用insert語句填充臨時表,
用臨時表進行進一步的處理,也可以用REF cursors糾正此缺憾.

declare
   l_sal    pls_integer := 2000;
begin
   execute immediate 'insert into temp(empno, ename) ' ||
                    '           select empno, ename from emp ' ||
                    '           where   sal > :1'
     using l_sal;
   commit;
end;
對於處理動態語句,EXECUTE IMMEDIATE 比以前可能用到的更容易並且更高效.
當意圖執行動態語句時,適當地處理異常更加重要.應該關注於捕獲所有可能的異

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM