You are on page 1of 19

SQL NOTES

I.DATA DEFINITION LANGUAGE

It is used to define the data base objects.


(A) CREATE
(B) ALTER
(C) DROP

1. How to create a table?


A. create table emp(empno number(3),ename varchar2(10),job varchar2(10),salary number(8),hire_date date);

2. How to create a table from the existing table?


A. create table t1 as select empno,ename,deptno from emp;

3. How to create user?


A. create user hari identified by hari;

4. How to view the table structure?


A. desc emp;

5. How to modify the structure of the table?


A. alter table emp modify address varchar2(25);

6. How to add a column in the existing table?


A. alter table emp add address varchar2 (20);

7. How to drop a column in the table?


A. alter table emp drop column address;

8. How to set the column unused?


A. alter table emp set unused (ename);

9. How to mdrop unused columns?


A. alter table emp drop unused columns;

10. How to drop a table?


A. drop table emp;

11. How to change the table name?


A. rename emp to emp1;
II.DATA MANIPULATION LANGUAGE

It is used to manipulate the information in the data base objects.


(A) INSERT
(B) UPDATE
(C) DELETE

1. How to insert data into the table?


A. insert into emp values(100,'hari','marketing ',8000,sysdate);
2. How to insert data into a particular column?
A. insert into emp(ename,salary) values('raju',7000);

3. How to insert null values into the table?


A. insert into emp values(100, 'hari','marketing ',8000,default);

4. How to update or insert row using single condition?


A. merge into dept d using emp e on(d.deptno=e.deptno)
when matched found then update set d.name=e.ename d.job=e.job d.salary=e.salary
d.deptno=e.deptno
when not matched then insert values(e.ename,e.job,e.salary,e.deptno);

5. How to insert multiple rows in to the table?


A. insert all into emp2 values(empno,ename,job,sal,deptno,comm,hire_date) select * from emp;

6. How to update the columns in the existing table?


A. update emp set hire_date=to_date(‘02/06/05’,’dd/mm/yyyy’) where empno=102;

7. How to update columns more than one?


A. update emp set ename=’manoj’,salary=7000 where empno=108;

8. How to delete or ‘truncating’ a rows in the table?


A. delete from emp; [If we use this condition total rows will be deleted]
truncate table emp;

9 How to delete a particular row in the table?


A. delete from emp where empno=100;

10. How to copy rows from one table to other table?


A. insert into emp1(no,name,job,salary) select empno,ename,job,salary from emp;
insert into emp1 select * from emp;
III.DATA CONTROL LANGUAGE

It is used to share information between users


(A) GRANT - It is used to give permission
(B) REVOKE - It is used to cancel the permission

1. How to give permission to another user?


A. grant all on emp to scott1; [all - means select,insert,update,delete]

2. How to give permission only on delete to another user?


A. grant delete on emp to scott1;

3. How to give permission for insert,update,select to another user?


A. grant insert,update,select on emp to scott1;

4. How to give permission to two users?


A. grant all on emp to scott1,scott2;

5. How to give permission to all users?


A. grant insert on emp to public;

6. How to give permission to scott1 to give grant option to other user?


A. grant all on emp to scott1 with grant option;

7. How to give permission on columns?


A. grant insert(empno,ename,hire_date) on emp to scott1;

8. How to cancel permission from the user?


A. revoke all on emp from scott1;

9. How to cancel permission on a particular columns from the user?


A. revoke insert,delete on emp from scott1;

10. How to cancel the permission from all the users?


A. revoke all on emp from public;

11. How to cancel permission on columns?


A. revoke insert on emp from scott1;

12. How to see the permission list given by user to others?


A. desc all_privs_made

13. How to see the permission list received by user from others?
A. desc all_privs_recd
IV.TRANSACTION CONTROL LANGUAGE

It is used to control the DML operation.


(A) COMMIT - It is used to save the data dynamically.
(B) ROLL BACK - It is used to cancel the changes.
(C) SAVEPOINT - It is used to mark the transactions.

1. How to save the changes permanently?


A. commit;

2. How to cancel changes which we made?


A. rollback; [if we use DROP condition it will delete permanently]

3. If we made delete,insert,update,how to cancel only update statements?


A. savepoint s1; [ use after deletion]
savepoint s2; [ use after insertion]
rollback to savepoint s2; [It will cancel only the updates]
commit; [It will save the changes only for delete & insert]

V.DATA QUERYING LANGUAGE

It is used to get the data from data base objects for read only purpose
(A) SELECT

1. How to see the data in the table?


A. select * from emp;

2. How to retrieve specific columns from the table?


A. select empno,ename,salary from emp;

3. How to do arithmetic operations in the select statement?


A. select empno,ename,salary,salary+3000 from emp;

4. How to use alias in the select statement?


A. select empno,ename,salary,salary*12 “annual salary” from emp;

5. How to eliminate the duplicate rows in the select statement?


A. select distinct deptno from emp;

WHERE CLAUSE & SPECIAL OPERATORS

1. How to retrieve a particular column from the table?


A. select * from emp where empno=100;
select * from emp where salary>4000;

2. How to retrieve the data from the table using between condition?
A. select * from emp where salary between 3000 and 7000;
select * from emp where salary not between 3000 and 7000;

3. How to retrieve data from the table using relation condition?


A. select * from emp where job_id=’marketing’ and salary>5000;

4. How to retrieve selected row more than one from the table?
A. select * from emp where empno in(100,105,110);
select * from emp where empno not in(100,105,110);

5. How to retrieve names which is starting with ‘S’ from the table?
A. select * from emp where ename like’s%’;
select * from emp where ename not like’s%’;

6. How to retrieve ‘upper case’ characters?


A. select * from emp where upper(ename) like’A%’;

7. How to retrieve lower case characters?


A. select * from emp where lower(ename) like’a%’;

8. How to retrieve names which is ending with ‘S’ from the table?
A. select * from emp where ename like’%s’;
select * from emp where ename not like’%s’;

9. How to retreive names which is the second letter ‘S’ from the table?
A. select * from emp where ename like’_s%’;
select * from emp where ename not like’_s%’;

10. How to retrieve null values from the table?


A. select * from emp where comm is null;
select * from emp where comm is not null;

11. How to retrieve data belonging two column conditions from the table?
A. select * from emp where job_id=’marketing’ or salary>5000;
select * from emp where ename like’s%’ or salary>5000;

ARITHEMATIC OPERATIONS

1. How to see the absolute value in the select statement?


A. select empno,ename,abs(comm-salary) from emp;

2. How to see the square root of the given number?


A. select sqrt(5) from dual;

3. How to see the power of the given number?


A. select power(4,2) from dual;

4. How to see the modulus of the given number?


A. select mod(6,3) from dual; [It gives the remainder value as output]

5. How to see the exponential value for the given number?


A. select exp(5) from dual;

6. How to see the ‘log’ value?


A. select log(4,3) from dual;
7. How to see the ‘sine’,’cosine’,’tan’ values?
A. select sin(30),cos(45),tan(60) from dual;

ARITHEMATIC FUNCTIONS

1. How to see the whole number for the output value?


A. select round(sin(30)) from dual;
Select round(19.7876) ,round(19.4634),round(19.7872,2) from dual; [Ans: 20 19 19.78]

2. How to use trunc function?


A. select trunc(19.7876),trunc(19.4536),trunc(19.7342,2) from dual; [Ans: 19 19 19.73]

CHARACTER FUNCTIONS

1. How to display the first char as capital?


A. select empno,initcap(ename),job,salary from emp;

2. How to display the string in capital letter?


A. select empno,upper(ename),salary from emp;

3. How to display the string in small letters?


A. select empno,lower(ename),job,deptno from emp;

4. How to display the length of the string in the table?


A. select empno,ename,length(ename) from the emp;

5. How to display the string in reverse?


A. select empno,ename,reverse(ename) from emp;

6. How to display the ascii value of the character in number?


A. select ascii(‘A’),ascii(‘a’) from dual;

7. How to display the number in ascii value?


A. select chr(43),chr(50) from the dual;

8. How to display the left space with * in the select statement?


A. select empno,ename,lpad(sal,6,’*) from emp;

9. How to display the right space with & in the select statement?
A. select empno,ename,rpad(sal,6,’*’) from emp;

10. How to remove the first char in the string?


A. select ename,trim(‘s’ from ename) from emp;

11. How to concatenate ename with a job in the select statement?


A. select ename ||’is a’|| job from emp; [‘is a’ it is a literal string]

12. How to replace string in the table?


A. select ename,replace(ename,’s’,2) from emp;

13. How to translate the characters in the select statement?


A. select empno,ename,translate(ename,’aeiou’,’12345’) from emp;

14. How to display the first five letter of the string in the table?
A. select empno,ename,substr(ename,1,5) from emp;

15. How to see the position of the char?


A. select ename,instr(ename,’a’,1,1) from emp;

DATE FUNCTIONS

1. How to display the sysdate date?


A. select sysdate from dual;

2. How to see how many months happen up to now from joining?


A. select ename,job,months_between(sysdate,hire_date) from emp;

3. How to add months to the hire_date in the select statement?


A. select ename,hire_date,add_months(hire_date,6) from emp;

4. How to display the next day using hire_date?


A. select hire_date,next_day(hire_date,’Friday’) from emp;

5. How to display the last day of the month?


A. select hire_date,last_day(hire_date) from emp;

TIME FUNCTIONS

1. How to see the fractional seconds with date & time of server?
A. select systimestamp from dual;

2. How to see the fractional seconds with date & time of client system?
A. select current_timestamp from dual;

3. How to see the date of client system?


A. select current_date from dual;

4. How to see the system date?


A. select sysdate from dual;

5. How to converts the date into timestamp?


A. select to_timestamp(sysdate) from dual;

CHARACTER FORMATS

1. How we can display the date format in another model?


A. select hire_date,to_char(hire_date,’ddth/mm/yyyy’) from emp;
select hire_date,to_char(hire_date,’dy mon yyyy’) from emp;
select hire_date,to_char(hire_date,’day month year’) from emp;
[special characters are th,nd,st]

GENERAL FUNCTIONS

1. How to select the least value from the given values?


A. select least(12,09,4,20,6,46) from dual;

2. How to select greatest value from the given values?


A. select greatest(12,09,4,20,6,46) from dual;

3. How to select minimum & maximum salary from the table?


A. select min(salary),max(salary) from emp;

SPECIAL OPERATORS

1. How to converts a null value in to actual value?


A. select empno,ename,nvl(comm,10) from emp;
select count(nvl(comm,0)) from emp; [if the comm is null then it takes the value as 10]

2. How to use ‘NVL2’ in the select statement?


A. select empno,ename,nvl2(comm,salary+comm,salary) from emp;
[if exp1 is true it displays exp2, if exp1 is false then it displays exp3]

3. How to use ‘NULLIF’ in the select statement?


A. select length(ename),length(job_id),nullif(length(ename),length(job_id)) “result”from emp;
[if the exp1 & exp2 lengths are equal then it displays value as null]

4. How to use ‘COALESCE’ in the select statement?


A. select empno,ename,coalesce(comm,salary,10) from emp; [if exp1 is not null it shows comm, if
exp1 is null it shows sal, if two exp’s are null it shows the value as 10]

5. How to use ‘CASE’ expression in the select statement?


A. select ename,saalary,case job_id when ‘marketing’ then .10*sal when ‘clerk’ then .5*sal
else salary end “revised salary” from emp;

6. How to use ‘DECODE’ expression in the select statement?


A. select ename,salary decode(job_id,’marketing’,.10*sal,’clerk’,.5*sal,sal) “salary” from emp;

OREDER BY CLAUSE

1. How to retreive the data in the ascending order from the table?
A. select * from emp order by deptno;

2. How to retrieve data in the descending order from the table?


A. select * from emp order by empno desc;

GROUP FUNCTIONS

Group functions operate on sets of rows to give one result per group

1. How to select average & sum salary from the table?


A. select avg(salary),sum(salary) from emp;

2. How to select maximum & minimum salary in the table?


A. select max(salary),min(salary) from emp;

3. How to select total no.of row in the table?


A. select count(*) from emp;
select count(empno) from emp;

4. How to display the salary according to deptno?


A. select deptno,avg(salary) from emp group by deptno;

5. How to use ‘HAVING’ clause in group function?


A. select deptno,avg(salary) from emp group by deptno having avg(salary)>5000;

HIERARCHICAL DATA ACCESS

1. Display all the employees name in their relationa hierarchy?


A. select ename from emp start with mgr is null connect by prior empno=mgr;

2. Display all the employees name in their relation hierarchy output should be formatted?
A. select lpad(ename,length(ename)+level) from emp start with mgr is null connect by prior
empno=mgr;

3. Display those employees whose level is odd number?


A. select ename from emp where mod(level,2)!=0 start with mgr is null connect by prior empno=mgr;

4. Display total salary paid by jones to his employees?


A. select sum(sal) from emp where level>1 start with ename=’JONES’ connect by prior empno=mgr;
JOINS

1. How to display data from two tables using ‘cross join’ or ‘cartesian join’?
A. select * from emp,dept;

2. How to display data from two tables using ‘equi join’ or ‘natural join’ condition?
A. select e.empno,e.ename,e.deptno,d.dname from emp e,dept d where e.deptno=d.deptno;
select e.empno,e.ename,e.deptno,d.dname from emp e,dept d where e.deptno=d.deptno
and ename=’hari’;

3. How to join more than two tables?


A. select e.ename,e.deptno,d.dname,d.loc_id,l.address from emp e,dept d,loc l where
e.deptno =d.deptno and d.loc_id=l.loc_id;

4. How to use ‘outer join’ in the select statement?


A. select e.empno,e.ename,e.deptno,d.dname from emp e,dept d where e.deptno(+)=d.deptno;
select e.empno,e.ename,e.deptno,d.dname from emp e,dept d where e.deptno=d.deptno(+);

5. How to retreive two tables using ‘USING’ clause?


A. select e.empno,e.ename,e.deptno,d.dname from emp e,dept d using(deptno);

6. How to retreive two tables using outer joins?


A. select e.empno,e.ename,d.dname from emp e full outer join dept d on(e.deptno=d.deptno);
select e.empno,e.ename,d.dname from emp e left outer join dept d on(e.deptno=d.deptno);
select e.empno,e.ename,d.dname from emp e right outer join dept d on(e.deptno=d.deptno);
7. How to use set operators in joins?
A. select distinct job from emp where deptno=10 union all select distinct job from emp where
deptno=20;
select distinct job from emp where deptno=10 union select distinct job from emp where
deptno=20;
select distinct job from emp where deptno=10 intersect select distinct job from emp where
deptno=20;
select distinct job from emp where deptno=10 minus select distinct job from emp where
deptno=20;

SUB QUERY

Simple Subquery
Correlated Subquery

1. How to display data who is having more than average salary?


A. select empno,ename,job,salary from emp where salary>(select avg(salary) from emp);
select * from emp where deptno=(select deptno from emp where ename=’krishna’);

2. How to display the employees who joined the company before their employees?
A. select * from emp e where e.hiredate>(select m.hiredate from emp m where e.mgr=m.empno);

LOCKS

1. How to lock a row for update or insert?


A. select * from emp where empno=100 for update;
[commit or rollback will release any type of lock]

2. How to lock the table for manipulations?


A. lock table emp in share mode;
lock table emp in share update mode;
lock table emp in exclusive mode;

CONSTRAINTS

1. How to create ‘primary constraint’?


A. create table emp(empno number(2),ename varchar2(10),job varchar2(10),deptno number(3),
constraint emp_deptno_pk primary key(deptno));

2. How to create ‘foreign constraint’?


A. create table dept(deptno number(3),dname varchar2(10),loc number(6),constraint
dept_deptno_fk foreign key(deptno) references emp(deptno);

3. How to delete child table from parent table?


A. create table dept(deptno number(3),dname varchar2(10),loc number(6),constraint
dept_deptno_fk foreign key(deptno) references emp(deptno) on delete cascade;

4. How to add a ‘not null’ in the existing table?


A. alter table emp modify empno not null;

5. How to add a ‘unique constraint’ in the existing table?


A. alter table emp add constraint emp_ename_uk unique(ename);

6. How to add a check salary constraint in the existing table?


A. alter table emp add constraint emp_salary_min check(salary>5000);

7. How to drop constraint?


A. alter table emp drop constraint emp_deptno_pk;

8. How to disable constraint?


A. alter table emp disable constraint emp_salary_min cascade;

9. How to enable constraint?


A. alter table emp enable constraint emp_salary_min cascade;

10. How to view constraint?


A. select owner,constraint_name,constraint_type from user_constraints where table_name=’emp’;
[To see the user constraints the query is : desc user_constraints;]

SQL REPORTING COMMANDS

1. How to set the title in the top of the table?


A. ttitle on;
ttitle ‘employee reports’;

2. How to off the ttitle?


A. ttitle off;

3. How to set the title in the left side?


A. ttitle left ‘employee reports’ skip left;

4. How to set the title in the center?


A. ttitle center ‘employee reports’ skip center;

5. How to set the title in the right side?


A. ttitle right ‘employee reports’ skip right;

6. How to set the title in the bottom of the table?


A. btitle on;
btitle ‘end of the report’

7. How to off the btitle?


A. btitle off;

8. How to change the column name?


A. column empno heading ‘no’;

9. How to change the salary format?


A. column sal heading ‘salary’ format $9,999.99;

10. How to clear column heading?


A. column empno clear;
11. How to break the deptno?
A. break on deptno;

12. How to clear the break command?


A. clear breaks; [it clears all the break command]
clear breaks on deptno;

VIEWS

* It is a stored select statement.


* It will not hold any data.
* Desc,DML,Select allowed in views.
* Any manipulations in view will be reflected in table.
* Providing high security while sharing view with other users.

1. How to create view?


A. create view v1 as select * from emp;

2. How to display the view structure?


A. desc view

3. How to insert values in view?


A. insert into view v1 values(108,’prasad’,’clerk’,7000,40,sysdate);
[these values will be automatically inserted into emp also]

4. How to update values in to view?


A. update v1 set salary=8000 where empno=102;

5. How to delete rows in view?


A. delete from v1 where empno=102;

6. How to drop column in view?


A. no alter option in views;

7. How to drop view?


A. drop view v1;

8. How to create view based on view?


A. create view v3 as select * from v1 where salary>5000;

9. How to create view for read purpose?


A. create view v2 as select * from emp with read only ;
[In this we cannot do any changes if we use read only condition]

10. How to create view with out table?


A. create force view v1 as select * from emp1;
create table emp1 as select * from emp;

11. How to create materialized view?


A. create materialized view mv1 as select * from emp; (OR)
create snapshot s1 as select * from emp;
[this view will exit if u drop the emp table also,we cannot use DML option]
[we have to create a table with primary key ]

12. How to effect manipulation of emp in materialized view?


A. create materialized view mv1 refresh on commit as select * from emp;
[After manipulation in emp we have to give commit to occur in the view]

13. How to drop materialized view?


A. drop materialized view mv1;
drop snapshot s1;

14. How to create view from more than one table?


A. create view v1 as select empno,ename,e.deptno,dname,loc from emp e,dept d where
e.deptno=d.deptno; [in join view, no DML option]

15. How to create view using ‘with check option’?


A. create view v1 as select * from emp where deptno=30 with check option;
[we cannot insert rows & update the deptno because it violates with check option]

16. How to create a complex view?


A. create view v1(name,max(sal),min(sal),avg(sal)) as select d.dname,max(e.sal),min(e.sal),
avg(e.sal) from emp e,dept d where e.deptno=d.deptno group by d.dname;
[we cannot do any manipulations in a complex view]

ROWNUM

* It is automatically generated when select is performed.


* It is a unique value but not stored permanently.

1. How to retreive the top 5 salaried employees?


A. select rownum,empno,ename,sal from (select rownum,empno,ename,sal from emp order by sal desc)
where rownum<=5;

2. How to retrieve the Nth salaried employee?


A. select empno,ename,job,sal from (select empno,ename,job,sal from emp order by sal desc) group by
rownum,empno,ename,job,sal having rownum=&n;

3. How to retrieve alternate rows?


A. select rownum,empno,ename,job,sal from emp group by rownum,empno,ename,job,sal having
mod(rownum,2)=0;

4. How to delete the duplicate records which is having the same data,empno & ename?
A. delete from emp where rowid not in(select min(rowid) from emp group by empno);

5. How to retrieve the first five rows from the table?


A. select * from emp where rownum<6;

6. How to retrieve the last five rows from the table?


A. select * from emp minus select * from emp where rownum<=10;
select * from emp minus select * from emp where rownum<=(select max(rownum)-5 from emp);

7. How to retrieve a particular row from the table?


A. select * from emp where rownum<6 minus select * from emp where rownum<5;
8. How to see the no.of rows in the table?
A. select max(rownum) from emp;

9. How to delete a row from the table using rownum?


A. delete from emp where rownum<2;
delete from emp where rownum<5;

10. How to update row using rownum?


A. update emp set sal=2500 where rownum<2;

SEQUENCE

It is used to generate number in order.


It is not related to the tables.
It uses two pseudo columns.
-- nextval (holds the next generated value)
--currval (holds the current available value)

1. How to create sequence?


A. create sequence s1
increment by 1
start with 200
maxvalue 210
nocache
nocycle;

2. How to insert into a table using sequence?


A. insert into emp(empno,ename,job,salary,deptno) values(s1.nextval,’ravi’,’employee’,8000,10);

3. How to see the sequence current value?


A. select s1.currval from dual;

4. How to see the sequence next value?


A. select s1.nextval from dual;

5. How to see the structure of sequence?


A. select sequence_name,min_value,max_value,increment_by,last_number from user_sequences;
[to see the user sequence the query is : desc user_sequences;]

6. How to modify sequence?


A. alter sequence s1
increment by 1
maxvalue 220
nocache
nocycle;

7. How to drop the sequence?


A. drop sequence s1;

INDEX
It is mainly used to improve performance which retrieving (or) manipulating data by using indexed
columns in where clause. It is a pointer to locate the address of data.

1. How to create index on emp?


A. create index i1 on emp(empno);
create index i2 on emp(sal,deptno);

2. How to use index?


A. select * from emp where ename=’ram’;
select * from emp where sal=3000 and deptno=20; [the index will be activate in the back ground]

3. How to drop index?


A. drop index idx1;

4. How to see the structure of index?


A. select index_name,index_type,table_owner,table_name,table_type from user_indexes;
[to see the user indexes, query is : desc user_indexes]

SYNONYM

Synonym is nothing but a Alias name to the table.

1. How to create synonym?


A. create synonym s1 for emp; [it is a copy of emp & it is related to emp table]

2. How to drop synonym?


A. drop synonym s1;

SPOOL

1. How to save the statements which we done in SQL in to a folder?


A. spool d:\hari
select * from emp;
spool off

DATABASE UTILITIES

Export the file


Import the file

1. How to export the data into the file?


A. conn system/manager
create user hari identified by hari;
grant connect,resource,dba to hari;
conn scott/tiger
host
D:\oracle\ora90\bin> EXP
Username : scott
Password : tiger
Enter array fetch buffersize:4096> (enter)
Export file:EXP.DMP.DMP> D:\hari.dmp (file name)
(2)U(sers),or (3)T(ables): (2)U> (enter)
export grants (yes/no): yes> (enter)
export table data (yes/no): yes> (enter)
compress extents (yes/no): yes> (enter)
D:\oracle\ora90\bin>exit

2. How to import data from file into database?


A. conn hari/hari
host

D:\oracle\ora90\bin> IMP
Username : hari
Password : hari
Import file:EXP.DMP.DMP> D:\hari.DMP
Enter insert buffer size(minimum is 8192)) 30720> (enter)
List contents of import file only(yes/no) no> (enter)
Ignore create error due to object existence (yes/no) no> (enter)
Import grants(yes/no) yes>
Import table data (yes/no): yes> (enter)
Import entire export file (yes/no): no> (enter)
Username: scott
Enter table(T) or partition(T:P)name or . If done: (enter)

SQL*LOADER

Fixed Flat File


Variable Flat File

1. How to insert data from the file.txt into the database using fixed method?
A. sql>conn hp/hp
sql>create table vtest(name varchar2(10),code varchar2(5));

cmd (run cmd prompt)


C:\> d:
D:\> SQLLDR USERID=HP/HP CONTROL=CONTROL_FXD.TXT
D:\>

Sql> select * from vtest;


HARI_TXT.txt

HARIHYD100
RAVISEC200

CONTROL_FXD.txt

LOAD DATA
INFILE HARI_TXT.TXT
REPLACE
INTO TABLE VTEST
(
NAME POSITION (1:4) CHAR,
ADR POSITION (5:7) CHAR,
CODE POSITION (8:10) CHAR
)

NOTE: These two .txt files should be place in one directory.

2. How to insert data from the file.txt into the database using variable method?
A. sql>conn hp/hp
sql>create table sqld(name varchar2(10),code varchar2(5));

cmd (run cmd prompt)


C:\> d:
D:\> SQLLDR USERID=HP/HP CONTROL=CONTROL_VAR.TXT
D:\>

Sql> select * from sqld;

PRA_TXT.txt

Hari,hyderabad,100
Prasad,secunderabad,200
Teja,bangalore,300
Seshu,vigaz,400

CONTROL_VAR .txt

LOAD DATA
INFILE PRA_TXT.TXT
INTO TABLE SQLD
FIELDS TERMINATED BY ','
(NAME,CODE)

NOTE: These two .txt files should be in the same directory.


UTL_FILE PACKAGE

UTL_FILE R Mode
UTL_FILE W Mode

1. How to send data into file.txt from the database using W mode?
A. sql> create or replace procedure proc1 is
vfileID UTL_FILE.file_type;
vstring varchar2(2000);
cursor c1 is select empno from emp;
c1empno number(4);
begin
vstring:='empno,ename,job,mgr,hiredate,sal,comm,deptno';
vfileID:=UTL_FILE.fopen('D:\','HARI1.DAT','W');
UTL_FILE.Put(vfileid, vstring);
UTL_FILE.New_Line(vfileid);
open c1;
loop
fetch c1 into c1empno;
exit when c1%notfound;
select
EMPNO||','||
ENAME||','||
JOB||','||
MGR||','||
HIREDATE||','||
SAL||','||
NVL(COMM,0)||','||
DEPTNO
INTO
vstring
FROM EMP
WHERE EMPNO=c1empno;
UTL_FILE.Put(vfileid, vstring);
UTL_FILE.New_Line(vfileid);
end loop;
close c1;
UTL_FILE.fclose(vfileID);
exception
when others then
DBMS_OUTPUT.PUT_LINE('SYSTEM ERROR');
end;
/
sql> exec proc1;

D:\hari1.txt (open the file in D:\ directory).

2. How to insert into database from the file.txt using R mode?


A. sql> create table hari(name varchar2(10),code number(4));
sql> create or replace procedure proc2 is
vfileID utl_file.file_type;
vstring varchar2(100);
vcode varchar2(10);
vname varchar2(10);
begin
vfileID:=utl_file.fopen('D:\','pra1.DAT','R');
loop
utl_file.Get_line(vfileID,vstring);
vcode :=substr (vstring,1,2);
vname :=substr(vstring,4);
insert into hari(code,name)
values(vcode,vname);
commit;
end loop;
EXCEPTION
when NO_DATA_FOUND then
utl_file.fclose(vfileID);
end;
/
sql> exec proc2;
sql> select * from hari;

PRA1.DAT

10 Hari
20 Prasad
30 Kiran

You might also like