سؤال

Is there a way to add a newline in an sql spool file?
I need the file to be structured in a specific way.

'\n' doesn't seem to work and gets printed literally

thanks,

Update: my SQL file

-- Prepare blank file
SET NEWPAGE 0
SET SPACE 0
SET LINESIZE 250
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET HEADING OFF
SET MARKUP HTML OFF 
SPOOL OFF

-- Create the Employees file 
SPOOL employees.txt;
SELECT (case GENDER
when '1' then 'Mr.'
else 'Mrs.'
end ) ||' '|| 
LNAME ||', '||
FNAME ||' newline character '||
FUNCTION ||', '|| DEPARTMENT
from EMPLOYEES;
SPOOL OFF;

-- EXIT SQL and go back to the .bat
EXIT;

Result would be something like:

Mr. Doe, John
Manager, Finance

هل كانت مفيدة؟

المحلول

You can embed a newline character using its ASCII code (well, not strictly ASCII, but close enough here) using the CHR function, in this case with CHR(10) to represent \n:

SELECT (case GENDER
when '1' then 'Mr.'
else 'Mrs.'
end ) ||' '|| 
LNAME ||', '||
FNAME || CHR(10) ||
FUNCTION ||', '|| DEPARTMENT
from EMPLOYEES;

If you want a carriage return as well, append CHR(13) (\r) too.

As noted in comments, this introduces a blank line between rows as well. The only way to get rid of that, as far as I can tell, is to - somewhat unintuitively - SET RECSEP OFF. The new line seems to make SQL*Plus treat the output as wrapped, which you can also see with the SET WRAP OFF command or with the COLUMN ... TRUNCATE option, both of which suppress the second line.

You might also want to SET TAB OFF, and SET TERMOUT OFF if you only want the data spooled to the file, not to the terminal.


(From before the question was edited with more detail)

If you just want a blank line between the results from two queries, you can use the SQL*Plus PROMPT command, with no arguments:

select dummy from dual;
prompt
select dummy from dual;

From the docs:

PRO[MPT] [text]

where text represents the text of the message you want to display.

Sends the specified message or a blank line to the user's screen. If you omit text, PROMPT displays a blank line on the user's screen.

You can also look at the section on formatting SQL*Plus reports if you have other specific requirements for how the output looks.

If you have a PL/SQL block that is producing output via dbms_output calls you can use NEW_LINE to add a blank line between existing output:

dbms_output.put_line('some text');
dbms_output.new_line;
dbms_output.put_line('some more text');
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top