There are many ways to do this. The main task is to make the query
includen the comma separaters).
After that there are no significant troubles (if you are professional!)
1. Spooling the result of the SQL in a spool file with some cosmetics to
mask the heading information and the SQl itself
SQL>spool c:\sql_otput.cvs
SQL> put the SQL here
2. As Steven said to use UTL_FILE package to create the desired output:
-- **************************
-- CSV_INVOICES.PRC Procedure to create text file for an
-- invoice
--
-- Inputs: BGWAPP_INVOICE
-- BGWAPP_INVOICE_LINE
-- BGWAPP_CUSTOMER
-- BGWAPP_PRODUCT
-- BGWAPP_ORDER
--
-- Parameters: in_sequence - file number
-- in_file_name - file name
-- in_customer_id - Customer Number
-- in_invoice_id - Invoice Number
-- in_start_date - Date Range - Start Date
-- in_end_date - Date Range - End Date
--
-- There will be headings at the top
--
-- Invoice Header information
-- Invoice #, Invoice Date, Customer #,
-- Customer Name, Order #, Order Date, Status,
-- Total Lines, Total Amount
--
-- Invoice Detail information
-- Invoice Line #, Product #, Description, Quantity,
-- Unit of Measure, Unit Price, Amount, Status
-- **************************
PROCEDURE CSV_INVOICES
(in_sequence IN NUMBER,
in_file_name IN VARCHAR2,
in_customer_id IN NUMBER,
in_invoice_id IN NUMBER,
in_start_date IN DATE,
in_end_date IN DATE)
is
out_file UTL_FILE.FILE_TYPE;
out_counter INTEGER := 0;
out_line INTEGER := 0;
v_invoice_id number(8);
v_file_name varchar2(40);
out_record varchar2(2000);
CURSOR crs_cust_inv_date (c_cust varchar2, c_inv number, c_start date, c_end date) IS
SELECT a.customer_id,
a.invoice_id,
a.invoice_date,
a.order_id,
d.order_date,
c.name,
a.status,
COUNT(*) total_lines,
SUM(b.amount) total_amount
FROM bgwapp_order d,
bgwapp_customer c,
bgwapp_invoice_line b,
bgwapp_invoice a
WHERE a.customer_id = nvl(c_cust,a.customer_id)
AND a.invoice_id = nvl(c_inv,a.invoice_id)
AND a.invoice_date between
nvl(c_start,a.invoice_date
nvl(c_end,a.invoice_date)
AND a.invoice_id = b.invoice_id
AND a.customer_id = c.customer_id
AND a.order_id = d.order_id(+)
GROUP BY
a.customer_id,
a.invoice_id,
a.invoice_date,
a.order_id,
d.order_date,
c.name,
a.status;
CURSOR crs_invoice_detail IS
SELECT a.invoice_id,
a.invoice_line_id,
a.product_id,
b.name,
a.quantity_invoiced,
a.unit_of_measure,
a.unit_price,
a.amount,
a.status
FROM bgwapp_product b,
bgwapp_invoice_line a
WHERE a.invoice_id = v_invoice_id
and a.product_id = b.product_id;
BEGIN
BEGIN
select machine_port, directory, app_identifier, file_location
into c_mach_port, c_dir, v_identifier, v_location
from ediapp_control;
EXCEPTION
when OTHERS then
htp.p('Error in accessing EDIAPP_CONTROL!! - '||SQLERRM);
goto skip;
END;
--
-- Open Output File
--
v_file_name := in_file_name;
out_file := UTL_FILE.FOPEN ( v_location, v_file_name, 'W' );
out_record := '"File Name","'||v_file_name||'",
UTL_FILE.PUT_LINE ( out_file, out_record );
out_record := '" ",,,,,,,,,,,,,,,,," "';
UTL_FILE.PUT_LINE ( out_file, out_record );
out_record := '"INVOICE #","INVOICE DATE","CUSTOMER #","NAME","ORDER #","ORDER DATE",'||
'"STATUS","TOTAL LINES","TOTAL AMOUNT",'||
'"INVOICE LINE #","PRODUCT #","PRODUCT NAME","QTY","UOM",'||
'"UNIT PRICE","AMOUNT","STATUS"';
UTL_FILE.PUT_LINE ( out_file, out_record );
out_record := '" ",,,,,,,,,,,,,,,,," "';
UTL_FILE.PUT_LINE ( out_file, out_record );
For crs_invoice in crs_cust_inv_date
(in_customer_id, in_invoice_id, in_start_date, in_end_date)
LOOP
--
-- INVOICE HEADER / DETAIL Output Line
--
v_invoice_id := crs_invoice.invoice_id;
out_counter := out_counter + 1;
out_line := 0;
For crs_detail in crs_invoice_detail LOOP
out_record := null;
out_line := out_line + 1;
out_record := to_char(crs_invoice.invoic
to_char(crs_invoice.invoic
to_char(crs_invoice.custom
crs_invoice.name ||'",'||
to_char(crs_invoice.order_
to_char(crs_invoice.order_
crs_invoice.status ||'",'||
to_char(crs_invoice.total_
to_char(crs_invoice.total_
to_char(out_line) ||','||
to_char(crs_detail.product
crs_detail.name ||'",'||
to_char(crs_detail.quantit
crs_detail.unit_of_measure
to_char(crs_detail.unit_pr
to_char(crs_detail.amount)
crs_detail.status ||'"';
UTL_FILE.PUT_LINE ( out_file, out_record );
END LOOP;
END LOOP;
UTL_FILE.FCLOSE ( out_file );
DBMS_OUTPUT.PUT_LINE ( 'Total Invoices: '||to_char(out_counter) );
<< skip >>
null;
EXCEPTION
when UTL_FILE.INVALID_PATH then
DBMS_OUTPUT.PUT_LINE('Inva
htp.p('Invalid Path: '||v_location);
when UTL_FILE.INVALID_MODE then
DBMS_OUTPUT.PUT_LINE('Inva
htp.p('Invalid Mode: '||SQLERRM);
when UTL_FILE.INVALID_FILEHANDL
DBMS_OUTPUT.PUT_LINE('Inva
htp.p('Invalid File Handle: '||v_file_name);
when UTL_FILE.INVALID_OPERATION
DBMS_OUTPUT.PUT_LINE('Inva
htp.p('Invalid Operation: '||SQLERRM);
when UTL_FILE.READ_ERROR then
DBMS_OUTPUT.PUT_LINE('Read
htp.p('Read Error: '||SQLERRM);
when UTL_FILE.WRITE_ERROR then
DBMS_OUTPUT.PUT_LINE('Writ
htp.p('Write Error: '||SQLERRM);
when UTL_FILE.INTERNAL_ERROR then
DBMS_OUTPUT.PUT_LINE('Inte
htp.p('Internal Error: '||SQLERRM);
when OTHERS then
DBMS_OUTPUT.PUT_LINE('Othe
htp.p('Other '||SQLERRM);
END CSV_INVOICES;
3. The most sophisticated solution is to use Oracle Reports 10g (with Reports Server - this is complex, but
very handy and powerful tool).
Oracle Reports 10g will create your report in all formats you want (text, PDF, Excell, XML, HTML)
and will snd via email as attachment the file with only some clicks (but for experienced professional only).
Main Topics
Browse All Topics





by: slightwvPosted on 2007-01-22 at 06:51:40ID: 18365561
Creating the CSV is pretty straight forward. You can use UTL_FILE to write it out to the file system. There may be some problems depending on the number of columns and their data types.
e.com/docs /cd/B19306 _01/win.10 2/ b14310/c h4plsql.ht m#sthref22 8
I've only played with this a little and only with the Word piece but you can use the COM automation to do this (at least the docs say you can).
http://download-east.oracl
I'm not sure if the COM automation is seperatly licensed but my guess is that it will be. You'll need to check into this before you use it in production.