|
|
Question : PL/SQL Export table data to .csv file and save; launch Excel inside PL/SQL and load saved .csv file
|
|
|
|
I'm using PL/SQL (not SQL*PLUS).
I want to export the entire table and save it as a .csv file using PL/SQL code. (I running Oracle 9i.) I want to launch Excel and have Excel load the saved .csv file (without having to manually open it in Excel) using PL/SQL code.
I've tried using the SPOOL command, but it appears that SPOOL is a SQL*PLUS-only command since I cannot get it to work.
My existing code deletes all records from the table, then inserts the resulting records from a SELECT query into the table. I'd rather not have to fool around with creating a procedure or a function to do this. I simply want a .csv export to a filename of my choice, then, once it is saved (on my local machine, not the server), I want the code to launch Excel and open the .csv file I just saved. I'd like to add the script to do this following the completion of the sample of my script below.
I've read some comments that this can be done with ODBC or OLE, but the examples given are lengthy and confusing, as are the examples I've seen doing this by creating a procedure. I don't mind using any of these approaches, but I have no idea how to code them, or to set up other Oracle options, if necessary, to get them to work. I do connect to the Oracle database with an existing ODBC connection, if that matters.
It seems like this should be a rather straight-forward task (and certainly not an unusual one), but after reading a number of responses on EE and AskTom, I cannot find anything that works. --------------------------------------- Table name: KSEND Here's an excerpt of the INSERT script:
INSERT INTO KSEND (SELECT DISTINCT f.alpha_5, f.alpha_6, f.ss_num, replace(f.last_name, ',', '') as last_name, replace(f.first_name, ',', '') as first_name, f.middle_initial, f.birth_date, f.gender_cd, f.marital_stat_cd, replace(f.addr_1, ',', '') as addr_1, replace(f.addr_2, ',', '') as addr_2,
--followed by 18 more fields---
FROM employee_copy f, system_lookup r WHERE (f.emp_stat_cd = 'A' AND (f.alpha_6 = 'NORM' OR f.alpha_6 = 'IMPR') AND f.decimal_2 IS NOT NULL));
|
|
|
|
Answer : PL/SQL Export table data to .csv file and save; launch Excel inside PL/SQL and load saved .csv file
|
|
Saving the CSV to a file on the database server's file system is pretty straight forward using UTL_FILE.
You really don't even need the temp table.
Just code the select using string concatinations:
create or replace procedure my_proc is being ...
for i in ( select distinct f.alpha5 || ',' || f.alpha_6 || ',' || f.ss_num ... as myRow from ...) loop utl_file.put_line(i.myRow);
end loop; ... end; /
I don't know of a way to launch Excel straight from PL/SQL. You might be able to create a Java stored procedure to do this.
I'm assuming you have some sort of application interface that will execute the stored procedure to create the CSV. Can that not make a OS call to launch a program?
|
|
|
|