convert oracle query output to xlsx ( excel ) format in oracle - oracle

I have the query like
select * from table ;
I want to put that result into xlsx format and schedule that query.
I have the issue in how to load oracle query result into xlsx ?

Use Spool to export the query result into csv:
set colsep , -- separate columns with a comma
set pagesize 0 -- No header rows
set trimspool on -- remove trailing blanks
set headsep off -- this may or may not be useful...depends on your headings.
set linesize X -- X should be the sum of the column widths
set numw X -- X should be the length you want for numbers (avoid scientific notation on IDs)
spool myfile.csv
select * from TABLE;
A true xlsx file is a binary file in a MS proprietary format.
sqlplus can only return character strings -- text data. 'spool' only writes what sqlplus returns -- character strings.

Related

How do I export oracle query result without enclosure

I have oracle 12c and trying to export query result to csv or text file but I dont want any enclosure of my data. I have tried SET SQLFORMAT csv which creates csv file but data comes in double quotes then I tried SET SQLFORMAT delimited | but that also comes with double quoted. I also tried SET MARKUP csv on delimeter | quote off it also gave me same result. I dont think MARKUP command works on 12c but it did not give me error. Here is my script:
SET SQLFORMAT delimited | ;
spool 'C:\Temp\MyResults.csv';
select 1 AS Col1, 'Data Line 1' AS Col2 from dual UNION select 2 AS Col1, 'Data Line 2' AS Col2 from dual;
spool off;
This gives me result:
"COL1"|"COL2"
1|"Data Line 1"
2|"Data Line 2"
But I want without double quotes on string data.
COL1|COL2
1|Data Line 1
2|Data Line 2
I would appreciate if someone can give me any poption.
Thanks.
I have the following working with Oracle 19 (client and server on Linux):
SQL> set markup CSV on quote off
SQL> desc t;
Name Null? Type
----------------------------------------- -------- ----------------------------
OBJECT_ID NUMBER
OBJECT_NAME VARCHAR2(128)
SQL> select * from t where rownum=1;
OBJECT_ID,OBJECT_NAME
16,TS$
SQL> set markup csv on quote on
SQL> select * from t where rownum=1;
"OBJECT_ID","OBJECT_NAME"
16,"TS$"
SQL>

script to fetch oracle database table in csv format

I am using this script as below
set colsep ','
set heading on
set headsep on
set pagesize 0
set trimspool off
spool C:\DXC\books11.csv
Select * from test_extract;
spool off
exit
but the problem with this is
ARKO ,1A , 20
ARKO1 ,1B , 20
space is comming after the values of each attribute as per the lenght of the attribute.
required output :
ARKO,1A,20
ARKO1,1B,20
As far as I can tell, no SET command will help.
One option - that helps - is to name all columns you're spooling and concatenate them using desired column separator.
For example, this is what you currently have:
SQL> set colsep ','
SQL> set heading on
SQL> set headsep on
SQL> set pagesize 0
SQL> set trimspool off
SQL> select * From dept;
10,ACCOUNTING ,Zagreb
20,RESEARCH ,DALLAS
30,SALES ,CHICAGO
40,OPERATIONS ,BOSTON
But, if you do it as follows:
SQL> select deptno ||','|| dname ||','|| loc from dept;
10,ACCOUNTING,Zagreb
20,RESEARCH,DALLAS
30,SALES,CHICAGO
40,OPERATIONS,BOSTON
it looks as you wanted. Drawback? You'll have to type all those columns.
Use SQLcl, it's in your SQL Developer bin directory, works like SQLPlus, only better.
It's SQL.exe on Windows for example, but also available as it's own 25mb download on Oracle.com.
set sqlformat csv
spool file.csv
select * from table;
It'll give you exactly what you're asking for.

Multiple columns added to single column when spooling table data to csv

I'm writing a shell script to spool multiple columns in PL/SQL table to CSV. But all the columns are added to a single column in the CSV file. Can't seem to figure out what the problem is.
FILE="x26837a/test.csv"i
sqlplus -s MyConnection << EOF
set heading on;
set pagesize 1000;
set tab on;
column owner format a10;
column parent_object_name format a20;
column sub_object_name format a30;
column object_type format a40;
column invalid_abbr format a50;
column email_flag format 9999999;
set linesize 300;
SPOOL $FILE;
SELECT * FROM NM_STD_TBL WHERE ROWNUM < 10;
SPOOL OFF;
EXIT;
EOF
I believe, if you are using SQL Plus, you will need to do
set colsep ,
so that it creates a CSV for you.

Spool CSV file using BASH Script | Errors with Output in generated CSV

I need to spool the output of tables from db to CSV file.
I checked and referred to How do I spool to a CSV formatted file using SQLPLUS?
But it's not working successfully. What I am getting is only the first 3 columns being separated by , and not the remaining. Others are getting separated by newline.
EDIT : Schema Details of Table being Spooled.
WWID VARCHAR2 (5 Byte)
TIMELOG_DATE DATE
TOTAL_HOURS NUMBER (10,2)
ACTIVITY VARCHAR2 (100 Byte)
SUBACTIVITY VARCHAR2 (100 Byte)
REF_PROJECT_ID VARCHAR2 (30 Byte)
REF_PROJECT_DESC VARCHAR2 (250 Byte)
WORK_REQUEST_ID VARCHAR2 (30 Byte)
EMP_COMMENTS VARCHAR2 (100 Byte)
APPROVER_COMMENTS VARCHAR2 (100 Byte)
Script:
echo "\n>>> ******Data Processing Started at `date '+%d-%m-%Y %T %Z'`******" >>${LOGFILE}
sqlplus $fims_user/$fims_pwd << EOF
set serveroutput on;
set colsep , ;-- separate columns with a comma
set pagesize 0 ;-- No header rows
set trimspool on ;-- remove trailing blanks
set headsep off ;-- this may be useful...depends on your headings.
spool /home/fimsctl/datafiles/outbound/timelog/timelog_file_`date +%y%m%d`.csv
select * from FIMS_OWNER.TIMELOG_EXTRACT_OUTBOUND_T;
commit;
spool off
exit
EOF
echo "\n>>> ******Data Load Completed at `date '+%d-%m-%Y %T %Z'`******" >>${LOGFILE}
echo "End of the script">> ${LOGFILE}
And Output in CSV i am getting is:
SQL> select * from FIMS_OWNER.TIMELOG_EXTRACT_OUTBOUND_T;
iv315,29-DEC-14, 8
DUMMY
REF01
New Ref Project of type CPRM
66
NA
iv315,30-DEC-14, 8
DUMMY
REF01
New Ref Project of type CPRM
66
NA
iv315,31-DEC-14, 8
DUMMY
REF01
New Ref Project of type CPRM
66
NA
That is values are then separated by newline(when seen in wordpad)
Thanks
The defaut linesize is 80 characters, so by default columns will wrap onto new lines when that length will be exceeded. Your 100-byte columns will cause that behaviour. You can add SQL*Plus commands to change that:
-- any number at least as large as the longest possible output
set linesize 1024
set wrap off
select * is generally frowned up as the output can change unexpectedly if the table definition changes - i.e. a column is added - or if the table has columns in different orders in different environments. (Which arguably shouldn't happen with source control, and generally doesn't matter as long as you don't use *. If you list the columns you want explicitly, it isn't much extra work to concatenate them with manually-added separators, e.g:
select wwid
||','|| to_char(timelog_date, 'DD-MON-YY') -- or another format
||','|| total_hours
||','|| activity
||','|| subactivity
... -- etc
from FIMS_OWNER.TIMELOG_EXTRACT_OUTBOUND_T;
That will remove extra whitespace that is currently going to pad all the CSV rows to the same length, and reduce the output file size. The output is then a single column, so the colsep setting isn't relevant any more.

Why does the result in TOAD and SQLPlus differ?

I have this query.
select
dbms_metadata.get_ddl('USER', username) || '/' usercreate
from
dba_users where username = 'NSAGUN';
In TOAD, I get this text. (using SAVE AS TAB DELIMITED)
USERCREATE
CREATE USER "NSAGUN" IDENTIFIED BY VALUES '1EE5F58CB716B194'
DEFAULT TABLESPACE "PIN01"
TEMPORARY TABLESPACE "PINTEMP"
/
But in SQLPlus I only get this:
USERCREATE
--------------------------------------------------------------------------------
CREATE USER "NSAGUN" IDENTIFIED BY VALUES '1EE5F58CB716B194'
DEFAULT T
Why is that? And how can I make the output in SQLPlus the same as in TOAD?
Try using these settings in SQL*Plus before executing the query:
set long 1000000
set longchunk 1000000
set linesize 200
The dbms_metadata.get_ddl function returns a CLOB value and by default SQL*Plus sets the LONG variable to 80 bytes.
SQL> set long 1000000
SQL> set pagesize 0
SQL> SELECT
2 dbms_metadata.get_ddl('USER', 'LALIT') || '/' usercreate
3 from
4 dba_users where username = 'LALIT'
5 /
CREATE USER "LALIT" IDENTIFIED BY VALUES 'S:F10EA8C6778ACE16430E4714FE8C41CFB
2C9E5BC73ADDC503E134EA91AF9;H:076ADC10B6F6540DEEB030DF6C97A752;C6F71E6F6BA0F4BD'
DEFAULT TABLESPACE "USERS"
TEMPORARY TABLESPACE "TEMP"/
SQL>
LONG {80|n}
Set the maximum width (in chars) for displaying and copying LONG values.
SET PAGES[IZE] {14 | n}
Sets the number of rows on each page of output in iSQL*Plus, and the
number of lines on each page of output in command-line and Windows
GUI. You can set PAGESIZE to zero to suppress all headings, page
breaks, titles, the initial blank line, and other formatting
information.

Resources