'Ran a SQL*Plus query directly against Pluggable Database

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.



Solution 1:[1]

After connecting to the Oracle database, just type show pdbs:

SQL> show pdbs
SQL> alter session set container=<DB_NAME>_PDB;
SQL> show pdbs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeremy Caney