oracle表

时间:2017-08-04 16:15:07

标签: oracle csv sql-loader

我正在使用SQL * Loader脚本将值从CSV文件输入到Oracle表。在此表中,存在具有NOT NULL约束的字段。在我的CSV文件中,相应的字段为"",我希望在发生这种情况时将空字符串放入Oracle表中。

这是我的控制文件:

LOAD DATA
    infile 'F:\tar.csv'
    REPLACE
    INTO TABLE tar
    fields terminated by ',' optionally enclosed by '"'
    TRAILING NULLCOLS
    (IDTAR   ,
DATABACKUP   DATE "YYYY-MM-DD",
PAESE ,
R_ELEM  NULLIF (R_ELEM=BLANKS))

这是日志文件中的错误:

ORA-01400:无法插入NULL(“MY_SCHEMA”。“TAR”。“PAESE”)

如何通过提供不同的值来避免错误?

1 个答案:

答案 0 :(得分:1)

您可以apply an SQL operator,例如NVL(:PAESE, 'XXX')。注意在引用字段名称之前的冒号。原地:

LOAD DATA
  infile 'gian.csv'
  REPLACE
  INTO TABLE tar
  fields terminated by ',' optionally enclosed by '"'
  TRAILING NULLCOLS
  (
    IDTAR,
    DATABACKUP  DATE "YYYY-MM-DD",
    PAESE "NVL(:PAESE, 'XXX')",
    R_ELEM  NULLIF (R_ELEM=BLANKS)
  )

使用虚拟表:

创建表tar(   idtar号码,   数据库日期,   paese varchar2(10)not null,   r_elem varchar2(10) );

和CSV,其中第3行和第4行具有nullif()子句的尾随空格:

1,2017-08-01,A,B
2,2017-08-02,C,
3,2017-08-03,,    
4,2017-08-04,"",    

然后使用该控制文件运行:

SQL*Loader: Release 11.2.0.4.0 - Production on Fri Aug 4 19:39:23 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Commit point reached - logical record count 4

并且日志说:

...
   Column Name                  Position   Len  Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
IDTAR                               FIRST     *   ,  O(") CHARACTER
DATABACKUP                           NEXT     *   ,  O(") DATE YYYY-MM-DD
PAESE                                NEXT     *   ,  O(") CHARACTER
    SQL string for column : "NVL(:PAESE, 'XXX')"
R_ELEM                               NEXT     *   ,  O(") CHARACTER
    NULL if R_ELEM = BLANKS


Table TAR:
  4 Rows successfully loaded.
  0 Rows not loaded due to data errors.
  0 Rows not loaded because all WHEN clauses were failed.
  0 Rows not loaded because all fields were null.
...

查询表格显示已加载所有四行:

set null "<null>"
select * from tar;

     IDTAR DATABACKU PAESE      R_ELEM    
---------- --------- ---------- ----------
         1 01-AUG-17 A          B         
         2 02-AUG-17 C          <null>    
         3 03-AUG-17 XXX        <null>    
         4 04-AUG-17 XXX        <null>    

显然将'XXX'替换为您要使用的实际默认字符串。您说'空字符串',因此您可以使用"NVL(:PAESE, ' ')"插入单个空格字符。但是,您不能使用 empty 字符串,因为就Oracle而言,它与null相同。