在oracle for循环中动态传递表名

时间:2012-01-23 09:28:15

标签: oracle plsql

是否可以在oracle for循环中动态传递表名?

例如: -

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

      dbms_output.put_line('chetan') ;
end loop;

1 个答案:

答案 0 :(得分:1)

您可以使用REF CURSOR类型的光标。 来自Oracle Doc :(也包含绑定 - 你没有要求他们......)

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;
/