简体   繁体   中英

How can i use column_name dynamically in pl/sql

---- Hey guys, kinldy help me in this code, i want to give column_name dynamically in dbms_output, but its giving me error ------

    --set serveroutput on;
    declare 
    type t_list_rec  is record
                              (table_name all_tab_columns.table_name%type,
                              column_name all_tab_columns.COLUMN_NAME%type);
    type t_list is table of t_list_rec index by PLS_INTEGER;
    v_array t_list;
    type e_list is table of CUSTOMER_ADDRESS%rowtype;
    t_array e_list:=e_list();
    
    begin
    select /*+ parallel(14) */ table_name,column_name bulk collect into v_array from all_tab_columns where table_name='CUSTOMER_ADDRESS' and OWNER='MIG';
    for k in 1..v_array.count() loop
    t_array.extend;
    EXECUTE IMMEDIATE
        'select /*+ parallel (16) */ * from '||v_array(k).table_name||' where not regexp_like ('||v_array(k).column_name||',''[A-za-z0-9.]'')'
          into t_array(k); 
        dbms_output.put_line(t_array(k).v_array(k).column_name);
          end loop; 
          end; 


---Error----
Error report -
ORA-06550: line 17, column 37:
PLS-00302: component 'V_ARRAY' must be declared
ORA-06550: line 17, column 5:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

Not quite sure what you want, but you can get something like it using SYS_REFCURSOR.
Example Procedure:

create or replace Procedure Desc_Tab_Content(p_tab IN VarChar2, p_owner IN VarChar2) IS
BEGIN
    DECLARE 
        type t_list_rec  is record (table_name all_tab_columns.table_name%type,
                                    column_name all_tab_columns.COLUMN_NAME%type);
        type t_list is table of t_list_rec index by PLS_INTEGER;
        v_array t_list;
        --
        mReport     SYS_REFCURSOR;  
        mSql        VarChar2(512);
        mCount      Number(6);
        mCountDist  Number(6);
        mHead       VarChar2(64);
        mHeadLen    Number(3);
        mDataLen    Number(3);
    BEGIN
        Select   table_name, column_name 
        bulk collect into v_array 
        From     all_tab_columns 
        Where    table_name = p_tab and owner = p_owner;
        --
        mHead := 'Table ' || v_array(1).table_name || ' - Colummn''s (DistVals/TotalVals)';
        mHeadLen := Length(mHead);
        dbms_output.put_line(mHead);
        dbms_output.put_line(RPAD('-', mHeadLen, '-'));
        --
        For k in 1..v_array.count() Loop
            mSql := 'Select Count(DISTINCT ' || v_array(k).column_name || '), Count(*) From ' || v_array(k).table_name;
            OPEN mReport FOR mSql;
            FETCH mReport Into mCount, mCountDist;
            mDataLen := Length(k || '. ' || v_array(k).column_name);
            dbms_output.put_line(k || '. ' || v_array(k).column_name || LPAD(' (' || mCount || '/' || mCountDist || ')', mHeadLen - mDataLen, ' '));
        End Loop;
    END;
END Desc_Tab_Content;

In the procedure you define what you want to do with your data. Here is the list of columns with number of distinct values (per column) out of total records in the given table.

... call & result:

set serveroutput on
BEGIN
    Desc_Tab_Content(your_tbl_name, your_owner_name);
END; 

/*  R e s u l t:
anonymous block completed
Table ATBL - Colummn's (DistVals/TotalVals)
-------------------------------------------
1. ADATE                              (1/3)
2. ANAME                              (3/3)
3. ID                                 (3/3)
*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM