Deleting last blank lines in SPOOL with DBMS_OUTPUT - sql

Using SPOOL and PL/SQL in SQL Developer (I must not use SQL Plus via command line) I need to create a file with some info. The problem is that at the end of the file I'm getting 2 blank lines with are causing me trouble when importing this file somewhere else.
Using the next code (called externally to avoid sql statements):
SET SERVEROUTPUT ON;
SET TERMOUT OFF;
SET HEAD OFF;
SET VERIFY OFF;
SET FEEDBACK OFF;
SPOOL 'C:\myfile.csv'
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello');
END;
/
SPOOL OFF;
I'm getting:
Hello
(blank line)
(blank line)
I can handle having 0 or 1 blank line, but not 2. I have tried SET TRIMSPOOL ON but SQL Developer skips this command and others too (TRIM, TRIMS, SQLBLANKLINES)

It really does look like it is not possible to remove that extra CRLF in SQL Developer. I can only suggest using SQLPlus which doesn't add that extra CRLF. If you absolutely can't use that, consider using sed to strip that last line.
For any further discussion and to save future readers' time, here are all the options I tried without success:
SET SERVEROUTPUT ON TERMOUT OFF HEAD OFF FEEDBACK OFF VERIFY OFF
SET ECHO OFF NEWPAGE NONE PAGESIZE 0 RECSEP OFF --TRIMSPOOL ON
SPOOL 'myfile.txt'
--EXEC DBMS_OUTPUT.PUT_LINE('Hello')
SELECT 'Hello' FROM DUAL;
SPOOL OFF
ED myfile.txt

Related

How to have spool file display result of query? (Having trouble)

I am trying to export a .csv file (spool file) that has the result set of a very simple query that I am running in Oracle SQL Developer. The spool file generates; however, only the query is displayed (select * FROM TABLE) with no result set. What am i doing wrong? The command I am using is as follows:
spool "C:\Temp\test.csv"
select * from table;
spool off;
Thanks in advance
Use below commands to get the output of the query in spool file
SET SERVEROUTPUT ON
SET ECHO ON
After executing the select query don't forget to spool off.
So i did some more research / experimenting and i found that the following works:
I first created a sql file with the appropriate sql script /command and placed it in a directory (C:\TEMP). Then i ran the following command:
SET NEWPAGE 0
SET SPACE 0
SET PAGESIZE 0
SET FEEDBACK OFF
SET HEADING OFF
set verify off
SET ECHO OFF
spool "c:\Temp\test.csv"
#c:\Temp\test.sql as script(F5);
spool off
However now i run into a road block where oracle throws me an error saying that only 5,000 rows are currently supposed in script results...
Edit: I created the above code as a .sql file (test2.sql) and ran the below script. But am still encountering the 5000 row error:
SET NEWPAGE 0
SET SPACE 0
SET PAGESIZE 0
SET FEEDBACK OFF
SET HEADING OFF
set verify off
SET ECHO OFF
spool "c:\Temp\test2.csv"
#c:\Temp\test2.sql as script(F5);
spool off
The following worked when I tried to increase the limit: I went up to my sql developer tool bar ( tools > prefs > database > worksheet) and was able to change the maximum output limit.

SQLPLUS script calling a function and executing the return String

I have an sqlplus script which needs to call a function. This function creates an SQL select dynamically and has already been compiled in the database.
My script needs to call this function, executes the SQL request it returns and then spool the data in a CSV file.
My SQLPLUS code is below:
SET HEAD OFF
SET TRIMOUT ON
SET TRIMSPOOL ON
SET LINESIZE 32000
SET PAGESIZE 0
SET TERMOUT OFF
SET ECHO OFF
SET COLSEP ,
spool /a/b/rvibap/c/&1..csv
EXECUTE IMMEDIATE build_select(&1)
spool off;
/
However, I am getting the below error in my CSV file:
BEGIN IMMEDIATE build_select(TYPE); END;
*
ERROR at line 1:
ORA-06550: line 1, column 17:
PLS-00103: Encountered the symbol "BUILD_SELECT" when expecting one of the following:
:= . ( # % ;
The symbol ":=" was substituted for "BUILD_SELECT" to continue.
I am calling my SQL script in the following way :
#test.sql TYPE
I have also tested the build_select function and it functions correctly; return a query in String.
There are few issues with your code.
There is no BEGIN END block enclosing your EXECUTE IMMEDIATE
expression.
EXECUTE IMMEDIATE does not display the results of an sql select statement.
You are calling your execute script as #test.sql TYPE which will be parsed as EXECUTE IMMEDIATE build_select(TYPE) i.e plain TYPE without quotes - which will throw an error. You can resolve this however by running it as
#test.sql "'TYPE'"
As I said you cannot directly execute and view results from a dynamic SQL select statement in 11g and below. So you can use the technique used in the other answer which I am not clear about if it generates the select SQL neatly.
If you are in 12c, you can use something like
open a_ref_cursor for build_select(&1);
dbms_sql.return_result(a_ref_cursor);
For 11g and below , here are some of the techniques explained.
How to output result of SELECT statement which is executed using native dynamic SQL?
A better solution if you are ok not to use your function would be
Generating SQL*Plus script using SQL*Plus
Wrong syntax... Maybe you could call the spool you just created?
Here is what could work:
SET HEAD OFF
SET TRIMOUT ON
SET TRIMSPOOL ON
SET LINESIZE 32000
SET PAGESIZE 0
SET TERMOUT OFF
SET ECHO OFF
SET COLSEP ''
spool /a/b/rvibap/c/&1..sql
prompt SET COLSEP ,
select build_select(&1) from dual;
spool off;
spool /a/b/rvibap/c/&1..csv
#/a/b/rvibap/c/&1
spool off;
Hope it helps.

How to spool query results in oracle

I am trying to print query result. Here is the Oracle SQL Script:
set linesize 32767;
set pagesize 0;
set newpage 0;
set space 0;
set echo off;
set feedback off;
set verify off;
set heading off;
set sqlprompt '';
set trimspool on;
set headsep off;
spool C:\asd.tmp
SELECT PROCESSNAME FROM ACTIVEPROCESSLIST ORDER BY PROCESSID;
spool off;
But the content of "asd.tmp" is that:
SELECT PROCESSNAME FROM ACTIVEPROCESSLIST ORDER BY PROCESSID
It prints just query text, not the result of it. How can I spool query results?
NOTE: I am using "sqldeveloper-4.0.3.16.84-x64" and it runned as administrator. Also setting properties given above may be non-sense. I have tried some combinations of them and executing just spool commands without settings.
Any other solution that I can execute in C++ is proper for me too.
you can use command line/sqlplus. try save your script as a file, then in command line:
sqlplus -s [username]/[password]#[sid] #file_path > output.txt
There is an alternative way in that link. I recommend it. It doesn't work as fast as spool solution but it works in my project at least.
There was a problem about the alternative way but the answer in this stackoverflow question works fine. I am using it now.

Append to spool file Oracle

I have one script file called Test.sql in D:\Scripts folder and the content of the file is given below
SET SERVEROUTPUT ON
SET DEFINE OFF
SPOOL Test.log;
SELECT USER_NAME FROM TUP_USER WHERE USER_ID=1432;
SPOOL OFF;
SET DEFINE ON
SET SERVEROUTPUT OFF
I normally execute this by opening command prompt, locate to D:\Scripts and give sqlplus username/password#Database and then give #test.sql to execute this and it will generate a log file called Test.log
Every time I execute this, it replaces the old file with the new data. I need to append new data to the file using spool. Is there a way to do that?
Any help would be appreciated. Thanks in advance.
Finally got the solution for this!
Add append after Test.log
SET SERVEROUTPUT ON
SET DEFINE OFF
SPOOL Test.log append;
SELECT USER_NAME FROM TUP_USER WHERE USER_ID=1432;
SPOOL OFF;
SET DEFINE ON
SET SERVEROUTPUT OFF
Just add append when you're writing the spool query:
spool d:\lab1.txt append;

Deleting empty spool file when no data

I have a sql script which creates a spool file.
When I do not have data, I am getting an empty file created. I do not want the file to get created in that case.
How can I do this?
set termout off;
set newpage 0;
set space 0;
set linesize 255;
set pagesize 0;
set echo off;
set feedback off;
set heading off;
set verify off;
set trimspool on;
UNDEFINE p_xml_filename
DEFINE p_xml_filename=&1
spool &p_xml_filename
SELECT * FROM emp;
spool off;
set feedback on
set verify on
set heading on
set echo on
exit
Instead of handling it in SQL*Plus, I would rather do it at OS level. In your scenario, you want to spool the file only if data exists. But, to do that, you need to check the COUNT of rows in the table, thus it is an overhead to the process.
Alternatively, I would:
Let the spool happen always
Do a grep of the spool file to check the content
Remove the file if no content found. rm filename