方法一: EXECUTE IMMEDIATE
DECLARE
dd date;
LC$Requete VARCHAR2(256) ;
BEGIN
LC$Requete:= 'SELECT sysdate from dual';
EXECUTE IMMEDIATE LC$Requete INTO dd ;
dbms_output.put_line(dd);
END ;
/
方法二: 游标 CURSOR
conn scott/tiger;
set serveroutput on ;
set linesize 150;
select * from emp;
DECLARE
TYPE EmpCurTyp IS REF CURSOR;
emp_cv EmpCurTyp;
v_name emp.ename%TYPE;
v_sal emp.sal%TYPE;
my_sal NUMBER := 200;
table_name VARCHAR2(30) := 'emp';
BEGIN
OPEN emp_cv FOR 'SELECT ename, sal FROM ' || table_name ||
' WHERE sal > ' || my_sal;
LOOP
FETCH emp_cv into v_name, v_sal;
EXIT WHEN emp_cv%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_name || ' ' || v_sal);
END LOOP;
CLOSE emp_cv;
END;
/