How to get the name and value of a field from a record dynamically

I have a procedure that takes as input an entry with 170 columns (it is based on a table structure).

In the procedure, I want to call the debugging procedure, one of the parameters of which is a text string containing all the field names and values ​​of this record.

For instance:

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;

Is there any simple way to achieve this in a dynamic way (iterate over the names and values ​​of record fields) without listing all of them?

Maybe something like this:

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; 

Many thanks,

+4
source share
2 answers

Here is one way to do it. The package specification contains a variable whose type corresponds to the one we will use in the procedure.

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).

+3

, , - 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 .

+1

Source: https://habr.com/ru/post/1693023/


All Articles