I have a business requirement that is looking for the ability to have CSV files placed in a network location. I am using a procedure which uses a cursor to send the data to the required location. But I want to include the column headers along with the data. How can I achieve that?
create or replace PROCEDURE REPORT_FILE (
p_status OUT NUMBER,
p_message OUT VARCHAR2)
IS
file_loc CONSTANT VARCHAR2(64) := 'New_DIR';
fid_out UTL_FILE.FILE_TYPE;
file_out VARCHAR2(58);
file_out_ext VARCHAR2(4):='.DAT';
tmp VARCHAR2(999);
CURSOR c_results is
Select
First_name,Last_name,Phone_number from customer
;
BEGIN
DBMS_OUTPUT.enable (1000000);
DBMS_OUTPUT.put_line (
'START TIME IS ' || TO_CHAR (SYSDATE, 'DD-MON-YYYY HH24:MI:SS'));
insert into customer (select * from customer_data);
----- output filename
file_out := 'New_data' || TO_CHAR(SYSDATE,cs_datefmt)
|| TO_CHAR(SYSDATE,cs_timefmt) ||'.CSV' ;
user_utility.print_line('current quater filename is : ' || file_out );
fid_out:=UTL_FILE.FOPEN(file_loc,file_out,'W');
FOR cur_rec in c_results LOOP
tmp := tmp || '"' || cur_rec .first_name|| '"|';
tmp := tmp || '"' || cur_rec .Last_name || '"|';
tmp := tmp || '"' || cur_rec .Phone_number|| '"|';
UTL_FILE.PUT_LINE(fid_out,tmp);
END LOOP;
user_utility.print_line ('REPORT completed successfully .. ');
COMMIT;
p_status := 0;
user_utility.update_job (loc_prog_name, loc_job_id,'C', sysname);
EXCEPTION
WHEN OTHERS
THEN
loc_errcode := SQLCODE;
loc_errmess := SQLERRM;
user_utility.update_job (loc_prog_name,
loc_job_id,
'I',
sysname);
user_utility.error_handler (loc_job_id,
loc_prog_name,
loc_prog_step,
loc_operation,
loc_table_name,
loc_errcode,
loc_errmess,
loc_note);
p_status := loc_errcode;
p_message := 'REPORT has failed. '|| loc_errmess;
rollback;
DBMS_OUTPUT.put_line ('Error code:' || loc_errcode);
DBMS_OUTPUT.put_line ('Error message:' || loc_errmess);
DBMS_OUTPUT.put_line (
'END TIME IS ' || TO_CHAR (SYSDATE, 'DD-MON-YYYY HH24:MI:SS'));
END;
The csv file is sent to the location but without column headers. I want the column headers as well to be sent.