问题
Is it possible to dynamically pass table name in oracle for loop ?
e.g :-
for nm in ('select * from '|| table_name))
loop
dbms_output.put_line('chetan') ;
end loop;
回答1:
You can use a REF CURSOR type cursor. From Oracle Doc: (contains also binds - you don't asked for them...)
CREATE OR REPLACE PROCEDURE query_invoice(
month VARCHAR2,
year VARCHAR2) IS
TYPE cur_typ IS REF CURSOR;
c cur_typ;
query_str VARCHAR2(200);
inv_num NUMBER;
inv_cust VARCHAR2(20);
inv_amt NUMBER;
BEGIN
query_str := 'SELECT num, cust, amt FROM inv_' || month ||'_'|| year
|| ' WHERE invnum = :id';
OPEN c FOR query_str USING inv_num;
LOOP
FETCH c INTO inv_num, inv_cust, inv_amt;
EXIT WHEN c%NOTFOUND;
-- process row here
END LOOP;
CLOSE c;
END;
/
来源:https://stackoverflow.com/questions/8969449/dynamically-pass-table-name-in-oracle-for-loop