2

I am using the following spooling script with Oracle SQL Developer 4.1:

set echo off
set feedback off
set termout off

spool A.txt
select /*csv*/ * from A where rownum <= 1000;
spool off

spool B.txt
select /*csv*/ * from B where rownum <= 1000;
spool off

...

But the spooled output files contain a blank line at the beginning.

This is the same problem detailed in sqlplus spooling: How to get rid of first, empty line?. I tried using SET NEWPAGE NONE but, as of SQL Developer 4.1.2, this only results in an error message and no change in the output format:

SP2-0158: unknown SET option "newpage"

Is there any way to suppress the output of this first, empty line in SQL Developer?

3 Answers 3

1

two things:

  1. it's the default SQL*Plus behavior, which we try to emulate 100% as much as possible
  2. there's a bug - we're not supporting SET PAGESIZE 0. if you use this in conjunction with SET TRIMSPOOL ON, you'll lose the blank line(s)

we've got it on the list for the next release

2020 Update

Using Version 20.2 of SQL Developer, your script works as expected

enter image description here

Unfortunately I see the issue in SQLcl (command line version of SQLDev) version 20.2, but it's fixed for 20.3 thanks to feedback from some folks on Twitter earlier this Summer.

Here's what it'll look like in a month or so when SQLcl 20.3 is released

10:38:34 nolog >show version
Oracle SQLDeveloper Command-Line (SQLcl) version: 20.3.0.0 build: 20.3.0.240.1605
10:40:31 nolog >set echo off
10:40:49 nolog >set feedback off
10:40:52 nolog >set termout off
10:40:56 nolog >spool A.txt
10:41:04 nolog >select /*csv*/ * from regions;
"REGION_ID","REGION_NAME"
1,"Europe"
2,"Americas"
3,"Asia"
4,"Middle East and Africa"
10:41:14 nolog >spool off
10:41:19 nolog >exit

Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

c:\SQLDev\sqlcl\20.3-lines\sqlcl\bin>type A.txt
"REGION_ID","REGION_NAME"
1,"Europe"
2,"Americas"
3,"Asia"
4,"Middle East and Africa"

c:\SQLDev\sqlcl\20.3-lines\sqlcl\bin>

4
  • 1
    3.5 years later, it's still not fixed. Commented Mar 19, 2019 at 16:26
  • things may have changed, i'm not sure if we'll ever support pagesize 0, but i just ran the code shown in the question, and there are no extra blanklines in the resulting spool Commented Mar 19, 2019 at 16:35
  • IMHO the bug is still there, as of 2020-08-29, SQL.EXE version SQLcl: Release 20.2.0.0 Production Build: 20.2.0.174.1557 Commented Aug 29, 2020 at 13:23
  • @MarceloFinki I updated my answer, from what i can tell, it works just fine in SQL Developer 20.2, but see the issue in SQLcl, but can confirm it's fixed for 20.3 SQLcl Commented Aug 29, 2020 at 14:50
1

That doesn't seem to be possible; it's been asked before but I don't see any resolution. (Tried with set sqlformat csv instead of the /*csv*/ hint, just in case, but the behaviour is the same).

If you export the query results from the grid as CSV (right-click the grid and choose Export...) then you don't get the blank line, but that's obviously a manual process outside your script, so not really equivalent.

Options seem to be to post-process the file to remove the blank line, export from the grid, or run your script using SQL*Plus instead. None of which seem ideal.

1
1

As thatjeffsmith kindly noted, hopefully this bug is fixed in the next release. Until then, you can accomplish the desired results using SQLcl (the modern SQL*Plus included with and used by SQLDeveloper 4.1.2) in conjunction with a SED script.

SQLcl is located here: $SQLDEV_HOME\sqldeveloper\bin\sql. $SQLDEV_HOME is simply the directory of your installed SQLDeveloper version.

In your example, you are generating two separate output files, A.txt and B.txt, from a single SQL file. My following proposed method will require that this be broken up into two separate SQL files: A.sql and B.sql.

A.sql:

set echo off
set feedback off
set termout off
select /*csv*/ * from A where rownum <= 1000;

B.sql:

set echo off
set feedback off
set termout off
select /*csv*/ * from B where rownum <= 1000;

Below is the one-line contents of my SED script named "remove-first-blank-line.sed":

0,/^$/{//d}

"0," defines that the following command block will only be applied to the first line.

"/^$/" defines the regular expression pattern to match. The "^" is the start of a line and the "$" is the end of line marker, so this pattern matches only lines have absolutely no content, no spaces... nothing. If you want to delete lines that might contain whitespace (tabs, spaces), then you may want to try this SED command: 0,/^[[:space:]]*$/{//d}

"//d" defines that the script is to delete the found matched pattern.

For more details on this SED script see: https://unix.stackexchange.com/questions/75424/remove-only-the-first-blank-line-sed.

So, to remove the first blank line from the output of each SQL file, A.sql and B.sql, they must be executed separately using SQLcl and then the output must be piped from SQLcl through the following SED script as follows:

$SQLDEV_HOME\sqldeveloper\bin\sql -s scott/tiger@XE @A.sql | sed -f remove-first-blank-line.sed > A.txt
$SQLDEV_HOME\sqldeveloper\bin\sql -s scott/tiger@XE @B.sql | sed -f remove-first-blank-line.sed > B.txt

The "-s" is the "silent" option for SQLcl (like SQL*Plus) which ensures that there is no database login information output to cluttter up your output file.

The "|" character pipes all output from SQLcl through the given SED script.

The ">" writes all output of the SED script to your output file "mysql.out". If you need to append more data to this same file using another SQL script, then use ">>" so that you can append data to the file instead of overwriting the file.

NOTE: Yes, you could also use the SED command directly on the command line, but this often requires escaping special characters in different ways depending on which OS you are running on. I find that I am much more likely to be able to re-use my SED scripts across various platforms (e.g., Linux and Windows) when I keep the SED commands in short well-named SED script files.

2
  • Piping through just sed -e '1d' removes the first line. Commented Mar 19, 2019 at 16:30
  • To remove the first line, but only if it is empty: sed '1{/^$/d}' Commented Jan 21, 2021 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.