如何动态获取记录中的字段名称和值

时间:2018-02-01 10:15:41

标签: oracle plsql record

我有一个过程,它接收170列的记录作为输入参数(它基于表的结构)。

在我想要调用调试过程的过程中,其中一个参数是一个包含该记录的所有字段名称和值的文本字符串。

例如:

CREATE OR REPLACE PROCEDURE xxx (pi_record IN table_name%ROWTYPE) as
    text VARCHAR2(10000) := NULL;
BEGIN
    ...
    text := 'pi_record.column1 = ' || pi_record.column1 || CHR(13) ||
            'pi_record.column2 = ' || pi_record.column2 || CHR(13) ||
            ...
            'pi_record.column170 = ' || pi_record.column170;
    logging_procedure (text);
    ...
END;

有没有简单的方法以动态方式实现这一点(循环记录字段名称和值)而不枚举所有这些?

也许是这样的:

CREATE OR REPLACE PROCEDURE xxx (pi_record IN table_name%ROWTYPE) as
    text VARCHAR2(10000) := NULL;
BEGIN
    ...      
    LOOP in pi_record.columns
        text := text || CHR(13) || pi_record.column.name || ' : ' || pi_record.column.value
    END LOOP

    logging_procedure (text);
    ...
END; 

非常感谢,

2 个答案:

答案 0 :(得分:3)

这是一种方法。包规范包含一个变量,其类型与我们将在过程中使用的变量匹配。

SQL> set serveroutput on
SQL> create or replace package pkg_xxx
  2  as
  3     dept_rec   dept%rowtype;
  4  end;
  5  /

Package created.

SQL> create or replace procedure xxx (pi_record in dept%rowtype)
  2  as
  3     text    varchar2 (10000) := null;
  4     l_str   varchar2 (200);
  5     l_var   varchar2 (200);
  6  begin
  7     pkg_xxx.dept_rec := pi_record;
  8
  9     for cur_r in (  select column_name
 10                       from user_tab_columns
 11                      where table_name = 'DEPT'
 12                   order by column_id)
 13     loop
 14        l_str :=
 15              'begin '
 16           || ':x := to_char(pkg_xxx.dept_rec.'
 17           || cur_r.column_name
 18           || '); '
 19           || 'end; ';
 20
 21        execute immediate l_str using out l_var;
 22
 23        text := text || chr (10) || cur_r.column_name || ' = ' || l_var;
 24     end loop;
 25
 26     dbms_output.put_line (text);
 27  end;
 28  /

Procedure created.

现在,让我们将一些内容传递给程序,看看会发生什么:

SQL> declare
  2     cursor c1
  3     is
  4        select *
  5          from dept
  6         where deptno = 10;
  7
  8     c1r   c1%rowtype;
  9  begin
 10     open c1;
 11     fetch c1 into c1r;
 12     close c1;
 13
 14     xxx (c1r);
 15  end;
 16  /

DEPTNO = 10
DNAME = ACCOUNTING
LOC = NEW YORK

PL/SQL procedure successfully completed.

SQL>

嗯,某种有效(如果这就是你的要求)。当然,这只是一个例子,如果你想获得非常聪明的东西,你必须修改它(提示:DATE列)。

答案 1 :(得分:1)

我唯一的想法是将记录插入TEMP表:

CREATE OR REPLACE PROCEDURE xxx (pi_record IN TABLE_NAME%ROWTYPE) AS

   TEXT VARCHAR2(10000) := NULL;
   item VARCHAR2(1000);

    TABLE_DOES_NOT_EXIST EXCEPTION;
    PRAGMA EXCEPTION_INIT(TABLE_DOES_NOT_EXIST, -942);

BEGIN

    BEGIN
        EXECUTE IMMEDIATE 'DROP TABLE TABLE_NAME_TMP';
        EXCEPTION
            WHEN TABLE_DOES_NOT_EXIST then null;
    END;
    EXECUTE IMMEDIATE 'CREATE GLOBAL TEMPORARY TABLE TABLE_NAME_TMP AS SELECT * FROM TABLE_NAME WHERE ROWNUM = 0';

    DELETE FROM TABLE_NAME_TMP;
    INSERT INTO TABLE_NAME_TMP VALUES pi_record;

    FOR aCol IN (SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS WHERE table_name = 'TABLE_NAME' ORDER BY COLUMN_ID) LOOP       
        EXECUTE IMMEDIATE 'SELECT '||aCol.COLUMN_NAME||' FROM TABLE_NAME_TMP' INTO item;
        TEXT := TEXT || CHR(13) || aCol.COLUMN_NAME || ' : ' || item;
   END LOOP;
   DBMS_OUTPUT.PUT_LINE ( TEXT );
END;

如果表TABLE_NAME具有静态属性,则应跳过动态DROP TABLE ...CREATE GLOBAL TEMPORARY TABLE ...并仅创建一次TEMP表。