【问题标题】:Go over all columns in tables in Oracle遍历Oracle中表中的所有列
【发布时间】:2009-09-01 10:56:41
【问题描述】:

这是一个菜鸟问题,很可能是语法问题。但我有点迷路了......

我需要遍历 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

【问题讨论】:

  • 第 1 行错误 ORA-06550:第 14 行,第 84 列:PLS-00382:表达式类型错误 ORA-06550:第 16 行,第 22 列:PLS-00221:'V_COLUMNS' 不是过程或未定义 ORA-06550:第 16 行,第 5 列:PL/SQL:语句被忽略

标签: oracle triggers cursor ora-06550


【解决方案1】:

试试这个:

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;

【讨论】:

  • 是的 - 我知道我的语法有问题 ;-)
【解决方案2】:

你也许可以像这样简单地逃脱:

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;

【讨论】:

  • 看起来非常接近。但仍然出现错误: 第 1 行 ORA-06550:第 11 行,第 82 列:PLS-00382:表达式类型错误 ORA-06550:第 12 行,第 30 列:PLS-00306:调用中的参数数量或类型错误到'||' ORA-06550:第 12 行,第 9 列:PL/SQL:语句被忽略
猜你喜欢
  • 2013-02-04
  • 1970-01-01
  • 2021-12-06
  • 2015-12-10
  • 1970-01-01
  • 2022-11-18
  • 2014-02-05
  • 1970-01-01
  • 2012-09-17
相关资源
最近更新 更多