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
Related
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
I am trying to get one file with my user name and passwords for some scripts that I run every day. I have several scripts working now using a batch file that has my user names and passwords.
My Password Batch file looks like this.
parms.bat
Rem Oracle Db
set odbUsername=myUserName
set odbpassword=myPassword
My other scripts call that batch file and get the information ok. I have a couple sql scripts that I also need to use these usernames and passwords. I can't seem to get them to work. But I am fairly new to sqlplus scripting.
My Sql Scripts looks like this.
CONNECT myusername#Oracleddbname/mypassword
SET FEEDBACK OFF;
SET ECHO OFF;
SET TERMOUT OFF;
SET HEADING OFF;
SET PAGESIZE 0;
SET LINESIZE 500;
SET TIMING OFF;
SET TRIMSPOOL ON;
SET COLSEP ',';
SPOOL C:\FileTransfers\MeterData.csv
PROMPT CoopCode,MeterID,DateTime,Value
SELECT DISTINCT
a.coopcode
|| ','
|| a.meterno
|| ','
|| a.readdatetime
|| ','
|| a.usage
FROM temp_reconfigured a, temp_goodsence b
WHERE a.coopcode = b.coopcode AND a.meterno = b.meterno
;
Spool off;
EXIT;
I call this script with a batch file that runs through windows task scheduler.
It looks like this.
sqlplus /nolog #C:\FileTransfers\AutomationScripts\GoodSence\SpoolGoodSenceDataSet.sql
So I would like to pass the user name to the sql from the batch file. I have read several things and tried about 30 and none seem to work. Thank you for your help in advance.
You can pass a parameter this way:
script.sql:
select '&1' from dual;
the call:
D:\>sqlplus user/password#db #d:\script.sql 'value'
SQL*Plus: Release 11.2.0.2.0 Production on Lun Ott 3 17:02:10 2016
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
old 1: select '&1' from dual
new 1: select 'value' from dual
'VALU
-----
value
Just typing that out made me think about passing it during the call vs inside the sql to connect. I then edited my SQL and took out the connect statement
So this worked...
echo Get Parameters
call C:\FileTransfers\AutomationScripts\parms.bat
echo %odbUsername%
echo %odbpassword%
sqlplus myusername#oracledb/%odbpassword%#C:\FileTransfers\AutomationScripts\GoodSence\SpoolGoodSenceDataSet.sql
I am on AIX using ksh. Created a unix script which generates the CREATE OR REPLACE VIEW ... and GRANT .. statements, which are placed in a single .txt file. Now I need to execute the contents in the file (there are around 300 view creation and grant statements) in Oracle and I need to log the sql statement that is about to execute and the feedback of Oracle whether the view is created or not..
My excerpt goes as
sqlplus -s username/password#servername <<EOF
SET ECHO ON;
SET NEWPAGE 0;
SET PAGESIZE 0;
SET LINESIZE 200;
SET LONG 10000000;
SET TRIMSPOOL ON;
SET HEADING OFF;
SET FEEDBACK ON;
SET VERIFY ON;
SET TERMOUT OFF;
SET SQLBLANKLINES ON;
SPOOL ${drctry}/${v_timestamp}_sql_execution_log.txt
#${drctry}/${v_date}_sql_statements.txt
SPOOL OFF;
EXIT;
EOF
If the contents of the file _${v_date}sql_statements.txt is
CREATE OR REPLACE VIEW V_TABLE1 AS SELECT * FROM TABLE1;
GRANT SELECT ON V_TABLE1 TO USER1;
...
CREATE OR REPLACE VIEW V_TABLE300 AS SELECT * FROM TABLE300;
GRANT SELECT ON V_TABLE300 TO USER300;
Expected output:
CREATE OR REPLACE VIEW V_TABLE1 AS SELECT * FROM TABLE1;
View created
GRANT SELECT ON V_TABLE1 TO USER1;
Grant succeeded
...
CREATE OR REPLACE VIEW V_TABLE300 AS SELECT * FROM TABLE300;
View created
GRANT SELECT ON V_TABLE300 TO USER300;
Grant succeeded
After some search, noticed List option; But it only records only the last statement that was executed if we have more than 1 statement, which doesn't fit here. In Teradata Bteq the ECHOREQuired attribute can be set to ON for this task. But I am not sure in Oracle. Also tried
sqlplus -s username/password#servername <<EOF > ${drctry}/${v_timestamp}_unix_sql_log.txt But still no luck. Will change the password hardcode once I overcome this issue;
The -s[ilent] option:
Suppresses all SQL*Plus information and prompt messages, including the command prompt, the echoing of commands, and the banner normally displayed when you start SQL*Plus.
So by including the -s flag you are overriding the set echo on directive.
If you omit that then the commands are echoed in the spool file, but (a) you also see the banner on stdout and (b) you see the SQL> prompt and the script name spool off echoed too. You can fix the first part by redirecting output, with the risk you may miss something you care about if there is a problem, by changing your shell command to do:
sqlplus -s username/password#servername >/dev/null <<EOF
And you can partly fix the spooled output by changing the prompt, adding:
SET SQLPROMPT ""
With those changes the shell sees no output, and the spool file contains:
#/path/to/v_date_sql_statements.txt
CREATE OR REPLACE VIEW V_TABLE1 AS SELECT * FROM TABLE1;
View created.
GRANT SELECT ON V_TABLE1 TO USER1;
Grant succeeded.
...
SPOOL OFF;
You could potentially post-process that to remove the first and last line, and (if you want) blank lines. You can also hide the script name by including the text file in the heredoc instead of using start/#:
SPOOL ${drctry}/${v_timestamp}_sql_execution_log.txt
`cat ${drctry}/${v_date}_sql_statements.txt`
SPOOL OFF;
I made a sqplus bash script to do that.
link to github: https://github.com/javierB-/ORACLE.git
It create two files, one with the sql sentences, and the other with the sql sentences and their output.
The script core is:
tee -a $FOUT $FOUT_TRAZA | sqlplus $# | tee -a $FOUT
I use the script throught the alias:
alias sqlpluss='$ADMIN_HOME/oracle/sqlpluss.sh'
it's in spanish, i have traslated the output to write here:
sqlpluss
USE ONLY INTERACTIVELY:
sqlplus enriched to save input and output trace.
Use:
sqlpluss [-f fout] user/password#conection #scripts ...
(fout will be opened in add mode and it will default to yyyymmdd.log)
summary:
tee -a fout | sqlplus $# | tee -a fout
the full script its too long to paste here, but if someone want, i'll do it.
When I type in:
C:\>sqlplus user/pass#OMP1 #CheckRowCount.sql
it connects but I don't see any results, in the .sql file I have this:
SELECT COUNT(*) as "rowcount" FROM dmsn.ds3r_1xrtt_voice_trigger;
I've also tried this
C:\>sqlplus user/pass#OMP1 SELECT COUNT(*) as "rowcount" FROM dmsn.ds3r_1xrtt_voice_trigger;
but all I get is the sql*plus commands to use in the CMD window
You don't get anything in this way, if you want result from a script you must add a spool command to you script.
spo result.txt;
SELECT COUNT(*) as "rowcount" FROM dmsn.ds3r_1xrtt_voice_trigger;
spo off;
Then execute C:\>sqlplus user/pass#OMP1 #CheckRowCount.sql and open result.txt to see result.
Anyway for single script like this above just put it in sql> prompt and see what happens.
I am using Solaris. I have to log into sql plus and run some queries, which give a huge result set.
I want to copy all that into a file. Is there any command for it in unix or sqlplus ?
Use the SPOOL command:
SQL> SPOOL /opt/output
SQL> SELECT ...
SQL> SPOOL OFF
setup Oracle environment
(there are ways around specifying username/password on the command line - not the best way especially when other users can 'ps' on the server and see your password)
sqlplus -s username/password <<-!!
set trimspool on trimout on pages 0 feedback off linesize 1000 echo off verify off
spool file.out
select sysdate from dual;
exit
!!
If you are on the command line then just use the > and 2> to redirect stdout and stderr respectively to log files
func > out.log