How to generate .csv file from oracle table data?

Alok picture Alok · Apr 27, 2017 · Viewed 8k times · Source

I have generated a csv file using the below query, but all column data are coming in one column in the csv file. Please help me to separate these column data.

create or replace procedure csv_generate as
CURSOR c1 is SELECT * FROM emp;
v1 c1%rowtype;
z1 VARCHAR2(2000);
z2 VARCHAR2(2000);
f1 utl_file.file_type;
BEGIN
FOR i IN c1
loop
--play(i.ename);
f1:=utl_file.fopen('TEST_DIR1','out.dbf','W');
z1:=i.empno||' '||i.ename||'    '||i.JOB||'   '||i.sal||'  '||i.deptno;
z2:=z2||chr(10)||z1;
utl_file.put_line(f1,z2);
utl_file.fclose(f1);
END loop;
--utl_file.put_line(f1,z2);
END;

Answer

Alok picture Alok · Apr 27, 2017

comma separator worked.

create or replace procedure text_generate as
CURSOR c1 is SELECT * FROM emp;
v1 c1%rowtype;
z1 VARCHAR2(2000);
z2 VARCHAR2(2000);
f1 utl_file.file_type;
BEGIN
f1:=utl_file.fopen('TEST_DIR1','out.xls','W');
FOR I IN C1
loop
z1:=i.empno||','||i.ename||','||i.JOB||','||i.sal||','||i.deptno;
Z2:=Z2||CHR(10)||Z1;
END LOOP;
UTL_FILE.PUT_LINE(F1,Z2);
utl_file.fclose(f1);
END;