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

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.

Related

Need double quotes in each column while spooling data from oracle database to Excel file

I am automating a process wherein I run a SQL query through batch and the output is spooled into a csv file.
Requirement: Each field of csv file should have double quotes.
Ex :
Currently the output is PROJ_SHORT_NAME,WBS_SHORT_NAME
CGL1,CGL1
Required output is "PROJ_SHORT_NAME","WBS_SHORT_NAME"
"CGL1","CGL1"
SQL Query :
set verify off
set trimout off
set trimspool off
set feedback off
set linesize 22000
set pagesize 200
col csv_string FORMAT a1200
set colsep ','
SET UNDERLINE OFF
SET ECHO OFF
SPOOL E:\PDE_GPO\outputfile1.csv
select * from <tablename>;
SPOOL OFF
exit;
The || concatenates items together.
You can do:
SELECT '"'||col1||'","'||col2||'","'||...
FROM table
That would produce something like:
row 1: "col1val","col2val","col3val"...
row 2: ...
The down-side is you have to list/know every column you want to pull, but best coding practices would state you should specify the columns anyways (in case things are added/removed you want to be sure you get what you want).
-Jim
I got the solution for this.
I had to import set markup csv on and the issue got resolved.

How to execute Sql query from batch file

I'm new to batch files scripting.
All I want is creating a batch file that is calling SQL file and store results in CSV file .
Can anyone help me, your help is highly appreciated.
I am using Oracle database (version : oracle 11g)
Update:
Set cn = CreateObject("ADODB.Connection")
cn.ConnectionString = "Driver={Microsoft ODBC for Oracle};
CONNECTSTRING=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=<host>)(PORT=<port>))(CONNECT_DATA=(SERVICE_NAME=whipripa)));
uid=<uid>;pwd=<pswd>;"
While executing above query, it's not giving any errors, but still it's not connecting to Database either. Can someone tell me how to go ahead.
Here is a template of SQL Plus Script:
set colsep ,
set headsep off
set pagesize 0
set trimspool on
set linesize 2
set numwidth 5
spool books.csv
SELECT
title,
primary_author
FROM
books;
spool off
you’ll simply issue the sqlplus command from your shell:
sqlplus user/pwd#mydb #query.sql

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.

sqlplus client redirect remote results to a local file

I'm accessing an oracle database remotely and I need to redirect results to a local csv file. I'm setting some options, a simple sql query statement and finally I redirect to a local file.
Based on this link and this I did that:
./sqlplus user/password#host:1521/SID <<< "SET PAGESIZE 40000 FEEDBACK OFF MARKUP HTML ON select * from mytable where ROWNUM <= 10" >> test_file.xls
Doing that I get this Oracle error: SP2-0158: unknown SET option "select". The error is self explanatory. I know I need these set statements to format my output. Removing SET statement works fine but output.
To output, I don't insert SPOOL statement because the file will be generated on remote machine.
What do I need to do to get results to my local file?
Try that:
./sqlplus user/password#host:1521/SID <<EOF >> test_file.xls
SET PAGESIZE 40000 FEEDBACK OFF MARKUP HTML ON
select * from mytable where ROWNUM <= 10;
exit
EOF

How to output oracle sql result into a file in windows?

I tried
select * from users
save D:\test.sql create;
But SQL plus gives me "no proper ended"
How to specify path in oracle sql in windows?
Use the spool:
spool myoutputfile.txt
select * from users;
spool off;
Note that this will create myoutputfile.txt in the directory from which you ran SQL*Plus.
If you need to run this from a SQL file (e.g., "tmp.sql") when SQLPlus starts up and output to a file named "output.txt":
tmp.sql:
select * from users;
Command:
sqlplus -s username/password#sid #tmp.sql > output.txt
Mind you, I don't have an Oracle instance in front of me right now, so you might need to do some of your own work to debug what I've written from memory.
Very similar to Marc, only difference I would make would be to spool to a parameter like so:
WHENEVER SQLERROR EXIT 1
SET LINES 32000
SET TERMOUT OFF ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON TAB OFF
SET SERVEROUTPUT ON
spool &1
-- Code
spool off
exit
And then to call the SQLPLUS as
sqlplus -s username/password#sid #tmp.sql /tmp/output.txt
spool "D:\test\test.txt"
select
a.ename
from
employee a
inner join department b
on
(
a.dept_id = b.dept_id
)
;
spool off
This query will spool the sql result in D:\test\test.txt
just to make the Answer 2 much easier, you can also define the folder where you can put your saved file
spool /home/admin/myoutputfile.txt
select * from table_name;
spool off;
after that only with nano or vi myoutputfile.txt, you will see all the sql track.
hope is that help :)
Having the same chore on windows 10, and windows server 2012.
I found the following solution:
echo quit |sqlplus schemaName/schemaPassword#sid #plsqlScript.sql > outputFile.log
Explanation
echo quit | send the quit command to exit sqlplus after the script completes
sqlplus schemaName/schemaPassword#sid #plsqlScript.sql execute plssql script plsqlScript.sql in schema schemaName with password schemaPassword connecting to SID sid
> outputFile.log redirect sqlplus output to log file outputFile.log