Question : SQLPlus query output to CSV (Database - Oracle Database 10g installed by RSA Authentication Manager 7)

Hi,

I am using RSA Authentication Manager 7.1(AM 7.1). AM 7.1 stores all the information in Oracle Database. I am trying to store result of my query in csv format using sqlplus. I did some googling and tried few things i got so far..

My SQL query:
--------------------------------------------------------------------------------------------------------------
set colsep ',' pagesize 0 trimspool on linesize 500 feedback off echo off
spool /home/aceadm/myfile.csv
SELECT ip.first_name,ip.last_name,ipd.loginuid,ipd.exuid,ipd.identity_src_key,isrc.name,sd.name as securitydomain,atk.serial_number,atk.token_type,to_char(atk.token_shutdown_date,'MM-DD-YYYY') as expiry
   FROM rsa_rep.ims_principal ip,  
                        rsa_rep.ims_principal_data ipd,  
                  rsa_rep.am_token atk,  
                  rsa_batchrep.am_token_oob ato,  
                  rsa_rep.ims_identity_source isrc,  
                        rsa_rep.ims_security_domain sd  
             WHERE ipd.id = atk.principal_id  
               AND ipd.identity_src_id=isrc.id  
               AND atk.id = ato.am_token_id  
                     AND ipd.owner_id = sd.id  
                     AND ipd.identity_src_key = ip.id(+);
spool off
-------------------------------------------------------------------------------------------

But i am getting following out put to CSV which is bit wired.. (Please refer to attached CSV file.)

My question is how can i modify this query so i can get output in following format:

col 1, col 2, col 3, col 4, col 5, col 6, col 7, col 8, col 9

I also want to remove the query statement in start of CSV file.

Attachments:
 
Output CSV File
 

Answer : SQLPlus query output to CSV (Database - Oracle Database 10g installed by RSA Authentication Manager 7)

the colsep operator in sql*plus seperates columns in the output.  if the datatype is char(50) and only has a single 'a' in it, the spooled output has all 50 characters for the column.

The best way is to concatenate the columns (note, remove the alias for the date column):
SELECT ip.first_name || ',' || ip.last_name|| ',' || ipd.loginuid|| ',' || ipd.exuid
...
|| ',' ||  to_char(atk.token_shutdown_date,'MM-DD-YYYY')

This words as long as the full text doesn't exceed 4000 characters.  If this is possible, you might have to go to a stored procedure/function and use a CLOB.

>>I also want to remove the query statement in start of CSV file.
Based on your set commands you shouldn't be seeing the SELECT is you execute the script and don't copy and paste it into SQL*Plus.

I ran the following code using your SET values and didn't get the SELECT statement.

SQL> @myscript.sql



1:
2:
3:
4:
set colsep ',' pagesize 0 trimspool on linesize 500 feedback off echo off
spool l
select sysdate from dual;
spool off
Random Solutions  
 
programming4us programming4us