前言:
最近在学习怎么使用Oracle数据库,我目前在做的是互联网金融方面的工作,之前用的一直都是DB2数据库,最近换工作后,新项目用的是oracle数据库。
问题:
先记录问题:使用Oracle数据创建外表所遇到的问题。
因为之前并没有使用过Oracle,更没有接触过外表,在各种百度后,找出来几篇大佬写的文章,写出了建表语句。
过程:
1.首先通过xshell连接数据库
sqlplus sysusr/sysusr@mydb
2.创建文件目录并为用户赋权,并建立文本text.txt。
--创建目录
create or replace directory table_dat_dir as '/home/oracle/gyx';
create or replace directory table_bad_dir as '/home/oracle/gyx';
create or replace directory table_log_dir as '/home/oracle/gyx';
--为用户赋权限
grant read on directory table_dat_dir to etl
grant write on directory table_bad_dir to etl
grant write on directory table_log_dir to etl
text.txt文本内容如下:
20190827,123
20190827,236
3.开始建外表
sqlplus etl/111111@mydb;
create table report.tmp_ext_gyx(
org varchar(15),
name varchar(20)
)
organization external(
type oracle_loader
default directory table_dat_gyx
access parameters
(
records delimited by newline
badfile table_bad_dir:'BAD.bad'
logfile table_log_dir:'LOG.log'
fields terminated by ','
missing field values are null
)
location (table_dat_dir:'test.txt')
)
reject limit unlimited
noparallel
nomonitoring;
4.外表已经创建,查一下
select * from tmp_ext_gyx
成功查询出文件内容
总结:
我只是把正确的代码写了上来,其实在代码运行成功之前还遇到了很多问题
ORA-29913: error in executing ODCIEXTTABLEOPEN callout
ORA-29400: data cartridge error
KUP-00554: error encountered while parsing access parameters
KUP-01005: syntax error
四个错误码出来以下就慌了,好吧,开始寻找解决错误方法之旅,
从CSDN上搜索说是代码中包含了注释类,关键字打错了,仔细一看,果然 fields 打成了firlds,改后重新运行不再报错。
在Oracle社区发现了和这四个错误码有关的问题,这个和数据文件(文中的text.txt)的内容也有关。