Sub topic 8
Connecting from SQLcl with CONNECT
This chapter assumes that you know basic SQL, can use a terminal, and have access to an Oracle Database account. You also need SQLcl installed and a database connection identifier, such as an Easy Connect string or a TNS alias.
SQLcl is Oracle's command-line interface for SQL and PL/SQL. The CONNECT command opens a database session or changes the session used by the current SQLcl process.
Start SQLcl
From a terminal, start SQLcl with a connection in one command:
sql hr@//dbhost.example.com:1521/orclpdb1.example.comThis uses Easy Connect syntax. The parts are:
| Part | Meaning |
|---|---|
| `hr` | Database username |
| `dbhost.example.com` | Database server host |
| `1521` | Listener port |
| `orclpdb1.example.com` | Database service name |
SQLcl asks for the password. The password is not shown while you type it.
You can also start SQLcl without connecting:
sql /nologThen use CONNECT at the SQLcl prompt:
SQL> connect hr@//dbhost.example.com:1521/orclpdb1.example.com
Enter password: ********
Connected.Starting with /nolog is useful when you do not want credentials in the terminal command or in shell history.
Connect with a TNS alias
A TNS alias is a short name defined in an Oracle Net configuration file, commonly named tnsnames.ora. For example:
ORCLPDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = dbhost.example.com)(PORT = 1521))
(CONNECT_DATA = (SERVICE_NAME = orclpdb1.example.com))
)If SQLcl can find this file, connect with the alias:
SQL> connect hr@ORCLPDB
Enter password: ********
Connected.The alias hides the host, port, and service details from the command. If SQLcl reports ORA-12154, Oracle Net could not resolve the alias. Check the alias spelling and the location selected by your Oracle client configuration.
The connection flow is the same for both forms:
Change or test the current connection
After SQLcl is running, CONNECT can replace the current session:
SQL> connect reporting@//dbhost.example.com:1521/orclpdb1.example.com
Enter password: ********
Connected.Use a query to verify the identity and destination of the session:
select sys_context('USERENV', 'SESSION_USER') as session_user,
sys_context('USERENV', 'SERVICE_NAME') as service_name,
sys_context('USERENV', 'SERVER_HOST') as server_host
from dual;The result should match the account, service, and host you intended. This check matters when several databases use similar names or when a TNS alias points to a different environment.
Common connection failures
ORA-01017usually means the username or password is invalid.ORA-12541usually means no listener is available at the specified host and port.ORA-12514means the listener does not know the requested service name.ORA-12154means a TNS alias could not be resolved.
Exercise
Which SQLcl command starts without logging in, and how do you connect afterward?
Show answer
Run sql /nolog, then enter connect username@//host:port/service_name at the SQL> prompt. SQLcl requests the password interactively.