有时需要在oracle 存储过程中执行动态SQL 语句 ,例如表名是动态的,或字段是动态的,或查询命令是动态的,可用下面的方法: set serveroutput on declare n number; sql_stmt varchar2(50); t varchar2(20); begin execute 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_pkg IS TYPE cur_typ IS REF CURSOR; PROCEDURE test_proc (v_table VARCHAR2,t_cur OUT cur_typ); END; / CREATE OR REPLACE PACKAGE BODY test_pkg IS PROCEDURE test_proc (v_table VARCHAR2,t_cur OUT cur_typ) IS sqlstr VARCHAR2(2000); BEGIN sqlstr := 'SELECT * FROM '||v_table; OPEN t_cur FOR sqlstr; END; END; / 在oracle 中批量导入,导出和删除表名以某些字符开头的表 spool c:\a.sql select 'drop table ' || tname || ';' from tab where tname like 'T%'; spool off @c:\a