Oracle Tutorial

UTL_FILE Examples

Example:

CREATE OR REPLACE PROCEDURE cust_file (
url IN VARCHAR2,
file IN VARCHAR2)
IS
test UTL_FILE.FILE_TYPE := UTL_FILE.FOPEN (url, file, 'W');
var VARCHAR2(1500);
BEGIN
FOR rec IN (SELECT c.customer_id, c.customer_name, c.email
FROM customers c )
LOOP
var := rec.customer_id ||','|| rec.customer_name ||','|| rec.email;
UTL_FILE.PUT_LINE (test, var);
END LOOP;
UTL_FILE.FCLOSE (test);
EXCEPTION
WHEN OTHERS THEN UTL_FILE.FCLOSE (test);
END;