暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 外部表跳过 # 行

askTom 2018-07-26
324

问题描述

我正在使用外部表读取csv文件,该文件的开头有一些行,需要跳过。我该怎么做?

ORGANIZATION EXTERNAL
  (  TYPE ORACLE_LOADER
     DEFAULT DIRECTORY GPC_DATA_CSV_DIR
     ACCESS PARAMETERS 
       ( RECORDS DELIMITED BY NEWLINE
     NOBADFILE NODISCARDFILE NOLOGFILE
     SKIP 1
     FIELDS TERMINATED BY ','
     MISSING FIELD VALUES ARE NULL
             )
     LOCATION (GPC_DATA_CSV_DIR:'simple.csv')
  )
复制


我最终没有跳过任何行,但是当我使用外部表时,我只是过滤掉了第一个字段以 # 开头的那些。

专家解答

那么,过滤掉你的where子句中的值是一个有效的方法。如果你觉得懒惰,你可以把它隐藏在一个视图中:

declare
  handle utl_file.file_type;
begin
  handle := utl_file.fopen ( 'TMP', 'test.txt', 'w' );
  
  utl_file.put_line ( handle, 'load,this,line' );
  utl_file.put_line ( handle, 'and,this,line' );
  utl_file.put_line ( handle, '#not,this,line' );
  utl_file.put_line ( handle, '#and,!this,line' );
  utl_file.fclose ( handle );
end;
/

drop table t cascade constraints purge;
create table t (
  c1 varchar2(10),
  c2 varchar2(10),
  c3 varchar2(10)
) organization external (
  default directory tmp
  location ( 'test.txt' )
);

select * from t;

C1     C2      C3     
load   this    line   
and    this    line   
#not   this    line   
#and   !this   line   

create or replace view vw as 
  select * from t
  where  c1 not like '#%';
  
select * from vw;

C1     C2     C3     
load   this   line   
and    this   line 
复制


但是,如果您确定自己永远不想看到这些行,则可以使表格绕过它们。使用load when子句定义要跳过哪些记录。

这必须使用 = 或!=。不像出去了。您也不能将诸如substr之类的函数应用于列。

所以你需要使用位置表示法。使用 (1:1) 访问该行上的第一个字符。并检查它不是 #:

drop table t cascade constraints purge;
create table t (
  c1 varchar2(10),
  c2 varchar2(10),
  c3 varchar2(10)
) organization external (
  default directory tmp
  access parameters (
    records delimited by newline
    load when ( 1:1 ) != '#' 
  )
  location ( 'test.txt' )
);

select * from t;

C1     C2     C3     
load   this   line   
and    this   line 
复制

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论