Monday, July 1, 2013

Write Data in External File from ORACLE Procedure


Create user mytest identified by mytest;

Grant dba to mytest;

CREATE TABLE MYTEST.TABLE1
(
  NUM1  INTEGER
)

Insert some records in table1

Create a file a.txt at c:\test

CREATE OR REPLACE DIRECTORY MYDIR AS 'c:\test'

GRANT WRITE ON DIRECTORY MYDIR TO DBA


CREATE OR REPLACE procedure MYTEST.myexport as
 FILEID UTL_FILE.FILE_TYPE;
    LINE_BUFF VARCHAR2(1000);
BEGIN
    FILEID:= UTL_FILE.FOPEN ('MYDIR', 'a.txt', 'W');
    FOR emprec IN (SELECT * FROM table1) LOOP
        LINE_BUFF :=TO_CHAR (emprec.num1);
        UTL_FILE.PUT_LINE ( FILEID ,LINE_BUFF);
    END LOOP;
END;

/

No comments:

Post a Comment