Ran a SQL*Plus query directly against Pluggable Database - oracle

we are starting to work with oracle multitenant architecture, and modify our custom shell commands accordingly. so in the aspect of running an SQLPlus query against the CDB, nothing has really much changed, we are still declaring the Oracle_HOME and Oracle_SID environment variables, and concatenating the actual query and the user and password credentials, like this:
export ORACLE_SID=contdev;export ORACLE_HOME=/oracle/app/oracle/product/19.3;printf "SHOW CON_NAME;"| $ORACLE_HOME/bin/sqlplus -s 'c##devuser/devpassword'
but we are struggling to achieve the same for the PDB, as we don't use tnsnames, we still required to connect with user name and password, with a one liner shell command, not a script. due to our security team policy.
searching on web, we have found that another environment variable is available for the PDB, named: ORACLE_PDB_SID. but when we trying to ran the following command:
export ORACLE_SID=contdev;ORACLE_PDB_SID=contDev1;export ORACLE_HOME=/oracle/app/oracle/product/19.3;printf "SHOW CON_NAME;"| $ORACLE_HOME/bin/sqlplus -s 'c##devuser/devpassword'
We are still getting the output from the root container, and not the PDB:
CON_NAME
------------------------------
CDB$ROOT
Another search on the web, we found that we need use ALTER SESSION SET CONTAINER=contDev1; but it's not clear how to use this, in the above command.

After connecting to the Oracle database, just type show pdbs:
SQL> show pdbs
SQL> alter session set container=<DB_NAME>_PDB;
SQL> show pdbs

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

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.

how to connect to multiple oracle databases of different servers via shell script

I want to connect to multiple (30) oracle databases which resides in different servers (27) via shell script and fetch details from each database. I have a user (test) created on all databases but the details I want to fetch needs sysdba privileges. In my environment, I cannot provide DBA privs to any other user due to limitations. Hence my idea is to connect to each database using sqlplus -s "$user/$password#$tnsentry" and then connect as sysdba to fetch details.
Though I am able to connect to all databases using test, the "connect as sysdba" is getting executed on current database in current server.
My script:
cat tmp/db.par | while read LINE
do
if [ -n "$LINE" ] ; then
tns_entry=$LINE
export tns_entry
sqlplus -s /nolog >> $tmp/query.log <
exit
sqlconn
fi
done
In the tns_entry loop, I have given ABC,DEF,GHI,JKL,MNO databases to get details from and I am running this query from a server where database XYZ resides. I didn't give XYZ in my loop and didn't connect to database but the query results are from XYZ database. Please help me on this.
Thank you!
You should connect as sysdba
add "as sysdba" in the script and try.
conn sys/password#tnsentry as sysdba

How I can pass OS value to sql

I am trying to write a shell scripts for changing the db link as everytime password for Dev got change
so I am putting like below.
=======================================
export DEV_PASS=nevert3ll
sqlplus /nolog
connect apps/appspwd#TEST
drop database link TEST_TO_DEV;
create database link TEST_TO_DEV connect to apps identified by ${DEV_PASS} using 'DEV';
exit
EOF
=======================================
but this is treating ${DEV_PASS} a value rather than export value.
Can you advise me how to solve this issue.
Thanks,SM
I think you're missing -s switch in sqlplus:
export DEV_PASS=nevert3ll
sqlplus -s /nolog
connect apps/appspwd#TEST
drop database link TEST_TO_DEV;
create database link TEST_TO_DEV connect to apps identified by $DEV_PASS using 'DEV';
exit
EOF

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