2009-09-01 99 views
0

这是一个noobie问题,最有可能的语法之一。但我有点不知所措...查看Oracle中表中的所有列

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

你会得到什么错误? – 2009-09-01 10:59:13

+0

错误位于第1行 ORA-06550:行14,列84: PLS-00382:表达式是错误的类型 ORA-06550:行16,列22: PLS-00221: 'V_COLUMNS' 不是一个过程或是未定义的 ORA-06550:第16行,第5列: PL/SQL:语句被忽略 – Rashack 2009-09-01 11:00:02

回答

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; 
+0

是的 - 我知道有一些与我的语法;-) – Rashack 2009-09-01 11:17:26

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; 
+0

看起来非常接近。仍然得到错误虽然: 错误在第1行 ORA-06550:行11,列82: PLS-00382:表达式是错误的类型 ORA-06550:行12,列30: PLS-00306:错误的号码或在调用'||'时参数的类型 ORA-06550:第12行,第9列: PL/SQL:语句被忽略 – Rashack 2009-09-01 11:08:59

相关问题