程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL >> SqlLoader怎樣應用

SqlLoader怎樣應用

編輯:MSSQL

SqlLoader怎樣應用。本站提示廣大學習愛好者:(SqlLoader怎樣應用)文章只能為提供參考,不一定能成為您想要的結果。以下是SqlLoader怎樣應用正文


SQL*Loader(SQLLDR)是Oracle的高速批量數據加載對象。這是一個異常有效的對象,可用於多種立體文件格局向Oralce數據庫中加載數據。明天看了請求了*loader的應用,本身小試了下,記載在這

1、假定要拔出數據的表ftest,字段是(id,username,password,sj)

2、導入表的數據 以txt格局存儲,名為data.txt

 1 f f 2010-8-19
2 f1 f1 2010-8-19
3 f2 f2 2010-8-19
4 f3 f3 2010-8-19
5 f4 f4 2010-8-19

 3、寫掌握文件,格局為ctl,定名為cont.ctl 內容以下:

 load data          
infile 'c:\data.txt'       
insert into table ftest    
fields terminated by " "
(id,username,password,sj)

注:假如表中沒稀有據就用insert,稀有據就用append,刪除舊數據拔出新的數據用replace或truncate

4 在cmd敕令窗口中履行 

sqlldr fyzh/fyzh control=c:\cont.ctl data=c:\data.txt

5 在plsql中檢查表ftest

檢查已勝利拔出。

 從新進修sqlldr

sqlldr導入數據的一個最簡略例子:

load data
infile * --告知sqlldr要加載的數據就包括在掌握文件自己
into table dept --加載到哪一個表
fields terminated by ',' --數據加載情勢應當是逗號分隔的值
(deptno,dname,loc) --所要加載的列
begindata      --告知sqlldr前面的行市要加載到dept表的數據
10,Sales,Virginia
20,Accounting,Virginia
30,Consulting,Virginia
40,Finance,Virginia
create table dept
(deptno number(2) constraint dept_pk primary key,
dname varchar2(14),
loc varchar2(13)
)
sqlldr userid=gwm/gwm@fgisdb control=c:\demol.ctl
select * from dept;
1  10  Sales  Virginia
2  20  Accounting  Virginia
3  30  Consulting  Virginia
4  40  Finance  Virginia

sqlldr導入的四種加載方法:

APPEND :本來的表稀有據 就加在前面
INSERT:裝載空表 假如本來的表稀有據 sqlloader會停滯 默許值
REPLACE :本來的表稀有據 本來的數據會全體刪除
TRUNCATE :指定的內容和replace的雷同 會用truncate語句刪除現存數據

用SQLLDR加載數據的FAQ

1、若何加載定界數據

    1)定界數據即用某個特別字符分隔的數據,能夠用引號括起,這是以後立體文件最多見的數據格局。
    關於定界數據,最經常使用的格局是逗號分隔值格局。采取這類文件格局,數據中的每一個字段與下一個字段用一個逗號分隔。文本串可以用引號括起,如許就串自己包括逗號。假如串還必需包括引號,普通商定是應用兩個引號。加載定界數據,響應的典范掌握文件與後面例子類似,然則fields terminated by子句平日以下指定:

 fields terminated by ',' optionally enclose by '"'

  它指定用逗號分隔數據字段,每一個字段可以用雙引號括起。假如把這個掌握文件的最初部門修正以下:

  fields terminated by ',' optionally enclosed by '"'
  (deptno,dname,loc) 
  begindata     
  10,Sales,"Virginia,USA"
  20,Accounting,"Va,""USA"""
  30,Consulting,Virginia
  40,Finance,Virginia
select * from dept
1  10  Sales  Virginia,USA
2  20  Accounting  Va,"USA"
3  30  Consulting  Virginia 
4  40  Finance  Virginia

2)另外一種經常使用的格局是制表符定界數據。有兩種辦法應用terminated by子句來加載這類數據:

   terminated by X'09' --應用十六進制格局的制表符;若用ASCII,制表符應當是9

 terminated by whitespace
--應用terminated by whitespace
load data
infile *
into table dept
replace
fields terminated by whitespace
(deptno,dname,loc) 
begindata     
10 Sales Virginia 
select * from dept;
1  10  Sales  Virginia
--應用terminated by X'09'
load data
infile *
into table dept
replace
fields terminated by X'09'
(deptno,dname,loc) 
begindata     
10        Sales        Virginia
select * from dept;
1  10  

Sales --由於一旦碰到一個制表符就會輸入一個值。

是以,將10賦給deptno,dname獲得了null,由於在第一個制表符和第二個制表符之間沒稀有據

3)sqlldr的filler症結字應用

如跳過制表符

load data
infile *
into table dept
replace
fields terminated by X'09'
(deptno,dummy1 filler,dname,dummy2 filler,loc) 
begindata     
10        Sales        Virginia
select * from dept;
1  10  Sales  Virginia

2、若何加載固定格局數據

   要加載定寬的固定地位數據,將會在掌握文件中應用position症結字。

load data
infile *
into table dept
replace
(deptno position(1:2),
 dname position(3:16),
 loc position(17:29)
 ) 
begindata     
10Accounting   Virginia,USA
select * from dept;
1  10  Accounting   Virginia,USA

    這個掌握文件沒有應用terminated by子句;而是應用了position來告知sqlldr 字段從哪裡開端,到哪裡停止。
關於position,我們可使用堆疊的地位,可以在記載中往返重復。以下修正dept表:

alter table dept add entire_line varchar(29);

並應用以下掌握文件:

load data
infile *
into table dept
replace
(deptno position(1:2),
 dname position(3:16),
 loc position(17:29),
 entire_line position(1:29)
 ) 
begindata     
10Accounting   Virginia,USA
select * from dept;
1  10  Accounting   Virginia,USA  10Accounting  
Virginia,USA

應用position時,可使用絕對偏移量,也能夠應用相對偏移量。後面的例子應用了相對偏移量,明白指定字段從哪開端,從哪停止,也能夠將
後面的掌握文件改寫以下:

load data
infile *
into table dept
replace
(deptno position(1:2),
 dname position(*:16),
 loc position(*:29),
 entire_line position(1:29)
 ) 
begindata     
10Accounting   
Virginia,USA

    *指導掌握文件得出上一個字段在哪裡停止。是以,在這類情形下,(*:16)與(3:16)是一樣的。留意,掌握文件可以混雜應用絕對地位和相對地位。
別的,應用*表現法時,可以把它與偏移量相加。例如dname從deptno停止以後的;兩個字符開端,可使用(*+2:16),即相當於(5:16).

    position子句中的停止地位必需是數據停止的相對各位置。有時,能夠指定每一個字段的長度更加輕易,特殊是假如這些字段是持續的。采取這類

  方法,只需告知sqlldr:記載從第一個字節開端,然後指定每一個字段的長度。以下:

load data
infile *
into table dept
replace
(deptno position(1) char(2),
 dname position(*) char(14),
 loc position(*) char(13),
 entire_line position(1) char(29)
 ) 
begindata     
10Accounting   Virginia,USA 
select * from dept;

3、若何加載日期

    應用sqlldr加載日期只需在掌握文件中date數據類型,並指定要應用的日期掩碼。這個日期掩碼與數據庫中to_char和to_date中應用的日期掩碼一樣。

  如修正dept表以下:

alter table dept add last_updated date;
load data
infile *
into table dept
replace
fields terminated by ','
(deptno,
 dname,
 loc,
 last_updated date 'dd/mm/yyyy'
 ) 
begindata     
10,Accounting,Virginia,1/5/2000
select * from dept;
1  10  
Accounting  
 Virginia   
 2000-5-1

4、若何應用函數加載數據

假如想確保加載的數據是年夜寫的,可以改寫掌握文件以下:

load data
infile *
into table dept
replace
fields terminated by ','
(deptno,
 dname "upper(:dname)",
 loc "upper(:loc)",
 last_updated date 'dd/mm/yyyy'
 ) 
begindata     
10,Accounting,Virginia,1/5/2000
select * from dept;
1  10  
 ACCOUNTING  
 VIRGINIA    
2000-5-1

以下掌握文件加載數據沒法導入

load data
infile *
into table dept
replace
fields terminated by ','
(deptno,
 dname "upper(:dname)",
 loc "upper(:loc)",
 last_updated date 'dd/mm/yyyy',
 entire_line ":deptno||:dname||:loc||:last_updated"
 ) 
begindata     
10,Accounting,Virginia,1/5/2000

1)TRAILING NULLCOLS的應用:普通默許用的好

    處理辦法,就是應用TRAILING NULLCOLS。如許,假如輸出記載中不存在某一列的數據,sqlldr就會為該列綁定一個null值。

這類情形下,增長TRAILING NULLCOLS會招致綁定變量:entire_line成為null。

load data
infile *
into table dept
replace
fields terminated by ','
TRAILING NULLCOLS
(deptno,
 dname "upper(:dname)",
 loc "upper(:loc)",
 last_updated date 'dd/mm/yyyy',
 entire_line ":deptno||:dname||:loc||:last_updated"
 ) 
begindata     
10,Accounting,Virginia,1/5/2000
select * from dept;
1  10  ACCOUNTING  VIRGINIA  10AccountingVirginia1/5/2000  2000-5-1

2)case在sqlldr中的應用

假定輸出文件中有以下格局的日期:
HH24:MI:SS:只要一個時光;日期時光默許為sysdate
DD/MM/YYYY:只要一個日期,時光默許為午夜0點
HH24:MI:SS DD/MM/YYYY:日期時光都顯式供給

可用以下的掌握文件

load data
infile *
into table dept
replace
fields terminated by ','
TRAILING NULLCOLS
(deptno,
 dname "upper(:dname)",
 loc "upper(:loc)",
 last_updated
 "case 
 when length(:last_updated)>9
 then to_date(:last_updated,'hh24:mi:ss dd/mm/yyyy')
 when instr(:last_updated,':')>0
 then to_date(:last_updated,'hh24:mi:ss')
 else to_date(:last_updated,'dd/mm/yyyy')
 end"
 )
 begindata
10,Sales,Virginia,12:03:03 17/10/2005
20,Accounting,Virginia,02:23:54
30,Consulting,Virginia,01:24:00 21/10/2006
40,Finance,Virginia,17/8/2005
alter session set nls_date_format='dd-mon-yyyy hh24:mi:ss';
select * from dept;

5、若何加載有內嵌換行符的數據

1)用非換行符的其它字符來表現換行符,並在加載時應用一個sql函數用一個CHR(10)調換該文本。

alter table dept add comments varchar2(4000);
--應用以下來加載文本
load data
infile *
into table dept
replace
fields terminated by ','
trailing nullcols
(deptno,
 dname "upper(:dname)",
 loc "upper(:loc)",
 comments "replace(:comments,'\\n',chr(10))" --'\\n'換行符用chr(10)這個取代
)
begindata
10,Sales,Virginia,this is the sales\noffice in Virginia

注:挪用中必需用\\n來表現調換符,而不是\n

2)在infile指令上應用FIX屬性,加載一個定長立體文件。
    應用該辦法,輸出數據必需湧現在定長記載中。關於固定地位的數據,應用FIX屬性就特殊適合,這些文件普通為定長文件。
    別的應用該辦法時,數據必需在內部存儲,不克不及存儲在掌握文件自己。

--掌握文件
load data
infile demo.dat "fix 80" --指定了輸出數據文件demo.dat,這個文件中每一個記載80字節
into table dept
replace
fields terminated by ','
trailing nullcols
(deptno,
 dname "upper(:dname)",
 loc "upper(:loc)",
 comments
)
--數據文件
10,Sales,Virginia,this is the sales\noffice in Virginia            
20,,,Sales,Virginia,this is the sales\noffice in Virginia   
               

注:

  在unix上,行停止標志是\n即CHR(10),而windows nt平台的行停止標志是\r\n即CHR(13)||CHR(10);
  可以在掌握文件中應用trim內置sql函數來完成截斷尾部的空白符

select * from dept;

3)在infile指令在、上應用VAR屬性,加載一個變寬文件,在該文件應用的格局中,每行前幾個字節指定了這一行的長度

--掌握文件
load data
infile demo.dat "var 3" --注解了前三個字節用於記載每行的字節數
into table dept
replace
fields terminated by ','
trailing nullcols
(deptno,
 dname "upper(:dname)",
 loc "upper(:loc)",
 comments
)
--數據文件
05410,Sales,Virginia,this is the sales office in Virginia

注:在unix上換行符只算一個字節,在windows nt上算兩個字節

select * from dept;

4)在infile指令上應用STR屬性,加載一個變寬文件,個中用某個字符序列來表現行停止符,而不是用換行符表現
    STR屬性以十六進制指定,要獲得十六進制串,最輕易的方法就是應用sql和utl_raw來生成十六進制串。如在unix平台,行停止標志是CHR(10),我們的特別字符是一個管道符號(|),則可以寫成:

 select utl_raw.cast_to_raw('|'||chr(10)) from dual;--可見在unix上為x'7C0A'

 在windows上用  

 select utl_raw.cast_to_raw('|'||chr(13)||chr(10)) from dual;--為x'7C0D0A'
--掌握文件
load data
infile demo.dat "str x'7C0D0A'" 
into table dept
replace
fields terminated by ','
trailing nullcols
(deptno,
 dname "upper(:dname)",
 loc "upper(:loc)",
 comments
)
--數據文件
10,Sales,Virginia,this is the sales
office in Virginia|
select * from dept;

6、加載lob數據

1)加載內聯的lob數據。這些lob數據平日內嵌有換行符和其他特別字符

--修正表dept
truncate table dept;
alter table dept drop column comments;
alter table dept add comments clob;
--數據文件
10,Sales,Virginia,this is the sales
office in Virginia|
20,Accounting,Virginia,this is the Accounting
office in Virginia|
30,Consuling,Virginia,this is the Consuling
office in Virginia|
40,Finance,Virginia,"this is the Finance
office in Virginia,it has embedded commas and is
much longer than the other comments filed.If you
feel the need to add double quotes text in here like
this:""you will need to double up those quotes!""to
preserve them in the string. This field keeps going for up to
1000000 bytes (because of the control file definition I used)
or until we hit the magic and of record marker,
the | followed by an end of line - it is right here ->"|
--掌握文件
load data
infile demo.dat "str x'7C0D0A'" 
into table dept
replace
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
(deptno,
 dname "upper(:dname)",
 loc "upper(:loc)",
 comments char(1000000) --sqlldr默許輸出的字段都是char(255)。char(1000000)表現許可輸出多達1000000個字符
)
select * from dept;

2)加載外聯的lob數據。

    須要把包括有一些文件名的數據文件加載在lob中,而不是讓lob數據與構造化數據混在一路。如許就不用應用上述的4種辦法之一來避開輸出數據中
的內嵌換行符成績,而這類情形在年夜量的文本或二進制數據中頻仍湧現。sqlldr稱這類額定的數據文件為lobfile。
    sqlldr還可以支撐加載構造化數據文件。可以告知sqlldr若何從別的一個文件解析lob數據,如許便可以加載個中的一部門作為構造化數據中的每行。
sqlldr稱這類內部援用的文件為龐雜二級數據文件。

    lobfile數據采取以下某種格局:

    定長字段(從lobfile加載字節100到10000);
    定界字段(以某個字符停止,或用某個字符括起);--最多見,以一個文件停止符(EOF)停止
    長度/值對,這是一個邊長字段

--加載數據的表
create table lob_demo
(owner varchar2(255),
time_stamp date,
filename varchar2(255),
data blob)
--假定有一目次,個中包括想要加載到數據庫中的文件。以下為想要加載文件的owner,time_stamp,文件名及文件自己
load data 
infile *
replace
into table lob_demo
(owner position(17:25),
 time_stamp position(44:55) date "Mon DD HH24:MI",
filename position(57:100),
data lobfile(filename) terminated by EOF
)
begindata
-rw-r--r-- 1 tkyte tkyte 1220342 jun 17 15:26 classes12.zip
select owner,time_stamp,filename,dbms_lob.getlength(data) from lob_demo;

3)將lob數據加載到對象列

普通用於加載圖象

 create table image_load(
 id number,
 name varchar2(255),
 image ordsys.ordimage) --起首要懂得ordsys.ordimage類型

加載這類數據的掌握文件以下所示:

load data
infile *
into table image_load
replace
fields terminated by ','
(id,
name,
file_name filler,
image column object
(
 source column object
 (
 localdata lobfile(file_name) terminated by EOF
     nullif file_name='none'
 )
)
)
begindata
1,icons,icons.gif

注:column object告知sqlldr這不是一個列名,而是列名的一部門。

應用的列名是image.source.localdata

select * from image_load
--持續編纂加載出去數據的屬性
begin
 for c in (select * from image_load) loop
  c.image.setproperties;--setproperties是ordsys.ordimage類型供給的辦法,處置圖象自己,並用恰當的值更新對象的其他屬性
 end loop;
end;

額定引見:

應用plsql加載lob數據

create table demo (id int primary key,theclob clob)
create or replace directory dir1 as 'D:\oracle';
SQL> host echo 'hello world!' >d:/oracle/test.txt
declare
 l_clob clob;
 l_bfile bfile;
begin
 insert into demo values (1, empty_clob()) returning theclob into l_clob;
 l_bfile := bfilename('DIR1', 'test.txt');
 dbms_lob.fileopen(l_bfile);
 dbms_lob.loadfromfile(l_clob, l_bfile, dbms_lob.getlength(l_bfile));
 dbms_lob.fileclose(l_bfile);
end;
select dbms_lob.getlength(theclob),theclob from demo;

注:

創立的目次默許為年夜寫DIR1,假如目次寫成dir1就會提醒毛病,假如要想應用混有年夜小寫的目次名,在創立如許的目次時應當帶引號的標識符,以下所示:

create or replace directory "dir2" as 'D:\oracle';

以上內容是小編給年夜家分享的關於SqlLoader怎樣應用的相干材料,願望年夜家愛好。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved