Util_file package can be used to perform read and write operations on text file ,Util_file package which is owned by sys, we have to grant execute privileges to user connect To sys user or system
Steps:
Connect sys/password as sysdba whatever you given password at time of installation
Grant execute on utl_file to public;
Create a logical directory (directory alias) in oracle that points to physical directory in filesystem.
However, directory alias can be created only dba (sys or system).so after you connect in as sys,
Create directory alias for oracle folder in c:\ and grant read and write permission to public as follows
Create directory EXPDP_DIR as ‘C:\oracle’
Grant read on directory EXPDP_DIR to public; -- (public or scott)
Grant write on directory EXPDP_DIR to public; -- (public or scott)
Read/Write into the Util_file using subprograms
fopen function , fclose,put_line,get_line
FOR WRITE
create or replace PROCEDURE EMP_CSV AS
CURSOR c_data IS
SELECT empno,ename,job,mgr,TO_CHAR(hiredate,'DD-MON-YYYY') AS hiredate,sal,comm,deptno FROM emp ORDER BY ename;
v_file UTL_FILE.FILE_TYPE;
BEGIN
v_file := UTL_FILE.FOPEN(location => 'EXPDP_DIR',
filename => 'emp_csv.txt',
open_mode => 'w',
max_linesize => 32767);
FOR cur_rec IN c_data LOOP
UTL_FILE.PUT_LINE(v_file,
cur_rec.empno || ',' ||cur_rec.ename || ',' ||
cur_rec.job || ',' ||cur_rec.mgr || ',' ||
cur_rec.hiredate || ',' ||cur_rec.empno || ',' ||
cur_rec.sal || ',' ||cur_rec.comm || ',' ||
cur_rec.deptno);
END LOOP;
UTL_FILE.FCLOSE(v_file);
EXCEPTION
WHEN OTHERS THEN
UTL_FILE.FCLOSE(v_file);
END;
READ THE FILE
create or replace PROCEDURE EMP_CSV_read AS
v_file UTL_FILE.FILE_TYPE;
v_input_buffer varchar2(4000);
BEGIN
DBMS_OUTPUT.PUT_LINE('start');
v_file := UTL_FILE.FOPEN(location => 'EXPDP_DIR',
filename => 'emp_csv.txt',
open_mode => 'R',
max_linesize => 32767);
LOOP
UTL_FILE.get_LINE(v_file,v_input_buffer);
DBMS_OUTPUT.PUT_LINE(v_input_buffer);
END LOOP;
UTL_FILE.FCLOSE(v_file);
DBMS_OUTPUT.PUT_LINE('END');
EXCEPTION
WHEN OTHERS THEN
UTL_FILE.FCLOSE(v_file);
END;
http://download.oracle.com/docs/cd/B14117_01/appdev.101/b10802/u_file.htm#996728
http://srikanthtechnologies.com/blog/utl_file.html
http://hany4u.blogspot.com/search/label/PL%2FSQL%20Codes
Steps:
Connect sys/password as sysdba whatever you given password at time of installation
Grant execute on utl_file to public;
Create a logical directory (directory alias) in oracle that points to physical directory in filesystem.
However, directory alias can be created only dba (sys or system).so after you connect in as sys,
Create directory alias for oracle folder in c:\ and grant read and write permission to public as follows
Create directory EXPDP_DIR as ‘C:\oracle’
Grant read on directory EXPDP_DIR to public; -- (public or scott)
Grant write on directory EXPDP_DIR to public; -- (public or scott)
Read/Write into the Util_file using subprograms
fopen function , fclose,put_line,get_line
FOR WRITE
create or replace PROCEDURE EMP_CSV AS
CURSOR c_data IS
SELECT empno,ename,job,mgr,TO_CHAR(hiredate,'DD-MON-YYYY') AS hiredate,sal,comm,deptno FROM emp ORDER BY ename;
v_file UTL_FILE.FILE_TYPE;
BEGIN
v_file := UTL_FILE.FOPEN(location => 'EXPDP_DIR',
filename => 'emp_csv.txt',
open_mode => 'w',
max_linesize => 32767);
FOR cur_rec IN c_data LOOP
UTL_FILE.PUT_LINE(v_file,
cur_rec.empno || ',' ||cur_rec.ename || ',' ||
cur_rec.job || ',' ||cur_rec.mgr || ',' ||
cur_rec.hiredate || ',' ||cur_rec.empno || ',' ||
cur_rec.sal || ',' ||cur_rec.comm || ',' ||
cur_rec.deptno);
END LOOP;
UTL_FILE.FCLOSE(v_file);
EXCEPTION
WHEN OTHERS THEN
UTL_FILE.FCLOSE(v_file);
END;
READ THE FILE
create or replace PROCEDURE EMP_CSV_read AS
v_file UTL_FILE.FILE_TYPE;
v_input_buffer varchar2(4000);
BEGIN
DBMS_OUTPUT.PUT_LINE('start');
v_file := UTL_FILE.FOPEN(location => 'EXPDP_DIR',
filename => 'emp_csv.txt',
open_mode => 'R',
max_linesize => 32767);
LOOP
UTL_FILE.get_LINE(v_file,v_input_buffer);
DBMS_OUTPUT.PUT_LINE(v_input_buffer);
END LOOP;
UTL_FILE.FCLOSE(v_file);
DBMS_OUTPUT.PUT_LINE('END');
EXCEPTION
WHEN OTHERS THEN
UTL_FILE.FCLOSE(v_file);
END;
http://download.oracle.com/docs/cd/B14117_01/appdev.101/b10802/u_file.htm#996728
http://srikanthtechnologies.com/blog/utl_file.html
http://hany4u.blogspot.com/search/label/PL%2FSQL%20Codes
No comments:
Post a Comment