Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6649
  • Last Modified:

SQLPLUS - Format Output to a file.

I am new to SQLPLUS and am trying to format my output to a txt file. I am using SPOOL, however there is lot of other information that it spools to the file that I do not need. And also I am not able to make it to a certain column length.

Example; SELECT 'TABLEEMPL      ', FIRST_NM, ID,'*' from mytable.

I want to have just the records on the file and nothing else. In other words my outfile should look like below. My first field has to be always 15 characters, Second be 20 characters, third 10 characters and last 1 character.



TABLEEMPL      JOHN                111       *
TABLEEMPL      CHRISTOPHER   121       *
TABLEEMPL      AL                    131       *



0
nazzie303
Asked:
nazzie303
  • 2
  • 2
1 Solution
 
SDuttaCommented:
set PAGESIZE 0
set COLSEP ''
set TERMOUT off
set WRAP on
set VERIFY off
set HEADING off
set SHOWMODE off
set feedback off
col first_nm format a20
col id       format 999999999
spool c:\test.log
SELECT 'TABLEEMPL      ', FIRST_NM, ID,'*' from mytable;
spool off
0
 
nazzie303Author Commented:
Close, but I still get extra text inside the output file.
The Output file  has the extra first line showing the sql and then the last line showinf spool off command too. Besides I get extra spaces afer the '*' in the end of each record.

SQL>SELECT 'TABLEEMPL      ', FIRST_NM, ID,'*' from mytable;
TABLEEMPL      JOHN                111       *      
TABLEEMPL      CHRISTOPHER   121       *      
TABLEEMPL      AL                    131       *      
SQL>Spool Off;

How can I eliminate the sql commands out of the output file.
0
 
SDuttaCommented:
Put the set commands along with the select sql into a file say test.sql

Then from OS

sqlplus /nolog
SQL> connect user/pwd@dbname
Enter password:
Connected.
SQL>@test.sql
SQL>exit

OR

sqlplus user/pwd@dbname @test.sql
0
 
nazzie303Author Commented:
Great. This is exactly what I was looking for. My output file has everything in order now.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now