Echo sqlplus command without using prompts or scripts - oracle

I have a script which I want to maintain as a single file, however I wish to echo the commands input into sqlplus without using either PROMPT <sql> or #script.sql can this be done?
Current script:
$ cat test.sh
#!/bin/bash
LOG=/home/oracle/output.log
sqlplus hr/hr <<EOF > $LOG
set echo on
select 1 from dual;
QUIT
EOF`
Current output:
$ cat output.log
SQL*Plus: Release 11.2.0.4.0 Production on Tue Mar 1 15:01:12 2016
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options
SQL> SQL>
1
----------
1
What I want:
$ cat output.log
SQL*Plus: Release 11.2.0.4.0 Production on Tue Mar 1 15:02:02 2016
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options
SQL> SQL> SQL> select 1 from dual;
1
----------
1

When SQL*Plus is reading commands from your TTY, the echoing of input is actually handled by your TTY, not SQL*Plus. If SQL*Plus handled the echoing, any time you manually typed a command you would see the command twice (once as you typed it, once as it was echo'd back).
Additionally, the TERMOUT option only applies when running a script file, not reading from STDIN.
The easy fix is telling SQL*Plus that /dev/stdin is a script:
sqlplus scott/tiger #/dev/stdin <<EOF
SET TERMOUT ON ECHO ON
SELECT SYSDATE FROM dual;
EOF

try spool instead of redirecting your STDOUT:
#!/bin/bash
LOG=/home/oracle/output.log
sqlplus hr/hr <<EOF
set echo on term on
spool $LOG
select 1 from dual;
QUIT
EOF

To expand on wonderful #Mr.llama answer.
His construct works perfect on Solaris.
However on Linux (Oracle Linux Server release 7.9), this causes every command to run twice in sqlplus, even though it echoes the command only one time. See below:
$ sqlplus -l / as sysdba #/dev/stdin <<-EOF
> set echo on termout on
> select systimestamp from dual;
> EOF
SQL*Plus: Release 19.0.0.0.0 - Production on Wed Feb 23 14:33:03 2022
Version 19.11.0.0.0
Copyright (c) 1982, 2020, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.11.0.0.0
SQL> select systimestamp from dual;
SYSTIMESTAMP
---------------------------------------------------------------------------
23-FEB-22 02.33.03.975542 PM -08:00
SQL> SQL>
SYSTIMESTAMP
---------------------------------------------------------------------------
23-FEB-22 02.33.03.976106 PM -08:00
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.11.0.0.0
Notice how there are two systimestamp lines, with different timestamps, which means that the select was executed twice.
This double execution does not happen when same block is run on Solaris.
A Linux workaround is to modify construct to:
$ cat /dev/stdin <<-EOF | sqlplus -l / as sysdba #/dev/stdin
> set echo on termout on
> select systimestamp from dual;
> EOF
SQL*Plus: Release 19.0.0.0.0 - Production on Wed Feb 23 14:38:08 2022
Version 19.11.0.0.0
Copyright (c) 1982, 2020, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.11.0.0.0
SQL> select systimestamp from dual;
SYSTIMESTAMP
---------------------------------------------------------------------------
23-FEB-22 02.38.08.931790 PM -08:00
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.11.0.0.0

Related

Getting error "Got minus one from a read call, connect lapse 4 ms., Authentication lapse 0 ms." connecting to Oracle image on docker

When I try to connect to a oracle in a docker container, from Oracle SQL Developer,
I get the error "Got minus one from a read call, connect lapse 4 ms., Authentication lapse 0 ms."
I went on the oracle container page https://container-registry.oracle.com/ords/f?p=113:10::::::
I pulled express version, I was going to pull the standard but its been removed.
docker pull container-registry.oracle.com/database/express:latest
I then ran the image
docker run --name oracle-container -p 1521:5500 -e ORACLE_PWD=MyPasswd123 -e ORACLE_CHARACTERSET=AL32UTF8 -v /opt/oracle/oradata container-registry.oracle.com/database/express:latest
Got database started, and then logged in on the command line
docker exec -it oracle-container sqlplus sys/MyPasswd123#//localhost:1521/xe as sysdba
SQL*Plus: Release 21.0.0.0.0 - Production on Sun Dec 11 10:49:14 2022
Version 21.3.0.0.0
Copyright (c) 1982, 2021, Oracle. All rights reserved.
Connected to:
Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
SQL>
Ran a command no problem
SQL> SELECT sys_context('userenv','instance_name') FROM dual;
SYS_CONTEXT('USERENV','INSTANCE_NAME')
--------------------------------------------------------------------------------
XE
The problem is trying to connect from java or from Oracle SQL Developer
I set username as sys as sysdba
password as MyPasswd123#
HostName as localhost
port as 1521
SID as XE
And I get the error.
Its confusing as I can connect, using the command line docker exec -it
You are binding enterprise manager (port 5500) to host port 1521.
# fire up the database
docker run --name oracle-container -d --rm -p 1521:5500 -e ORACLE_PWD=MyPasswd123 -e ORACLE_CHARACTERSET=AL32UTF8 container-registry.oracle.c
om/database/express:latest
# query from inside the container
echo 'select dummy from dual;' | docker container exec -i oracle-container sqlplus -S system/MyPasswd123#localhost:1521/XE
D
-
X
# query from outside the container
echo 'select dummy from dual;' | sqlplus -S system/MyPasswd123#localhost:1521/XE
ERROR:
ORA-12537: TNS:connection closed
# let's bind to the correct port
docker run --name oracle-container -d --rm -p 1521:1521 -e ORACLE_PWD=MyPasswd123 -e ORACLE_CHARACTERSET=AL32UTF8 container-registry.oracle.com/database/express:latest
# query from inside the container
echo 'select dummy from dual;' | docker container exec -i oracle-container sqlplus -S system/MyPasswd123#localhost:1521/XE
D
-
X
# query from outside the container. This time it works!
echo 'select dummy from dual;' | sqlplus -S system/MyPasswd123#localhost:1521/XE
D
-
X
Best of luck!

sqlplus: password contains slash "/" is not accepted

Maybe this is trivial for DB people but unfortunately not for me.
I spent hours with this but the solutions that I have found did not help. My DB password contains a / character and I think this confuses sqlplus and that is the reason why I am not able to log in. Let's say my password is pass/word.
This is what I have tried so far:
$ sqlplus MYUSER/pass/word#ABCDEF
$ sqlplus MYUSER/pass\/word#ABCDEF
$ sqlplus MYUSER/'pass/word'#ABCDEF
$ sqlplus MYUSER/'pass\/word'#ABCDEF
$ sqlplus MYUSER/"pass/word"#ABCDEF
$ sqlplus MYUSER/"pass\/word"#ABCDEF
$ sqlplus MYUSER/\""pass/word\""#ABCDEF
None of them above worked.
Then I tried this way:
$ sqlplus MYUSER#ABCDEF
SQL*Plus: Release 12.2.0.1.0 Production on Fri Nov 19 15:10:13 2021
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Enter password:
SP2-0306: Invalid option.
Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM|SYSBACKUP|SYSDG|SYSKM|SYSRAC}] [edition=value]]
where <logon> ::= <username>[/<password>][#<connect_identifier>]
<proxy> ::= <proxyuser>[<username>][/<password>][#<connect_identifier>]
Enter user-name: ^C
And this way:
$sqlplus /nolog
connect MYUSER#ABCDEF
SQL*Plus: Release 12.2.0.1.0 Production on Fri Nov 19 15:10:13 2021
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Enter password:
SP2-0306: Invalid option.
Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM|SYSBACKUP|SYSDG|SYSKM|SYSRAC}] [edition=value]]
where <logon> ::= <username>[/<password>][#<connect_identifier>]
<proxy> ::= <proxyuser>[<username>][/<password>][#<connect_identifier>]
Enter user-name: ^C
No result.
Google did not help.
What is the trick here that I need to use?
The big idea is that the first and last character of what you type needs to be ". Then the interpreters (whether it's sqlplus or the RDBMS itself) will strip them and whatever remains is taken verbatim.
Your first four attempts aren't close but the final three are.
$ sqlplus MYUSER/"pass/word"#ABCDEF
This one is nearly perfect except that the shell1 gobbles up the " characters and so the forward slash is not taken verbatim.
$ sqlplus MYUSER/"pass\/word"#ABCDEF
This one is also close but the backslash doesn't help and the quotes are still gobbled up.
$ sqlplus MYUSER/\""pass/word\""#ABCDEF
Actually, this one will work but I suspect this is not precisely what you tried (i.e., it's a typo because SO handles backslashes in an unexpected way when you're quoting). In this example, the properly matched quote characters are "received" by the sqlplus main entry point.
All of the following should work for you.
Let sqlplus prompt you for the password and enter "pass/word" instead of pass/word.
sqlplus myuser/\"pass/word\" so long as you set TWO_TASK=ABCDEF.
sqlplus myuser/\"pass/word\"#ABCDEF # simplest imo
sqlplus myuser/\""pass/word\""#ABCDEF
sqlplus myuser/\""pass/word"\"#ABCDEF
footnotes:
1: If you're using a real operating system and not a Microsoft one.

sqlplus fails on execution from shell script, but works fine from it's execution from terminal

I have a software that runs a shell script. There is a sqlplus command among with a lot of other staff in that shell script.
sqlplus -L 'user/passwd'#\'localhost:1521/orcl\' #file_path.ddl test_cpm_user test_cpm_password CPMDAT CPMIDX
Shell scripts fails on that line above with message:
SQL*Plus: Release 11.2.0.4.0 Production on Fri Oct 12 11:50:36 2018
Copyright (c) 1982, 2013, Oracle. All rights reserved.
ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified
SP2-0751: Unable to connect to Oracle. Exiting SQL*Plus
However, if I run it by myself from terminal it works fine.
What might be a reason of such behaviour?
Try below command with braces since I see hostname contains special characters
sqlplus -L '{user}/{passwd}'#{hostname}' #file_path.ddl test_cpm_user test_cpm_password CPMDAT CPMIDX

for db recover process testing, unable obtain the query o/p using shell script

I have written shell script as:
#!/bin/bash
#shell script for recovery testing
$ORACLE_HOME/bin/rman target/ <<EOF >rman.log
shutdown immediate;
startup mount;
run
{
recover database;
}
sql 'alter database open read only';
exit;
EOF
$ORACLE_HOME/bin/sqlplus '/as sysdba' <<_EOF1_ >sql.log
spool '/home/oracle/test1.log'
select * from hr.employees;
spool off;
exit;
_EOF1_
, but unable to obtain the spool the output from sql query, how to solve the issue?
I don't know what is wrong with your code. It should work. I tested it:
$ sqlplus '/as sysdba' <<_EOF1_ >sql.log
spool '/home/oracle/test1.log'
select * from dual;
spool off;
exit;
_EOF1_
Output
$ cat sql.log
SQL*Plus: Release 11.2.0.4.0 Production on Mon Nov 6 15:18:42 2017
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
SQL> SQL>
D
-
X
SQL> SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
Output 2
$ cat /home/oracle/test1.log
SQL> select * from dual;
D
-
X
SQL> spool off;
There are two options to acomplish your goal. You use both in one. Any reasons for that?
Option 1
$ sqlplus / as sysdba << EOF > /dev/null
spool test.out
select * from dual;
spool off
EOF
Output
$ cat test.out
SQL> select * from dual;
D
-
X
SQL> spool off
Option 2
$ sqlplus / as sysdba << EOF > test.out
> select * from dual;
> EOF
Output
$ cat test.out
SQL*Plus: Release 11.2.0.4.0 Production on Mon Nov 6 15:12:05 2017
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
SQL>
D
-
X
SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options

Shell script cannot connect to Oracle since update from Oracle 8 to Oracle 10

I have a shell script that connects to a Oracle database.
function getdata {
sqlplus user/pass -s <<EOF
select c1 from t1;
exit;
EOF
}
The database has been upgraded from Oracle 8 to Oracle 10 and now the results file which should contain database results just contains the Oracle help text.
SQL*Plus: Release 10.2.0.5.0 - Production
Copyright (c) 1982,2010,Oracle. All Rights Reserved.
Usage 1: sqlplus -H | -V
-H Displays the SQL*Plus version and the
usage help.
-V Displays the SQL*Plus version.
Usage 2: sqlplus [ [<option>] [<logon>] [<start>] ]
<option> is: [-C <version>] [-L] [-M "<options>"] [-R <level>] [-S]
-C <version> Sets the compatibility of affected commands to the
version specified by <version>. The version has
the form "x.y[.z]". For example,-C 10.2.0
-L Attempts to log on just once,instead of
reprompting on error.
-M "<options>" Sets automatic HTML markup of output. The options
have the form:
HTML [ON|OFF] [HEAD text] [BODY text] [TABLE text]
[ENTMAP {ON|OFF}] [SPOOL {ON|OFF}] [PRE[FORMAT] {ON|OFF}]
-R <level> Sets restricted mode to disable SQL*Plus commands
that interact with the file system. The level can
be 1,2 or 3. The most restrictive is -R 3 which
disables all user commands interacting with the
file system.
-S Sets silent mode which suppresses the display of
the SQL*Plus banner,prompts,and echoing of
commands.
<logon> is: (<username>[/<password>][#<connect_identifier>] | /)
[AS SYSDBA | AS SYSOPER] | /NOLOG
Specifies the database account username,password and connect
identifier for the database connection. Without a connect
identifier,SQL*Plus connects to the default database.
The AS SYSDBA and AS SYSOPER options are database administration
privileges.
The /NOLOG option starts SQL*Plus without connecting to a
database.
<start> is: #<URL>|<filename>[.<ext>] [<parameter> ...]
Runs the specified SQL*Plus script from a web server (URL) or the
local file system (filename.ext) with specified parameters that
will be assigned to substitution variables in the script.
When SQL*Plus starts,and after CONNECT commands,the site profile
(e.g. $ORACLE_HOME/sqlplus/admin/glogin.sql) and the user profile
(e.g. login.sql in the working directory) are run. The files may
contain SQL*Plus commands.
Refer to the SQL*Plus User's Guide and Reference for more information.
option should be before logon so sqlplus -s user/pass

Resources