時需要在oracle 儲存過程中執行動態SQL 語句 ,例如表名是動態的,或欄位是動態的,或查詢命令是動態的,可用下面的方法:set serveroutput ondeclaren number;sql_stmt varchar2(50);t varchar2(20);beginexecute immediate "alter session set nls_date_format=""YYYYMMDD""";t := "t_" || sysdate;sql_stmt := "select count(*) from " || t;execute immediate sql_stmt into n;dbms_output.put_line("The number of rows of " || t || " is " || n);end;如果動態SQL 語句 很長很複雜,則可用包裝.CREATE OR REPLACE PACKAGE test_pkgISTYPE cur_typ IS REF CURSOR;PROCEDURE test_proc (v_table VARCHAR2,t_cur OUT cur_typ);END;/CREATE OR REPLACE PACKAGE BODY test_pkgISPROCEDURE test_proc (v_table VARCHAR2,t_cur OUT cur_typ)ISsqlstr VARCHAR2(2000);BEGINsqlstr := "SELECT * FROM "||v_table;OPEN t_cur FOR sqlstr;END;END;/在oracle 中批次匯入,匯出和刪除表名以某些字元開頭的表spool c:\a.sql
時需要在oracle 儲存過程中執行動態SQL 語句 ,例如表名是動態的,或欄位是動態的,或查詢命令是動態的,可用下面的方法:set serveroutput ondeclaren number;sql_stmt varchar2(50);t varchar2(20);beginexecute immediate "alter session set nls_date_format=""YYYYMMDD""";t := "t_" || sysdate;sql_stmt := "select count(*) from " || t;execute immediate sql_stmt into n;dbms_output.put_line("The number of rows of " || t || " is " || n);end;如果動態SQL 語句 很長很複雜,則可用包裝.CREATE OR REPLACE PACKAGE test_pkgISTYPE cur_typ IS REF CURSOR;PROCEDURE test_proc (v_table VARCHAR2,t_cur OUT cur_typ);END;/CREATE OR REPLACE PACKAGE BODY test_pkgISPROCEDURE test_proc (v_table VARCHAR2,t_cur OUT cur_typ)ISsqlstr VARCHAR2(2000);BEGINsqlstr := "SELECT * FROM "||v_table;OPEN t_cur FOR sqlstr;END;END;/在oracle 中批次匯入,匯出和刪除表名以某些字元開頭的表spool c:\a.sql