Wednesday, April 29, 2015
Oracle SQL Loader
SIMPLE CONTROL FILE USING SINGLE DATA FILE
1.create table in scott and give grant to apps then create synonym in app
create table emp3(empno number(10),ename varchar2(10),job varchar2(10),sal number(10,2))
SQL> /
Table created.
SQL> grant all on emp3 to apps;
Grant succeeded.
SQL> conn apps/apps@prod
Connected.
SQL> create public synonym emp3 for scott.emp3;
Synonym created.
SQL> desc emp3;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
EMPNO NUMBER(10)
ENAME VARCHAR2(10)
JOB VARCHAR2(10)
SAL NUMBER(10,2)
2.data file
3.control file
4.RUN CMD THEN GIVE COMMAND AS
D:\ORACLE\PRODDB\8.1.7\BIN> SQLLDR APPS/APPS@PROD
CONTROL =D:\DATA\EMP3.CTL
5.GOTO SQL PROMPT THEN VIEW THE OUT PUT:
SQL> SELECT * FROM EMP3;
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
100 'smith' 'mgr' 7000
200 'ramu' 'clerk' 4000
300 'raju' 'act' 6000
SQL>
Multiple Data Files Into Single Database table using single control file
1.Create second data file:
111,'RAVI','MGR',7000
222,'KIRAN','CLERK',4000
333,'CHANDRA','ASTMGR',10000
save this as EMP4.DAT in same directory
2.Goto control file then assign this file as second infile to control file
controlfile:
load data
infile 'd:\data\emp3.dat'
infile 'd:\data\EMP4.dat'
insert into table emp3
fields terminated by ','
(empno
,ename
,job
,sal
)
3.Again run cmd as above
4.goto backend view data
SQL> select * from emp3;
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
111 'RAVI' 'MGR' 7000
222 'KIRAN' 'CLERK' 4000
333 'CHANDRA' 'ASTMGR' 10000
100 'smith' 'mgr' 7000
200 'ramu' 'clerk' 4000
300 'raju' 'act' 6000
6 rows selected.
Control file using sql functions
1) Assign the sql functions to the columns in Control file
load data
infile 'D:\DATA\emp3.dat'
infile 'D:\DATA\EMP4.dat'
insert
into table emp3
(
empno "S1.NEXTVAL",
ename POSITION(4:7) "UPPER(:ENAME)",
job POSITION(8:10) "INITCAP(:JOB)",
sal POSITION(11:14) "NVL(:SAL*10,0)"
)
into table emp4 when projno!=' '
(
empno "S1.NEXTVAL",
ename POSITION(4:7) "UPPER(:ENAME)",
job POSITION(8:10) "INITCAP(:JOB)",
sal POSITION(11:14) "NVL(:SAL*10,0)",
projno POSITION(15:17)
)
2
AGAIN RUN CMD:
2. THEN VIEW THE DATA IN BACK END AS:
SQL> SELECT * FROM EMP3;
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
192 RAVI Mgr 70000
193 RAJU Mgr 40000
194 CHAN Mgr 10000
186 RAVI Mgr 70000
187 RAJU Mgr 40000
188 CHAN Mgr 10000
6 rows selected.
SQL> SELECT * FROM EMP4;
EMPNO ENAME JOB SAL PROJNO
---------- ---------- ---------- ---------- ----------
195 RAVI Mgr 70000 120
196 RAJU Mgr 40000 121
197 CHAN Mgr 10000 122
189 RAVI Mgr 70000 120
190 RAJU Mgr 40000 121
191 CHAN Mgr 10000 122
6 rows selected.
Using where clause in control file to update data into multiple tables
Multiple data files and Multiple tables
1.Create 2 tables and 2 data files
first is emp3 which is already created
Now created second table
CREATE TABLE EMP4 AS SELECT * FROM EMP3 WHERE 1=2
1.1ALTER TABLE EMP4 ADD PROJNO NUMBER; /*for additing additional column for secondtable */
SQL> DESC EMP4;
Name Null? Type
----------------------------------------------------- -------- ----------------------
EMPNO NUMBER(10)
ENAME VARCHAR2(10)
JOB VARCHAR2(10)
SAL NUMBER(10,2)
PROJNO NUMBER
SQL> GRANT ALL ON EMP4 TO APPS;
Grant succeeded.
SQL> CONN APPS/APPS@PROD
Connected.
SQL> CREATE PUBLIC SYNONYM EMP4 FOR SCOTT.EMP4;
Synonym created.
2.create datafile as emp3
111RAVIMGR7000120
222RAJUMGR4000121
333CHANMGR1000122
3.create data file as emp4
111RAVIMGR7000120
222RAJUMGR4000121
333CHANMGR1000122
4.create control file as
load data
infile 'D:\DATA\emp3.dat'
infile 'D:\DATA\EMP4.dat'
insert
into table emp3
(
empno POSITION(1:3),
ename POSITION(4:7),
job POSITION(8:10),
sal POSITION(11:14)
)
into table emp4 when projno!=' '
(empno POSITION(1:3),
ename POSITION(4:7),
job POSITION(8:10),
sal POSITION(11:14),
projno POSITION(15:17)
)
save;
5.run cmd as usual
6.view the output in backend
SQL> select * from emp3;
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
111 RAVI MGR 7000
222 RAJU MGR 4000
333 CHAN MGR 1000
111 RAVI MGR 7000
222 RAJU MGR 4000
333 CHAN MGR 1000
6 rows selected.
SQL> select * from emp4;
EMPNO ENAME JOB SAL PROJNO
---------- ---------- ---------- ---------- ----------
111 RAVI MGR 7000 120
222 RAJU MGR 4000 121
333 CHAN MGR 1000 122
111 RAVI MGR 7000 120
222 RAJU MGR 4000 121
333 CHAN MGR 1000 122
6 rows selected.
Using commit in control files for each 10 records
1 .we will create a sequence as s1
SQL> CONN SCOTT/TIGER@PROD
Connected.
SQL> CREATE SEQUENCE S1 INCREMENT BY 1 START WITH 100;
Sequence created.
SQL> conn apps/apps@prod
Connected.
2.then we will create control file as
load data
infile 'D:\DATA\emp3.dat'
infile 'D:\DATA\EMP4.dat'
insert
into table emp3
(
empno "S1.NEXTVAL",
ename POSITION(4:7),
job POSITION(8:10),
sal POSITION(11:14)
)
into table emp4 when projno!=' '
(
empno "S1.NEXTVAL",
ename POSITION(4:7),
job POSITION(8:10),
sal POSITION(11:14),
projno POSITION(15:17)
)
then save;
4.then run as usual.
5 .view the data from backend as
SQL> /
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
106 RAVI MGR 7000
107 RAJU MGR 4000
108 CHAN MGR 1000
100 RAVI MGR 7000
101 RAJU MGR 4000
102 CHAN MGR 1000
6 rows selected.
SQL> ed
Wrote file afiedt.buf
1* select * from emp4
SQL> /
EMPNO ENAME JOB SAL PROJNO
---------- ---------- ---------- ---------- ----------
109 RAVI MGR 7000 120
110 RAJU MGR 4000 121
111 CHAN MGR 1000 122
103 RAVI MGR 7000 120
104 RAJU MGR 4000 121
105 CHAN MGR 1000 122
6 rows selected
To Create Commit Statement in cmd Prompt
GOTO RUN CMD AND GIVE COMMAND AS TO FOR EVERY 2 RECORDS
TO SKIP RECORDS FROM LOADING USING SKIP KEYWORDS IN CONTROL FILE
GO TO RUN CMD:THEN GIVE THE COMMAND AS:
Goto backend and view the data:
SQL> SELECT * FROM EMP3;
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
148 CHAN MGR 1000
TO SKIP COLUMNS IN CONTROL FILES USING FILLER KEYWORD
Use the FILLER keyword in control files as:
load data
infile 'D:\DATA\emp3.dat'
infile 'D:\DATA\EMP4.dat'
insert
into table emp3
(
empno "S1.NEXTVAL",
ename POSITION(4:7),
job filler,
sal POSITION(11:14)
)
into table emp4 when projno!=' '
(
empno "S1.NEXTVAL",
ename POSITION(4:7),
job POSITION(8:10),
sal POSITION(11:14),
projno POSITION(15:17)
)
Then goto run cmd give the command as usual:
Then view the data in backend :
SQL> select * from emp3;
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
150 RAVI 7000
151 RAJU 4000
152 CHAN 1000
156 RAVI 7000
157 RAJU 4000
158 CHAN 1000
6 rows selected.
SQL> select * from emp4;
EMPNO ENAME JOB SAL PROJNO
---------- ---------- ---------- ---------- ----------
153 RAVI MGR 7000 120
154 RAJU MGR 4000 121
155 CHAN MGR 1000 122
159 RAVI MGR 1000 120
160 RAJU MGR 4000 121
161 CHAN MGR 1000 122
6 rows selected.
1.create table in scott and give grant to apps then create synonym in app
create table emp3(empno number(10),ename varchar2(10),job varchar2(10),sal number(10,2))
SQL> /
Table created.
SQL> grant all on emp3 to apps;
Grant succeeded.
SQL> conn apps/apps@prod
Connected.
SQL> create public synonym emp3 for scott.emp3;
Synonym created.
SQL> desc emp3;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
EMPNO NUMBER(10)
ENAME VARCHAR2(10)
JOB VARCHAR2(10)
SAL NUMBER(10,2)
2.data file
3.control file
4.RUN CMD THEN GIVE COMMAND AS
D:\ORACLE\PRODDB\8.1.7\BIN> SQLLDR APPS/APPS@PROD
CONTROL =D:\DATA\EMP3.CTL
5.GOTO SQL PROMPT THEN VIEW THE OUT PUT:
SQL> SELECT * FROM EMP3;
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
100 'smith' 'mgr' 7000
200 'ramu' 'clerk' 4000
300 'raju' 'act' 6000
SQL>
Multiple Data Files Into Single Database table using single control file
1.Create second data file:
111,'RAVI','MGR',7000
222,'KIRAN','CLERK',4000
333,'CHANDRA','ASTMGR',10000
save this as EMP4.DAT in same directory
2.Goto control file then assign this file as second infile to control file
controlfile:
load data
infile 'd:\data\emp3.dat'
infile 'd:\data\EMP4.dat'
insert into table emp3
fields terminated by ','
(empno
,ename
,job
,sal
)
3.Again run cmd as above
4.goto backend view data
SQL> select * from emp3;
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
111 'RAVI' 'MGR' 7000
222 'KIRAN' 'CLERK' 4000
333 'CHANDRA' 'ASTMGR' 10000
100 'smith' 'mgr' 7000
200 'ramu' 'clerk' 4000
300 'raju' 'act' 6000
6 rows selected.
Control file using sql functions
1) Assign the sql functions to the columns in Control file
load data
infile 'D:\DATA\emp3.dat'
infile 'D:\DATA\EMP4.dat'
insert
into table emp3
(
empno "S1.NEXTVAL",
ename POSITION(4:7) "UPPER(:ENAME)",
job POSITION(8:10) "INITCAP(:JOB)",
sal POSITION(11:14) "NVL(:SAL*10,0)"
)
into table emp4 when projno!=' '
(
empno "S1.NEXTVAL",
ename POSITION(4:7) "UPPER(:ENAME)",
job POSITION(8:10) "INITCAP(:JOB)",
sal POSITION(11:14) "NVL(:SAL*10,0)",
projno POSITION(15:17)
)
2
AGAIN RUN CMD:
2. THEN VIEW THE DATA IN BACK END AS:
SQL> SELECT * FROM EMP3;
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
192 RAVI Mgr 70000
193 RAJU Mgr 40000
194 CHAN Mgr 10000
186 RAVI Mgr 70000
187 RAJU Mgr 40000
188 CHAN Mgr 10000
6 rows selected.
SQL> SELECT * FROM EMP4;
EMPNO ENAME JOB SAL PROJNO
---------- ---------- ---------- ---------- ----------
195 RAVI Mgr 70000 120
196 RAJU Mgr 40000 121
197 CHAN Mgr 10000 122
189 RAVI Mgr 70000 120
190 RAJU Mgr 40000 121
191 CHAN Mgr 10000 122
6 rows selected.
Using where clause in control file to update data into multiple tables
Multiple data files and Multiple tables
1.Create 2 tables and 2 data files
first is emp3 which is already created
Now created second table
CREATE TABLE EMP4 AS SELECT * FROM EMP3 WHERE 1=2
1.1ALTER TABLE EMP4 ADD PROJNO NUMBER; /*for additing additional column for secondtable */
SQL> DESC EMP4;
Name Null? Type
----------------------------------------------------- -------- ----------------------
EMPNO NUMBER(10)
ENAME VARCHAR2(10)
JOB VARCHAR2(10)
SAL NUMBER(10,2)
PROJNO NUMBER
SQL> GRANT ALL ON EMP4 TO APPS;
Grant succeeded.
SQL> CONN APPS/APPS@PROD
Connected.
SQL> CREATE PUBLIC SYNONYM EMP4 FOR SCOTT.EMP4;
Synonym created.
2.create datafile as emp3
111RAVIMGR7000120
222RAJUMGR4000121
333CHANMGR1000122
3.create data file as emp4
111RAVIMGR7000120
222RAJUMGR4000121
333CHANMGR1000122
4.create control file as
load data
infile 'D:\DATA\emp3.dat'
infile 'D:\DATA\EMP4.dat'
insert
into table emp3
(
empno POSITION(1:3),
ename POSITION(4:7),
job POSITION(8:10),
sal POSITION(11:14)
)
into table emp4 when projno!=' '
(empno POSITION(1:3),
ename POSITION(4:7),
job POSITION(8:10),
sal POSITION(11:14),
projno POSITION(15:17)
)
save;
5.run cmd as usual
6.view the output in backend
SQL> select * from emp3;
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
111 RAVI MGR 7000
222 RAJU MGR 4000
333 CHAN MGR 1000
111 RAVI MGR 7000
222 RAJU MGR 4000
333 CHAN MGR 1000
6 rows selected.
SQL> select * from emp4;
EMPNO ENAME JOB SAL PROJNO
---------- ---------- ---------- ---------- ----------
111 RAVI MGR 7000 120
222 RAJU MGR 4000 121
333 CHAN MGR 1000 122
111 RAVI MGR 7000 120
222 RAJU MGR 4000 121
333 CHAN MGR 1000 122
6 rows selected.
Using commit in control files for each 10 records
1 .we will create a sequence as s1
SQL> CONN SCOTT/TIGER@PROD
Connected.
SQL> CREATE SEQUENCE S1 INCREMENT BY 1 START WITH 100;
Sequence created.
SQL> conn apps/apps@prod
Connected.
2.then we will create control file as
load data
infile 'D:\DATA\emp3.dat'
infile 'D:\DATA\EMP4.dat'
insert
into table emp3
(
empno "S1.NEXTVAL",
ename POSITION(4:7),
job POSITION(8:10),
sal POSITION(11:14)
)
into table emp4 when projno!=' '
(
empno "S1.NEXTVAL",
ename POSITION(4:7),
job POSITION(8:10),
sal POSITION(11:14),
projno POSITION(15:17)
)
then save;
4.then run as usual.
5 .view the data from backend as
SQL> /
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
106 RAVI MGR 7000
107 RAJU MGR 4000
108 CHAN MGR 1000
100 RAVI MGR 7000
101 RAJU MGR 4000
102 CHAN MGR 1000
6 rows selected.
SQL> ed
Wrote file afiedt.buf
1* select * from emp4
SQL> /
EMPNO ENAME JOB SAL PROJNO
---------- ---------- ---------- ---------- ----------
109 RAVI MGR 7000 120
110 RAJU MGR 4000 121
111 CHAN MGR 1000 122
103 RAVI MGR 7000 120
104 RAJU MGR 4000 121
105 CHAN MGR 1000 122
6 rows selected
To Create Commit Statement in cmd Prompt
GOTO RUN CMD AND GIVE COMMAND AS TO FOR EVERY 2 RECORDS
TO SKIP RECORDS FROM LOADING USING SKIP KEYWORDS IN CONTROL FILE
GO TO RUN CMD:THEN GIVE THE COMMAND AS:
Goto backend and view the data:
SQL> SELECT * FROM EMP3;
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
148 CHAN MGR 1000
TO SKIP COLUMNS IN CONTROL FILES USING FILLER KEYWORD
Use the FILLER keyword in control files as:
load data
infile 'D:\DATA\emp3.dat'
infile 'D:\DATA\EMP4.dat'
insert
into table emp3
(
empno "S1.NEXTVAL",
ename POSITION(4:7),
job filler,
sal POSITION(11:14)
)
into table emp4 when projno!=' '
(
empno "S1.NEXTVAL",
ename POSITION(4:7),
job POSITION(8:10),
sal POSITION(11:14),
projno POSITION(15:17)
)
Then goto run cmd give the command as usual:
Then view the data in backend :
SQL> select * from emp3;
EMPNO ENAME JOB SAL
---------- ---------- ---------- ----------
150 RAVI 7000
151 RAJU 4000
152 CHAN 1000
156 RAVI 7000
157 RAJU 4000
158 CHAN 1000
6 rows selected.
SQL> select * from emp4;
EMPNO ENAME JOB SAL PROJNO
---------- ---------- ---------- ---------- ----------
153 RAVI MGR 7000 120
154 RAJU MGR 4000 121
155 CHAN MGR 1000 122
159 RAVI MGR 1000 120
160 RAJU MGR 4000 121
161 CHAN MGR 1000 122
6 rows selected.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment