SQL Loader错误 - 由于数据错误而未加载行

时间:2017-09-04 10:32:06

标签: oracle oracle11g sql-loader

我正在尝试使用SQL Loader加载数据但是低于错误。

要加载的表 -

CREATE TABLE TEST_PIPE_SEP (FILE_NAME VARCHAR2(3000), KEY_COL VARCHAR2(4000), DESCR VARCHAR2(100), RUN_DATE DATE );  

我正在使用Shell和SED命令创建动态CTL文件,以便我可以获取当前处理文件并将其作为FILE_NAME列的默认值传递。

#!/bin/ksh  

echo "starting script"   
#cd data  

for i in data/Key_Mismatch_Output_UAT*.csv  
do   

  #echo "$i"  

  filename=`basename "${i}"`   
  echo "$filename"  

  #sed '1d' "$i" >> test.csv   
  sed -e "s/#file_name#/file_name \"${filename}\",/g" test.ctl > new_test_3.ctl  

 sqlldr ERRORS=100000 userid=$USER_CRED control=new_test_3.ctl data=data/$filename silent=all log=data/$filename".log" bad=data/$filename.bad skip=1  

wait   
done   
echo "ending script"  

test.ctl的格式如下所示我在运行时使用SED编辑以传递当前处理文件名 -

LOAD DATA        
CHARACTERSET WE8ISO8859P1        
APPEND        
INTO TABLE TEST_PIPE_SEP        
FIELDS TERMINATED BY ','        
(      
#file_name#     
key_col "trim(:key_col)",  
descr   "trim(:descr)",  
run_date "SYSDATE"  
)  

在SED推荐之后创建的新文件如下 -

LOAD DATA  
CHARACTERSET WE8ISO8859P1  
APPEND  
INTO TABLE TEST_PIPE_SEP  
FIELDS TERMINATED BY ','  
(  
file_name  "Key_Mismatch_Output_UAT.csv.20170804070448.1_LIVE.csv.20170804070448.2_20170804070448.csv",  
key_col "trim(:key_col)",  
descr   "trim(:descr)",  
run_date "SYSDATE"  
)  

CSV文件及其包含 -

Key Columns,Description  

"C"|"G000053929"|"ABCD"|"G000053929"|""|""|"TTA"|""|""|""|""|""|"DRR"|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|"",Not Present in file1   
"C"|"G000053621"|"HGHQ"|"G000053621"|""|""|"CBI"|""|""|""|""|""|"DRR"|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|"",Not Present in file2  
"C"|"G000053929"|"HGHQ"|"G000053929"|""|""|"TTA"|""|""|""|""|""|"DRR"|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|"",Not Present in file2  

我收到以下错误并且无法找到路由导致尝试尽可能多地更改CTL文件但无法加载数据

Table TEST_PIPE_SEP, loaded from every logical record.  
Insert option in effect for this table: APPEND  
   
   Column Name                  Position   Len  Term Encl Datatype        
------------------------------ ---------- ----- ---- ---- ---------------------  
FILE_NAME                           FIRST     *   ,       CHARACTER              
    SQL string for column : "TRIM('Key_20170804070448.csv')"  
KEY_COL                              NEXT     *   ,       CHARACTER              
DESCR                                NEXT     *   ,       CHARACTER              
RUN_DATE                             NEXT     *   ,       CHARACTER              
    SQL string for column : "SYSDATE"  
   

Table TEST_PIPE_SEP:  

  0 Rows successfully loaded.  
  1 Row 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.  

请问我是否需要更多信息。

我正在使用

Oracle Database 11g企业版11.2.0.4.0版 - 64位生产

1 个答案:

答案 0 :(得分:1)

据我所知,使用sqlldr加载常量值时,应使用CONSTANT一词。所以在你的情况下你应该替换

...
FIELDS TERMINATED BY ','  
(file_name "Key_Mismatch_Output_UAT.csv.20170804070448.1_LIVE.csv.20170804070448.2_20170804070448.csv",  
...

...
FIELDS TERMINATED BY ','  
(file_name CONSTANT "Key_Mismatch_Output_UAT.csv.20170804070448.1_LIVE.csv.20170804070448.2_20170804070448.csv",  
...

请让我知道它适合你(与此同时我尝试重现你的表和你的脚本,如果有必要,做更多的测试)

<强>附录

我使用您的数据进行了测试。我确认您应该使用上面建议的CONSTANT关键字。

此外,我认为你应该添加TRAILING NULLCOLS选项,因为你想用SYSDATE加载run_date。所以你应该改变

...
INTO TABLE TEST_PIPE_SEP  
FIELDS TERMINATED BY ','  
(  
...

...
INTO TABLE TEST_PIPE_SEP  
FIELDS TERMINATED BY ','
TRAILING NULLCOLS   
(  
...

CTL用于进行测试:

LOAD DATA  
CHARACTERSET WE8ISO8859P1  
APPEND  
INTO TABLE TEST_PIPE_SEP  
FIELDS TERMINATED BY ','
TRAILING NULLCOLS  
(  
file_name  CONSTANT "Key_Mismatch_Output_UAT.csv.20170804070448.1_LIVE.csv.20170804070448.2_20170804070448.csv",  
key_col "trim(:key_col)",  
descr   "trim(:descr)",  
run_date "SYSDATE"  
) 

日志文件SQLLDR:(我加载了4行,因为我在您的文件中添加了另一个示例行(简称A,B)。

SQL*Loader: Release 11.2.0.2.0 - Production on Mon Sep 4 15:01:12 2017
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Control File:   TEST2.CTL
Character Set WE8ISO8859P1 specified for all input.
Data File:      test2.csv
  Bad File:     test2.bad
  Discard File:  none specified
 (Allow all discards)
Number to load: ALL
Number to skip: 1
Errors allowed: 100000
Bind array:     64 rows, maximum of 256000 bytes
Continuation:    none specified
Path used:      Conventional
Table TEST_PIPE_SEP, loaded from every logical record.
Insert option in effect for this table: APPEND
TRAILING NULLCOLS option in effect
   Column Name                  Position   Len  Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
FILE_NAME                                                 CONSTANT
    Value is 'Key_Mismatch_Output_UAT.csv.20170804070448.1_LIVE.csv.20170804070448.2_20170804070448.csv'
KEY_COL                             FIRST     *   ,       CHARACTER            
    SQL string for column : "trim(:key_col)"
DESCR                                NEXT     *   ,       CHARACTER            
    SQL string for column : "trim(:descr)"
RUN_DATE                             NEXT     *   ,       CHARACTER            
    SQL string for column : "SYSDATE"

Table TEST_PIPE_SEP:
  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.

Space allocated for bind array:                  55296 bytes(64 rows)
Read   buffer bytes: 1048576

Total logical records skipped:          1
Total logical records read:             4
Total logical records rejected:         0
Total logical records discarded:        0

Run began on Mon Sep 04 15:01:12 2017
Run ended on Mon Sep 04 15:01:12 2017

Elapsed time was:     00:00:00.09
CPU time was:         00:00:00.00

示例数据:

Key Columns,Description  
A,B
"C"|"G000053929"|"ABCD"|"G000053929"|""|""|"TTA"|""|""|""|""|""|"DRR"|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|"",Not Present in file1  
"C"|"G000053621"|"HGHQ"|"G000053621"|""|""|"CBI"|""|""|""|""|""|"DRR"|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|"",Not Present in file2  
"C"|"G000053929"|"HGHQ"|"G000053929"|""|""|"TTA"|""|""|""|""|""|"DRR"|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|""|"",Not Present in file2