遍历Oracle表中的所有列

发布于 2024-08-03 17:45:26 字数 1012 浏览 4 评论 0原文

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

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

This is a noobie question, most likely syntax one. But I am kinda lost...

I need to go over all columns in all tables in Oracle to generate trigger script. That trigger should insert the row being updated to a log table which is nearly the same as the original table. I thought I would just go over all the columns and just concatenate strings. Fairly easy but I am struggling with the syntax...

Here's what I have so far:

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;

For that I am getting the following error:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不忘初心 2024-08-10 17:45:26

试试这个:

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;

Try this:

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;
雪若未夕 2024-08-10 17:45:26

您也许能够摆脱像这样简单的事情:

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;

You may be able to get away with something as simple as this:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文