dynamically pass table name in oracle for loop

后端 未结 1 1012
暖寄归人
暖寄归人 2021-01-28 05:52

Is it possible to dynamically pass table name in oracle for loop ?

e.g :-

for nm in (\'select * from \'|| table_name))
     loop 

      dbms_output.pu         


        
相关标签:
1条回答
  • 2021-01-28 06:15

    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;
    /
    
    0 讨论(0)
提交回复
热议问题