重庆思庄Oracle、Redhat认证学习论坛

标题: 使用外部表访问操作系统上的文本文件 [打印本页]

作者: 郑全    时间: 2012-7-8 22:15
标题: 使用外部表访问操作系统上的文本文件

比如目前有一个文本文件,名字为emp.dat,目录在/home/oracle/下,内容为:

EMPLOYEE_ID LAST_NAME HIRE_DATE SALARY
198 OConnell 2007/6/21 2600.00
199 Grant 2008/1/13 2600.00
200 Whalen 2003/9/17 4400.00
201 Hartstein 2004/2/17 13000.00
202 Fay 2005/8/17 6000.00
203 Mavris 2002/6/7 6500.00
204 Baer 2002/6/7 10000.00
205 Higgins 2002/6/7 12008.00
206 Gietz 2002/6/7 8300.00
100 King 2003/6/17 24000.00
101 Kochhar 2005/9/21 17000.00
102 De Haan 2001/1/13 17000.00
103 Hunold 2006/1/3 9000.00
104 Ernst 2007/5/21 6000.00
105 Austin 2005/6/25 4800.00
106 Pataballa 2006/2/5 4800.00
107 Lorentz 2007/2/7 4200.00
108 Greenberg 2002/8/17 12008.00
109 Faviet 2002/8/16 9000.00
110 Chen 2005/9/28 8200.00
111 Sciarra 2005/9/30 7700.00
112 Urman 2006/3/7 7800.00
113 Popp 2007/12/7 6900.00
114 Raphaely 2002/12/7 11000.00
115 Khoo 2003/5/18 3100.00
116 Baida 2005/12/24 2900.00
117 Tobias 2005/7/24 2800.00
118 Himuro 2006/11/15 2600.00
119 Colmenares 2007/8/10 2500.00
120 Weiss 2004/7/18 8000.00
121 Fripp 2005/4/10 8200.00
122 Kaufling 2003/5/1 7900.00
123 Vollman 2005/10/10 6500.00
124 Mourgos 2007/11/16 5800.00
125 Nayer 2005/7/16 3200.00
126 Mikkilineni 2006/9/28 2700.00
127 Landry 2007/1/14 2400.00
128 Markle 2008/3/8 2200.00
129 Bissot 2005/8/20 3300.00
130 Atkinson 2005/10/30 2800.00
131 Marlow 2005/2/16 2500.00
132 Olson 2007/4/10 2100.00
133 Mallin 2004/6/14 3300.00
134 Rogers 2006/8/26 2900.00
135 Gee 2007/12/12 2400.00
136 Philtanker 2008/2/6 2200.00
137 Ladwig 2003/7/14 3600.00
138 Stiles 2005/10/26 3200.00
139 Seo 2006/2/12 2700.00
140 Patel 2006/4/6 2500.00
141 Rajs 2003/10/17 3500.00
142 Davies 2005/1/29 3100.00
143 Matos 2006/3/15 2600.00
144 Vargas 2006/7/9 2500.00
145 Russell 2004/10/1 14000.00
146 Partners 2005/1/5 13500.00
147 Errazuriz 2005/3/10 12000.00
148 Cambrault 2007/10/15 11000.00
149 Zlotkey 2008/1/29 10500.00
150 Tucker 2005/1/30 10000.00
151 Bernstein 2005/3/24 9500.00
152 Hall 2005/8/20 9000.00
153 Olsen 2006/3/30 8000.00
154 Cambrault 2006/12/9 7500.00
155 Tuvault 2007/11/23 7000.00
156 King 2004/1/30 10000.00
157 Sully 2004/3/4 9500.00
158 McEwen 2004/8/1 9000.00
159 Smith 2005/3/10 8000.00
160 Doran 2005/12/15 7500.00
161 Sewall 2006/11/3 7000.00
162 Vishney 2005/11/11 10500.00
163 Greene 2007/3/19 9500.00
164 Marvins 2008/1/24 7200.00
165 Lee 2008/2/23 6800.00
166 Ande 2008/3/24 6400.00
167 Banda 2008/4/21 6200.00
168 Ozer 2005/3/11 11500.00
169 Bloom 2006/3/23 10000.00
170 Fox 2006/1/24 9600.00
171 Smith 2007/2/23 7400.00
172 Bates 2007/3/24 7300.00
173 Kumar 2008/4/21 6100.00
174 Abel 2004/5/11 11000.00
175 Hutton 2005/3/19 8800.00
176 Taylor 2006/3/24 8600.00
177 Livingston 2006/4/23 8400.00
178 Grant 2007/5/24 7000.00
179 Johnson 2008/1/4 6200.00
180 Taylor 2006/1/24 3200.00
181 Fleaur 2006/2/23 3100.00
182 Sullivan 2007/6/21 2500.00
183 Geoni 2008/2/3 2800.00
184 Sarchand 2004/1/27 4200.00
185 Bull 2005/2/20 4100.00
186 Dellinger 2006/6/24 3400.00
187 Cabrio 2007/2/7 3000.00
188 Chung 2005/6/14 3800.00
189 Dilly 2005/8/13 3600.00
190 Gates 2006/7/11 2900.00
191 Perkins 2007/12/19 2500.00
192 Bell 2004/2/4 4000.00
193 Everett 2005/3/3 3900.00
194 McCain 2006/7/1 3200.00
195 Jones 2007/3/17 2800.00
196 Walsh 2006/4/24 3100.00
197 Feeney 2006/5/23 3000.00

如何使用外部表把他关联起来?

1.创建一个目录
  create directory exp_dir as '/home/oracle/';

  grant read,write on directory exp_dir to hr;

2.创建外部表

create table extab_emp
     (employee_id number(4),
      last_name varchar2(30),
      hire_date date,
      salary number)
  organization external
  (type ORACLE_LOADER default directory exp_dir
     access parameters
       ( records delimited by newline
         FIELDS TERMINATED BY X'09' 
        (
         EMPLOYEE_ID,
         LAST_NAME ,
         HIRE_DATE DATE "fmYYYY/MM/DD",
         SALARY 
        )
       )
   location ('emp.dat'))
   reject limit unlimited

3.之后,就可以查询这个外部表了.
   select * from extab_emp;






欢迎光临 重庆思庄Oracle、Redhat认证学习论坛 (http://bbs.cqsztech.com/) Powered by Discuz! X3.2