sql query appears in the outpout of spool command(file.txt) - sql

i want to save the result of a query using spool command.
But i got the query and its output in the result file (result.txt)
spool "result.txt"
select * from table;
spool off
how can i remove the query from the file result.txt?

You need to add the following set command:
set termout off
If you store the complete script in a script file (for instance getData.sql) and then run it from SQL Plus with the following command you will have the results wanted.
#getData.sql
Note that typing these commands directly in SQL Plus will cause the select statement to be in the file, even with the set parameter.

Related

How to create a spool file in plsql developer tool command window

After executing query in plsql developer tool command window the query &output should be saved in spool file automatically
eg;
update im set folio_no=123 where active_flag='Y';
1 rows updated

How to loop output to csv in Oracle 11g?

I have a function that returns a table of data. I created a query that runs the function and spools the output to a file. I need to know if there is a way to loop this and automate this output for many files.
set heading off
spool testfile.csv
select * from table(function(location));
spool off;
This code successfully leaves me with a csv file of the table. What I would like to do is loop it and feed it a list of locations and have it print me a file for each. When I tried to implement a loop I ran into an issue where my spool path gives me an error if there is a begin statement in front of it. This is concerning because if I wanted to use a function or procedure I would need to use a begin and end.

Using path relative to SQL script location in that script?

I have a sql script in
dir:\some\path\script.sql
This script is executed via SqlDeveloper and spools some data into
dir:\some\path\data\tablename.csv
Now, I have to store the script in a .sql file because if I execute it from SqlDeveloper directly, spool writes the query itself to the file which I do not want.
But I also do not want to hard-code paths because everyone in my team has their GIT repo whereever they want, so the scenario I'd like to achieve is that my sql script uses relative paths from it's location to write the csv files.
Currently the script.sql looks something like this:
SET COLSEP ";"
spool dir:\some\path\data\table1.csv
select /*csv*/ * from table1;
spool off;
and I want to look it something like this
SET COLSEP ";"
spool current_dir\data\table1.csv
select /*csv*/ * from table1;
spool off;
So that when using the script, the path has to be typed only once, when calling the script.
More of a workaround than a solution to the problem, but for now I just created a scriptRunner.sql file in the same directory as the script.sql which simply runs the script (#script.sql).
When opening this file, it seems like SqlDevelopers working directory is set to the directory where both .sql files are in and this property is extended to the called script, therefore I can simply use
spool data\table1.csv

SQL Plus SP2-0606

Using SQL tool, I want to create a text file that has a VARCHAR records.
Using SQL Developer I have successfully created the output text file by calling a script. But based on my research, the TRIMSPOOl command is currently not supported by SQL Developer, making my output to have trailing spaces.
Here is the code in the script:
spool on
set echo off
set verify off
set feedback off
set heading off
set trimspool on
set termout off
spool C:\SQLFiles\Output.txt;
select cust_name || cust_addr as Cust_Details
from customer_db;
spool off;
I now have the SQL Plus 11g, and I'm trying to run the script i created in SQL Developer. I'm getting an SP2-0606 error, saying:
SP2-0606: Cannot create SPOOL file "on.LST:
Based on the research I did, it is because of the Spool command, and I don't have the right to access the default folder??..
Can you help please on what setup I should change do to make the desired result in SQL Plus.
You have to put the file name in double quotes - I guess SQLPlus uses C-style escape sequences, giving the backslash \ a special meaning. Also, you should remove the semicolon ';':
So if you replace
spool C:\SQLFiles\Output.txt;
with
spool "C:\SQLFiles\Output.txt"
it should work as expected (if C:\SQLFiles exists and is writable for your account).
UPDATE
As #LalitKumarB has pointed out, spool works perfectly fine without the double quotes. The real problem is elsewhere:
spool on
This tells SQLPlus to create an output file on in the current directory. So you'll get this error message if you don't have access to the directory you're starting SQLPlus from.
You are getting this error because of permission issues. Either open your command prompt window in administrator mode or save your file to a location where admin permission is not required.
From the docs,
SP2-0606 Cannot create file file_name
Cause: The STORE command was unable to create the specified file. There may be insufficient disk space, too many open files, or
read-only protection on the output directory.
Action: Check that there is sufficient disk space and that the protection on the directory allows file creation.
My test case shows there is nothing wrong with the spool command. I don't think there is any need of quotes at all.
For example,
Client: 12c
OS : Windows 7
SQL> spool D:\text.txt
SQL> select ename||to_char(empno) from emp;
ENAME||TO_CHAR(EMPNO)
--------------------------------------------------
SMITH7369
ALLEN7499
WARD7521
JONES7566
MARTIN7654
BLAKE7698
CLARK7782
SCOTT7788
KING7839
TURNER7844
ADAMS7876
ENAME||TO_CHAR(EMPNO)
--------------------------------------------------
JAMES7900
FORD7902
MILLER7934
14 rows selected.
SQL> spool off;
And the spool file looks like:
So it works without any double-quotation marks. I guess you need to really look at the directory you are using to spool.
this error is just because of , not having "WRITE" privileges on directory where you are creating the spool file.
Solution
give or ask to give the permission to you who is owner of that directory, if its not owned by you using chmod. for ex- full permission to all chmod 777 <file/directory name>
When you get this error check for spaces. For example, below you see both single quotes (') but the first one has a space after it and the second one doesn't.
SP2-0606: Cannot create SPOOL file ' E:\SQL CLASS\New folder\sql class prt'
SQL> spool 'E:\SQL CLASS\New folder\sql class prt'

Log file avoid dumping sql query data in it.. c shell

I have sql script "example.sql":
SPOOL &1
Select '<.TR>'||'<.TD align="left">'||column_name||'<./TD>'||'<.TR>' from table1;
spool off
which dumps it contents to cshell script "getdata.csh" this is how i get data from sql script to csh script
sqlplus $ORA_UID/$ORA_PSWD #${SQL}example.sql ${DATA}${ext}
My problem is when I run my job to do this it dumbs all data extracted from sql query in log file too.. How can I avoid dumbing data into log files. What do I have to do in this code to do so? thank you
Look here for information about your issue: sqlplus spool
Essentially, you will need to add these two settings:
set echo off
set termout off