【问题标题】:SQL Loader data insert: can we use default value as another column value?SQL Loader 数据插入:我们可以使用默认值作为另一个列值吗?
【发布时间】:2018-08-17 18:37:39
【问题描述】:

我正在尝试使用 SQL*Loader 导入数据。在导入期间,如果 col1 值为空白,我需要将其替换为 col0 值。像这些我需要为多个列设置默认值。

我尝试通过添加长度检查将其设置为DEFAULTIF col1=col0。假设 col1 值为空,它的长度将为 0,它将使用执行defaultif 条件。但它给出错误说

需要带引号的字符串或十六进制标识符,找到“col0”。

有人可以帮忙解释一下如何将默认值设置为另一个列值吗?

【问题讨论】:

    标签: oracle sql-loader


    【解决方案1】:

    一个简单的NVL 函数调用就可以完成这项工作。

    这是一个例子:

    SQL> create table test (col0 number, col1 varchar2(10), col2 number);
    
    Table created.
    

    控制文件:注意col2 "nvl(:col2, :col0)",如果col2 不存在,它将把col0 值放入col2。在我的示例中,它是第二行 2,yyy,,它不包含 col2 的值,因此它将填充 col0 值,即 2

    load data 
    infile *
    replace
    into table test
    fields terminated by ","
    trailing nullcols
    (
    col0,
    col1,
    col2 "nvl(:col2, :col0)"
    )
    
    begindata
    1,xxx,111
    2,yyy,
    3,zzz,333
    

    加载会话和结果:

    SQL> $sqlldr scott/tiger@xe control=test02.ctl log=test02.log
    
    SQL*Loader: Release 11.2.0.2.0 - Production on Pet Kol 17 21:48:59 2018
    
    Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
    
    Commit point reached - logical record count 2
    Commit point reached - logical record count 3
    
    SQL> select * From test;
    
          COL0 COL1             COL2
    ---------- ---------- ----------
             1 xxx               111
             2 yyy                 2
             3 zzz               333
    
    SQL>
    

    【讨论】:

    • 嗨 Littlefoot,感谢您提供详细信息,我尝试了建议的选项,它工作正常。我有 30 多列,百万行。我可以在@9 分钟内插入所有内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 2019-09-09
    相关资源
    最近更新 更多