본문 바로가기
스터디북

[11/26] exec print_table

by 파이어볼러 2015. 11. 26.
create or replace procedure print_table( p_query in varchar2 )
AUTHID CURRENT_USER
is
    l_theCursor     integer default dbms_sql.open_cursor;
    l_columnValue   varchar2(4000);
    l_status        integer;
    l_descTbl       dbms_sql.desc_tab;
    l_colCnt        number;
begin
    execute immediate
    'alter session set 
        nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';

    dbms_sql.parse(  l_theCursor,  p_query, dbms_sql.native );
    dbms_sql.describe_columns
    ( l_theCursor, l_colCnt, l_descTbl );

    for i in 1 .. l_colCnt loop
        dbms_sql.define_column
        (l_theCursor, i, l_columnValue, 4000);
    end loop;

    l_status := dbms_sql.execute(l_theCursor);

    while ( dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
        for i in 1 .. l_colCnt loop
            dbms_sql.column_value
            ( l_theCursor, i, l_columnValue );
            dbms_output.put_line
            ( rpad( l_descTbl(i).col_name, 30 )
              || ': ' || 
              l_columnValue );
        end loop;
        dbms_output.put_line( '-----------------' );
    end loop;
    execute immediate
        'alter session set nls_date_format=''dd-MON-rr'' ';
exception
    when others then
      execute immediate
          'alter session set nls_date_format=''dd-MON-rr'' ';
      raise;
end;
/
exec print_table('select * from hr.employees');
EMPLOYEE_ID                   : 197
FIRST_NAME                    : Kevin
LAST_NAME                     : Feeney
EMAIL                         : KFEENEY
PHONE_NUMBER                  : 650.507.9822
HIRE_DATE                     : 23-may-2006 00:00:00
JOB_ID                        : SH_CLERK
SALARY                        : 3000
COMMISSION_PCT                :
MANAGER_ID                    : 124
DEPARTMENT_ID                 : 50
STARS                         :



'스터디북' 카테고리의 다른 글

<11/27> 청춘  (0) 2015.11.27
[11/26] Optimizer 용어들  (0) 2015.11.26
[11/26] partition table  (0) 2015.11.26
<11/26> HIGH HIGH  (0) 2015.11.26
<11/25> 소중한 사람  (0) 2015.11.25