You could use the open ... for cursor syntax:
declare
v_tbl_name varchar2(30) := 'EMPLOYEES'; -- assume this will be a parameter later
v_sql varchar2(32767) := 'select employee_id, first_name from ' || v_tbl_name;
-- if there is a static table you could use %rowtype, or your own type with column%type
type t_rec is record (employee_id number, first_name varchar2(20));
v_rec t_rec;
c1 sys_refcursor;
begin
open c1 for v_sql;
loop
fetch c1 into v_rec;
exit when c1%notfound;
dbms_output.put_line(v_rec.employee_id||' '||v_rec.first_name);
end loop;
end;
/
100 Steven
101 Neena
102 Lex
103 Alexander
104 Bruce
...
205 Shelley
206 William
PL/SQL procedure successfully completed.
Hopefully you are specifying the columns you actually want, and not using *...
Depending on your application/client you could access the ref cursor directly; this syntax works in SQL*Plus, SQL Developer, SQLcl and possibly some third-party clients:
var rc refcursor;
declare
v_tbl_name varchar2(200) := 'EMPLOYEES'; -- assume this will be a parameter later
v_sql varchar2(200) := 'select * from ' || v_tbl_name;
begin
open :rc for v_sql;
end;
/
print rc
which produces output like:
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- -------------------- ------------------------- ------------------------- -------------------- ---------- ---------- ---------- -------------- ---------- -------------
100 Steven King SKING 515.123.4567 1987-06-17 AD_PRES 24000 90
101 Neena Kochhar NKOCHHAR 515.123.4568 1989-09-21 AD_VP 17000 100 90
102 Lex De Haan LDEHAAN 515.123.4569 1993-01-13 AD_VP 17000 100 90
...
205 Shelley Higgins SHIGGINS 515.123.8080 1994-06-07 AC_MGR 12000 101 110
206 William Gietz WGIETZ 515.123.8181 1994-06-07 AC_ACCOUNT 8300 205 110
107 rows selected.
You can access the ref cursor from JDBC etc.
billing202001@billingdbnext monthbilling202002@billingdb,after many research I figure out I have to use dynamic SQL but I don't know how fetch the data into a variable and print it in a for loop .