dynamically pass table name in oracle for loop

独自空忆成欢 提交于 2021-02-05 11:51:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!