tom提供的print_table程序包

 

SQL代码
  1. create or replace procedure print_table   
  2. ( p_query in varchar2,   
  3.   p_date_fmt in varchar2 default 'dd-mon-yyyy hh24:mi:ss' )   
  4.   
  5.   
  6. -- this utility is designed to be installed ONCE in a database and used   
  7. -- by all.  Also, it is nice to have roles enabled so that queries by   
  8. -- DBA's that use a role to gain access to the DBA_* views still work   
  9. -- that is the purpose of AUTHID CURRENT_USER   
  10. AUTHID CURRENT_USER  
  11. is  
  12.     l_theCursor     integer default dbms_sql.open_cursor;   
  13.     l_columnValue   varchar2(4000);   
  14.     l_status        integer;   
  15.     l_descTbl       dbms_sql.desc_tab;   
  16.     l_colCnt        number;   
  17.     l_cs            varchar2(255);   
  18.     l_date_fmt      varchar2(255);   
  19.   
  20.   
  21.     -- small inline procedure to restore the sessions state   
  22.     -- we may have modified the cursor sharing and nls date format   
  23.     -- session variables, this just restores them   
  24.     procedure restore   
  25.     is  
  26.     begin  
  27.        if ( upper(l_cs) not in ( 'FORCE','SIMILAR' ))  
  28.        then  
  29.            execute immediate  
  30.            'alter session set cursor_sharing=exact';  
  31.        end if;  
  32.        if ( p_date_fmt is not null )  
  33.        then  
  34.            execute immediate  
  35.                'alter session set nls_date_format=''' || l_date_fmt || '''';  
  36.        end if;  
  37.        dbms_sql.close_cursor(l_theCursor);  
  38.     end restore;  
  39. begin  
  40.     -- I like to see the dates print out with times, by default, the  
  41.     -- format mask I use includes that.  In order to be "friendly"  
  42.     -- we save the date current sessions date format and then use  
  43.     -- the one with the date and time.  Passing in NULL will cause  
  44.     -- this routine just to use the current date format  
  45.     if ( p_date_fmt is not null )  
  46.     then  
  47.        select sys_context( 'userenv', 'nls_date_format' )  
  48.          into l_date_fmt  
  49.          from dual;  
  50.  
  51.  
  52.        execute immediate  
  53.        'alter session set nls_date_format=''' || p_date_fmt || '''';  
  54.     end if;  
  55.  
  56.  
  57.     -- to be bind variable friendly on this ad-hoc queries, we  
  58.     -- look to see if cursor sharing is already set to FORCE or  
  59.     -- similar, if not, set it so when we parse -- literals  
  60.     -- are replaced with binds  
  61.     if ( dbms_utility.get_parameter_value  
  62.          ( 'cursor_sharing', l_status, l_cs ) = 1 )  
  63.     then  
  64.         if ( upper(l_cs) not in ('FORCE','SIMILAR'))  
  65.         then  
  66.             execute immediate  
  67.            'alter session set cursor_sharing=force';  
  68.         end if;  
  69.     end if;  
  70.  
  71.  
  72.     -- parse and describe the query sent to us.  we need  
  73.     -- to know the number of columns and their names.  
  74.     dbms_sql.parse(  l_theCursor,  p_query, dbms_sql.native );  
  75.     dbms_sql.describe_columns  
  76.     ( l_theCursor, l_colCnt, l_descTbl );  
  77.  
  78.  
  79.     -- define all columns to be cast to varchar2's, we   
  80.     -- are just printing them out   
  81.     for i in 1 .. l_colCnt loop   
  82.         if ( l_descTbl(i).col_type not in ( 113 ) )   
  83.         then  
  84.             dbms_sql.define_column   
  85.             (l_theCursor, i, l_columnValue, 4000);   
  86.         end if;   
  87.     end loop;   
  88.   
  89.   
  90.     -- execute the query, so we can fetch   
  91.     l_status := dbms_sql.execute(l_theCursor);   
  92.   
  93.   
  94.     -- loop and print out each column on a separate line   
  95.     -- bear in mind that dbms_output only prints 255 characters/line   
  96.     -- so we'll only see the first 200 characters by my design...   
  97.     while ( dbms_sql.fetch_rows(l_theCursor) > 0 )   
  98.     loop   
  99.         for i in 1 .. l_colCnt loop   
  100.             if ( l_descTbl(i).col_type not in ( 113 ) )   
  101.             then  
  102.                 dbms_sql.column_value   
  103.                 ( l_theCursor, i, l_columnValue );   
  104.                 dbms_output.put_line   
  105.                 ( rpad( l_descTbl(i).col_name, 30 )   
  106.                 || ': ' ||  
  107.                 substr( l_columnValue, 1, 200 ) );  
  108.             end if;  
  109.         end loop;  
  110.         dbms_output.put_line( '-----------------' );   
  111.     end loop;   
  112.   
  113.   
  114.     -- now, restore the session state, no matter what   
  115.     restore;   
  116. exception   
  117.     when others then  
  118.         restore;   
  119.         raise;   
  120. END;   
  121. /  

 



Link URL: http://www.csjrc.com/default.asp?id=91

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/89703/viewspace-217908/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/89703/viewspace-217908/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值