How can i extract data from flat file to oracle tablw with plsql?

上一篇 / 下一篇  2008-01-21 23:24:29 / 个人分类:Concepts&Basic

Pl/sql cao do it
 
 
 
SQL> create or replace function  dump_csv( p_query     in varchar2,
E!vq\.V6TcF)U0  2                                        p_separator in varchar2ITPUB个人空间x `9Pt~ZB
  3                                                      default ',',ITPUB个人空间9D3f)zo]5l?b
  4                                        p_dir       in varchar2 ,ITPUB个人空间HJR-R3x$S}n
  5                                        p_filename  in varchar2 )
#x NE4G$gK_6G)s0  6  return numberITPUB个人空间(sTZ}:A
  7  AUTHID CURRENT_USERITPUB个人空间([KK6_}
  8  isITPUB个人空间g?4}7Y5mG7~;J
  9      l_output        utl_file.file_type;
b)Ri3X4p;o&Sb5q0 10      l_theCursor     integer default dbms_sql.open_cursor;
9t#Z]Hf(Y K/`Q0 11      l_columnValue   varchar2(2000);
0W!U;z^N-oy0 12      l_status        integer;
~g,]M"h|!p\0 13      l_colCnt        number default 0;
7~u_*kb1~0 14      l_separator     varchar2(10) default '';
9ozi*G/EE0 15      l_cnt           number default 0;
| [ ?$h2tS\G0 16  beginITPUB个人空间 ^j7R;dB"RM:L b
 17      l_output := utl_file.fopen( p_dir, p_filename, 'w' );
p(g_;~ wJw'W(z0 18 ITPUB个人空间wm.vdn
 19      dbms_sql.parse(  l_theCursor,  p_query, dbms_sql.native );ITPUB个人空间~5wt U!\&W~O-y:f h
 20 ITPUB个人空间:OvX/A1p8So
 21      for i in 1 .. 255 loop
-L6mpL"bf z0 22          begin
3LI-Wc"~ UYV8^B!m0 23              dbms_sql.define_column( l_theCursor, i,
a^vL"j\*v0 24                                      l_columnValue, 2000 );
T6k,lX2k/G8Ms0 25              l_colCnt := i;ITPUB个人空间.j;B9e Wt V3]
 26          exception
N3?1](i,M;j!fD0 27              when others thenITPUB个人空间$A(p@}!h"Xw3Kw
 28                  if ( sqlcode = -1007 ) then exit;ITPUB个人空间:? D'T)[7K1SQ%B
 29                  else
3rxG3c5W U.C-L0 30                      raise;
:U)f9Y#_ d.O[.A5D0 31                  end if;ITPUB个人空间\6Co@7@ N+m
 32          end;ITPUB个人空间g)Aez1j-Z
 33      end loop;
,gu f [Ca!vO3v0 34 
u8m6dD.|$e o b0 35      dbms_sql.define_column( l_theCursor, 1, l_columnValue,
}"Ub/\uRw0 36                              2000 );
3\Y7K*Pa3A0 37 ITPUB个人空间?3|]*M%a
 38      l_status := dbms_sql.execute(l_theCursor);ITPUB个人空间"Ra+H;];X
 39 ITPUB个人空间gG%^i'F)]M7?
 40      loopITPUB个人空间QK L nl y:n\&]
 41          exit when ( dbms_sql.fetch_rows(l_theCursor) <= 0 );
#`Bv3fl(G rU/u6_b5n|9G0 42          l_separator := '';
:H4j@x-H m }0 43          for i in 1 .. l_colCnt loopITPUB个人空间4l)qX'zJ*O
 44              dbms_sql.column_value( l_theCursor, i,ITPUB个人空间dC f(D;u-d
 45                                     l_columnValue );
.OM%}k9}fV,o)X[0 46              utl_file.put( l_output, l_separator ||
UL"nCS#@ S |0 47                                      l_columnValue );ITPUB个人空间 uj3V gd\.b9fG
 48              l_separator := p_separator;ITPUB个人空间(Dx6c'd6y;O Z @{!o
 49          end loop;
4@IP9m^5{2CF7W0 50          utl_file.new_line( l_output );
YVS1}#h_9Bvp3Zb0 51          l_cnt := l_cnt+1;
q%B5{p7r8R B9F0 52      end loop;ITPUB个人空间 og'fk!}`!Op
 53      dbms_sql.close_cursor(l_theCursor);ITPUB个人空间S9z/Q#t] p/n
 54 
q#A{IP+R0 55      utl_file.fclose( l_output );
7O;x+L4Y!iE1q%k0 56      return l_cnt;ITPUB个人空间'r6v$z9x4Sk
 57  end dump_csv;
U9K Up"DEdv4V3^!B G}0 58  /
 
Function created.
 
SQL> create or replace directory tmp as '/tmp';
 
Directory created.
 

{,Oavv Lc0SQL> declareITPUB个人空间 d&n7S`Ws.zuO
  2           l_rows  number;
-Bm:SQg6Pr0  3        beginITPUB个人空间 T7]e+j'h7_/y)Em%L
  4           l_rows := dump_csv( 'select *
N r;B3]#STC.E|-t Oy0  5                                            from all_usersITPUB个人空间`;H$e VwBA@O!P t
  6                                          where rownum < 5',ITPUB个人空间9Y3{3[%m \0~)}7H
  7                                        ' , ' , 'TMP', 'test.dat' );
+_+oY1}v\dxD0  8        end;ITPUB个人空间6X"`v(O!A"f
  9  /
 
PL/SQL procedure successfully completed.
 

WWCkR']0[oracle@rac2 tmp]$ pwdITPUB个人空间y N5g,_f
/tmpITPUB个人空间P6~ W+|*uv"G
[oracle@rac2 tmp]$ cat test.datITPUB个人空间IY$\Rm _}7N)?
SYS , 0 , 03-AUG-07
+I7gRS1I+Vv"p&HP0SYSTEM , 5 , 03-AUG-07
,c`1g4_$le(f7h9LG0OUTLN , 9 , 03-AUG-07ITPUB个人空间R2Tg6XB:R)j
DIP , 13 , 03-AUG-07
 
 
 
 
 
 
 

TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-09-07  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 3292
  • 日志数: 649
  • 建立时间: 2007-12-21
  • 更新时间: 2008-09-06

RSS订阅

Open Toolbar