测试TOM==SQLLDR加载固定格式数据

本文详细介绍了如何使用SQLLDR的POSITION选项进行数据加载,包括使用绝对偏移量、相对偏移量以及指定列长度的方法,并通过实例演示了不同方式的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果平面文件中的数据格式固定,如NAME1-5字节,CITY6-10字节。这种格式的数据最适合用SQLLDR来处理,加载的速度也最快,要实现这一功能只需要用到SQLLDR的POSITION选项,这个选项是用来指定列的数据位置范围的。

测试开始
会话1:清空测试表,重建控制文件
SQL> select * from dept_load;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 sales          virginia

SQL> truncate table dept_load;

Table truncated.

SQL> 

[oracle@oraclelinux ~]$ ls -ltr dept*
-rw-r--r--  1 oracle oinstall  123 May 14 11:06 dept_load4.ctl
-rw-r--r--  1 oracle oinstall 1548 May 14 11:07 dept_load4.log
[oracle@oraclelinux ~]$ cp dept_load4.ctl dept_load5.ctl
[oracle@oraclelinux ~]$ rm -f dept_load4*
[oracle@oraclelinux ~]$ ls -ltr dept*
-rw-r--r--  1 oracle oinstall 123 May 14 11:17 dept_load5.ctl
[oracle@oraclelinux ~]$ vi dept_load5.ctl

      1 load data
      2 infile *
      3 into table dept_load
      4 replace
      5 (deptno position(1:2),
      6 dname position(3:16),
      7 loc position(17:29)
      8 )
      9 begindata
     10 10accounting virginia,usa
~
~
会话1:加载数据
[oracle@oraclelinux ~]$ sqlldr userid=scott/scott control=dept_load5.ctl

SQL*Loader: Release 10.2.0.1.0 - Production on Mon May 14 11:22:30 2012

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Commit point reached - logical record count 1
[oracle@oraclelinux ~]$ !
bash: syntax error near unexpected token `newline'
[oracle@oraclelinux ~]$ exit
exit

SQL> select * from dept_load;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 accounting vir ginia,usa

SQL> 
测试结束
这个控制文件中没有使用FIELDS TERMINATED BY,使用POSITION来指定列从那开始,从那里结束

关于POSITON选项的另一用法,利用它在记录中来回反复,不过好像没有什么实际意义。

测试开始:

会话1:重建控制文件
[oracle@oraclelinux ~]$ ls -ltr dept*
-rw-r--r--  1 oracle oinstall  151 May 14 11:20 dept_load5.ctl
-rw-r--r--  1 oracle oinstall 1548 May 14 11:22 dept_load5.log
[oracle@oraclelinux ~]$ cp dept_load6.ctl
cp: missing destination file
Try `cp --help' for more information.
[oracle@oraclelinux ~]$ cp dept_load5.ctl dept_load6.ctl
[oracle@oraclelinux ~]$ rm -f dept_load5*
[oracle@oraclelinux ~]$ ls -ltr dept_*
-rw-r--r--  1 oracle oinstall 151 May 14 11:29 dept_load6.ctl
[oracle@oraclelinux ~]$ vi dept_load6.ctl

      1 load data
      2 infile *
      3 into table dept_load
      4 replace
      5 (deptno position(1:2),
      6 dname position(3:16),
      7 loc position(17:29),
      8 entire_line position(1:29)
      9 )
     10 begindata
     11 10accounting virginia,usa
会话1:加载数据
oracle@oraclelinux ~]$ sqlldr userid=scott/scott control=dept_load6.ctl

SQL*Loader: Release 10.2.0.1.0 - Production on Mon May 14 11:33:03 2012

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Commit point reached - logical record count 1
[oracle@oraclelinux ~]$ exit
exit

SQL> select * from dept_load;

    DEPTNO DNAME          LOC           ENTIRE_LINE
---------- -------------- ------------- -----------------------------
        10 accounting vir ginia,usa     10accounting virginia,usa

SQL> 
这里把所有记录放在了一个字段里边
测试结束

关于POSITON选项的另一说明:在用这个选项的时候可以使用,相对便宜量和绝对便宜量,使用绝对全称量得去计算数据的开始结束位置,如果很多列的话,这会让人觉得很烦!
测试相对偏移量开始
会话1:清空测试表,重新控制文件
[oracle@oraclelinux ~]$ vi dept_load7.ctl

      1 load data
      2 infile *
      3 into table dept_load
      4 replace
      5 (deptno position(1:2),
      6 dname position(*:16),
      7 loc position(*:29),
      8 entire_line position(1:29)
      9 )
     10 begindata
     11 10accounting virginia,usa
~
会话1:加载数据
[oracle@oraclelinux ~]$ sqlldr userid=scott/scott control=dept_load7.ctl

SQL*Loader: Release 10.2.0.1.0 - Production on Mon May 14 11:42:13 2012

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Commit point reached - logical record count 1
[oracle@oraclelinux ~]$ 
[oracle@oraclelinux ~]$ exit
exit

SQL> select * from dept_load;

    DEPTNO DNAME          LOC           ENTIRE_LINE
---------- -------------- ------------- -----------------------------
        10 accounting vir ginia,usa     10accounting virginia,usa

SQL> 
可以看到效果和使用绝对偏移量是一样的,这里使用*号来自动计算上一个列结束的位置,另外使用*号时也可以把它和偏移量相加,像这样(*+2:16),这就表示从上一字段结束后在加上两个位置做为新字段的起始位置,新字段结束位置16.
测试偏移量结束

POSITION选项另一说明:POSITION的结束位置必须是数据结束的绝对位置,有些时候可能指定每个字段的长度更省事,特别是字段是连续的情况下。采用这种方式只需要告诉SQLLDR:记录从第1个字节开始,然后指定每个列的长度就可以了,这样不需要计算开始和结束偏移量。

测试指定列长度开始
会话1:清空测试数据,重建控制文件,加载数据
SQL> truncate table dept_load;

Table truncated.

SQL> 1
  1* truncate table dept_load
SQL> !
[oracle@oraclelinux ~]$ ls -ltr dept*
-rw-r--r--  1 oracle oinstall  178 May 14 11:41 dept_load7.ctl
-rw-r--r--  1 oracle oinstall 1628 May 14 11:42 dept_load7.log
[oracle@oraclelinux ~]$ rm -f dept_load7*
[oracle@oraclelinux ~]$ vi dept_load8.ctl

      1 load data
      2 infile *
      3 into table dept
      4 replace
      5 (deptno position(1) char(2),
      6 dname position(*) char(14),
      7 loc position(*) char(13),
      8 entire_line position(1) char(29)
      9 )
     10 begindata
     11 10accounting virginia,usa
[oracle@oraclelinux ~]$ sqlldr userid=scott/scott control=dept_load8.ctl

SQL*Loader: Release 10.2.0.1.0 - Production on Mon May 14 12:16:36 2012

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Commit point reached - logical record count 2
[oracle@oraclelinux ~]$ exit
exit

SQL> select * from dept_load;

    DEPTNO DNAME          LOC           ENTIRE_LINE
---------- -------------- ------------- -----------------------------
        10 accounting vir ginia,usa     10accounting virginia,usa

SQL> 
测试结束





来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15720542/viewspace-723496/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15720542/viewspace-723496/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值