从表中选择表格(子查询)

时间:2014-09-18 13:03:16

标签: sql oracle

我在plsql中写道:

w_table    USER_TAB_COLS.TABLE_NAME%TYPE  :='T481INPUTRIGADOC' ;

cursor selecttable is

select cols.*
FROM USER_TAB_COLS cols 
JOIN USER_TABLES tabs ON cols.TABLE_NAME = tabs.TABLE_NAME
WHERE 1=1
AND cols.COLUMN_NAME LIKE '%IDANAGPROPRIETARIO%' --Dovrei prenedere ogni i-esimo elemento della lista
ORDER BY cols.TABLE_NAME, cols.COLUMN_ID;


BEGIN
    tmpVar := 0;

    w_esitoElaborazione:='OK';
    w_descrizioneErrore:='';

    FOR REC IN selecttable LOOP
        w_cnt_record := w_cnt_record+1;
        w_table := REC.TABLE_NAME;
        w_col := REC.COLUMN_NAME;

        select w_col into a from w_table;

    end loop

但问题出在select查询中,因为输出中写有

[错误] ORA-00942(76:30):PL / SQL:ORA-00942:表或视图不存在

1 个答案:

答案 0 :(得分:1)

在形成整个quire字符串后,您已使用EXECUTE IMMEDIATE进行SQL调用动态

 FOR REC IN selecttable LOOP
        w_cnt_record := w_cnt_record+1;
        w_table := REC.TABLE_NAME;
        w_col := REC.COLUMN_NAME;

        EXECUTE IMMEDIATE 'select '||w_col||' from '||w_table INTO a;

end loop