遍历Oracle中表中的所有列

时间:2009-09-01 10:56:41

标签: oracle triggers cursor ora-06550

这是一个noobie问题,很可能是语法问题。但我有点失落......

我需要遍历Oracle中所有表中的所有列以生成触发器脚本。该触发器应该将正在更新的行插入到与原始表几乎相同的日志表中。我以为我会遍历所有列,只是连接字符串。相当简单,但我正在努力学习语法...

这是我到目前为止所拥有的:

DECLARE
   cursor tableNames is
      select table_name
      from user_tables
      where table_name not like '%_A';
    lSql varchar2(3000);
    type t_columnRow is ref cursor;

    v_columns t_columnRow;
begin

FOR tableName in tableNames
LOOP
    open v_columns for select COLUMN_NAME from user_tab_columns where table_name = tableName;

    for columnRow in v_columns LOOP
        DBMS_OUTPUT.PUT_LINE(tableName || '.' || columnRow.COLUMN_NAME);
        -- Here I would just concatenate the strings ....
    END LOOP;

END LOOP;    

End;

为此我收到以下错误:

Error at line 1
ORA-06550: line 14, column 84:
PLS-00382: expression is of wrong type
ORA-06550: line 16, column 22:
PLS-00221: 'V_COLUMNS' is not a procedure or is undefined
ORA-06550: line 16, column 5:
PL/SQL: Statement ignored

2 个答案:

答案 0 :(得分:2)

试试这个:

BEGIN
    FOR t IN (SELECT table_name FROM user_tables WHERE table_name not like '%_A')
    LOOP
        FOR c IN (SELECT column_name FROM user_tab_columns WHERE table_name = t.table_name)
        LOOP
            DBMS_OUTPUT.PUT_LINE(t.table_name||'.'||c.column_name);
            -- Here I would just concatenate the strings ....
        END LOOP;
    END LOOP;
END;

答案 1 :(得分:1)

你可能能够摆脱这样简单的事情:

DECLARE
   cursor tableNames is
      select table_name
      from user_tables
      where table_name not like '%_A';
    lSql varchar2(3000);
begin

FOR tableName in tableNames
LOOP   
    for columnRow in (select COLUMN_NAME from user_tab_columns where table_name = tableName) LOOP
        DBMS_OUTPUT.PUT_LINE(tableName || '.' || columnRow.COLUMN_NAME);
        -- Here I would just concatenate the strings ....
    END LOOP;

END LOOP;    

End;