Should CONNECT work in SQL*PLUS script? - oracle

I'd like to run a sqlplus script from a cron job.
I thought I could put a line like:
CONNECT "myuser/mypass#mydb"
within the script and then just execute it with:
sqlplus #myscript
However, when I do so, I get:
SP2-0306: Invalid Option
SP3-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus
Am I misunderstanding the usage of the connect command?

When running CONNECT inside SQL*Plus, remove the quotes:
CONNECT myuser/mypass#mydb
They double quotes are required if you are passing the credentials as an argument to sqlplus:
sqlplus "myuser/mypass#mydb"
, for the shell to parse myuser/mypass#mydb as a single argument if you have spaces in your connection identifier or use additional options like AS SYSDBA.

Use the /NOLOG option.
sqlplus /nolog #myscript

Oracle 11gR2
I ran a .sql file via SQL*Plus connected initially as JOHN. Within the file I connect as SYS and then run a GRANT. See .sql file contents below:
connect sys/password as sysdba
GRANT EXECUTE ON DBMS_CRYPTO TO JOHN;
connect JOHN/DOE
NOTE: I don't recommend keeping the sys/password in a text file btw.
HTH

Related

start oracle .sql script through SQLcl on a remote windows host

On an Oracle database server, I have been able to schedule a nightly job that runs oracle scripts initiated from a powershell script which has this line:
sqlplus accountID/password #C:\scripts\run_scripts.sql
Now I need to achieve the same on another windows machine where the only thing of Oracle is the SQLcl client. I am able to invoke the initial_script.sql SQL script, but only interactively so far through three steps:
PS C:\sql_scripts> sql /nolog
SQL> connect id_maint/password#111.22.33.44:1521/sid
Connected.
SQL> #initial_script.sql
What I need is to have the oracle initial_script.sql script started from powershell. I have two hurdles.
To connect to the remote database, I have to first get the SQLcl's SQL> prompt via the PS> sql /nolog command at the powershell command prompt, and then run the SQL> connect id_maint/password#111.22.33.44:1521/sid command at the SQL> prompt.
SQL> connect id_maint/password#111.22.33.44:1521/sid works fine. But I need to invoke the oracle script initial_script.sql at the same time. However,
SQL> connect id_maint/password#111.22.33.44:1521/sid #C:\sql_scripts\initial_script.sql
incurs an error.
So what I am seeking is to get three things done (get the SQL> prompt -> connect to the remote database -> invoke the oracle script), all initiated by a power script at the PS> prompt.
Appreciated it very much if someone can help me out.
SQLcl is 99% compatible with SQLPlus so for almost any sqlplus command you should be able to just use sql.
Launch the script using your existing sqlplus command but using sql instead
sql id_maint/password#111.22.33.44:1521/sid #C:\sql_scripts\initial_script.sql

SQLPLUS Connection to DB with special characters '#'

I'm trying to connect to my remote db with a user who has an # in his password.
I use sqlplus v19 with an OracleClient and my remote db is an OracleServer v19 aswell. I had no problem during the alter user command on the database :
alter user USER identified by "P#ssword123";
user altered.
Below are the commands I tried to connect with this user :
sqlplus USER/"P#ssword123"#tnsname
sqlplus USER/'"P#ssword123"'#tnsname
sqlplus 'USER/"P#ssword123"'#tnsname
sqlplus USER/\"P#ssword123\"#tnsname
And some variants of those commands.
This always return me the same TNS error :
TNS:could not resolve the connect identifier specified
It looks like these solutions works for old sqlplus versions but I can't figure out how can I solve my problem with this version 19.
Of course, I tried to change the password with non # character and it works but this is not a possible solution in my specific case.
Thank you for all your replies.
I figure it out with your help. I even modified it to make it compatible with a PL/SQL Script which I pass some variables.
The answer :
sqlplus /NOLOG << EOF
connect USER/"P#ssword123"#tnsname
#script_plsql.sql $var1 $var2 $var3
EOF

Understanding connecting to the database through SQLPLUS

when reading the Oracle APEX documentation, I did not understand the reason it asks to connect to the database through sqlplus using sqlplus /nolog then connect sys as sysdba. If it will eventually connect with sys as sysdba, why not doing it from the beginning with sqlplus sys as sysdba command?
Here is what I am talking about:
https://docs.oracle.com/en/database/oracle/application-express/20.1/htmig/downloading-installing-Oracle-AE.html#GUID-7E432C6D-CECC-4977-B183-3C654380F7BF
I would suspect its historical. In older releases on SQL Plus, if you ran:
sqlplus my_user/my_pass
on the command line, then on unix environments someone running the 'ps' command could see those details. Even in modern versions of SQL Plus where we remove that from obvious view, then digging into things like command history could expose it.
So always best never to type user or password details on the command line.

Connected to an Oracle Database from Oracle SQL Developer, but cannot connect from the command line

I connect successfully to an Oracle Database from Oracle SQL Developer with the following settings:
but if I connect with the same settings from the command line
sqlplus PDBADMIN#172.28.59.199:1521/ORCLPDB1
I get
ERROR:
ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA
what does this mean?
As indicated in the SQLPlus documentation, to use this form of connect string on the command line, you need to enclose the whole thing in double quotes:
sqlplus PDBADMIN#"172.28.59.199:1521/ORCLPDB1"
(Depending on your OS, you may also need to escape those quotes with a backslash.)
I think syntactically this is necessary to distinguish /servicename from /password.

Connect SQLplus in oracle

I want to connect user sys in sqlplus of oracle but after I connect, I type like this:
sqlplus sys as sysdba
password:123456
Error:
ORA-01030:insufficient privilege
warning:You are no longer to connect oracle.
Does anyone help me to solve my problem?
Your example looks a little garbled; to connect to sqlplus via the command line, with a user sys and password 123456:
sqlplus sys/123456 as sysdba
or
sqlplus "sys/123456 as sysdba"
prior to Oracle 10. If you are already inside sqlplus (as I assume from the fact that your example starts with a SQL>), you use the connect command:
SQL> connect sys/123456 as sysdba
In all cases, if you haven't set the environment variable ORACLE_SID, you need to specify that after the password, like this:
sqlplus sys/123456#<mydbname> as sysdba
where <mydbname> is either of the form <hostname>/<sid>, if you're using Oracle 10 or later, or a valid entry from your tnsnames.ora file (located in $ORACLE_HOME/network/admin) for all versions.
Is your operating system user account that you are logged in as a member of the ORA_DBA group (windows) or the DBA group (*nix)?
If the answer is yes, then the next thing to check is for the existence of the ORACLE_HOME\database\orapwORCL.ora file, which contains the passwords for all of the users defined as sysdba users. If it does not exist, you need to shutdown the database and execute the orapwd utility:
cd ORACLE_HOME\database
orapwd file=orapwORCL.ora password=123456 entries=10
This defines the sys password as 123456. You should then be able to start up the database and connect sys/123456 as sysdba
The password file must exist for password=based authentication on sysdba logins. The reason for this is the fact that sysdba connections must be allowed when the database is not up and the database cannot authenticate you.

Resources