To use Oracle SQL Command Line (SQL*Plus), connect to an Oracle database using the sqlplus executable with a username, password, and connection identifier, then execute SQL and PL/SQL statements directly from the terminal.
What You'll Need
- Oracle Database client software installed (includes SQL*Plus)
- Valid Oracle database username and password
- Network access to the Oracle database server
- Connection details: hostname, port, and service name or SID
- Terminal or command prompt access (Windows CMD, PowerShell, or Unix/Linux shell)
Step-by-Step
- Open your terminal or command prompt. On Windows, press Win+R, type
cmd, and hit Enter. On macOS or Linux, open Terminal from the applications menu. - Verify SQL*Plus is accessible by typing
sqlplus -vand pressing Enter. If you see a version number (e.g., "Release 21.0.0.0.0"), the tool is installed and in your PATH. If not, locate the Oracle client installation directory (e.g.,C:\oracle\product\21c\client_1\binon Windows or/opt/oracle/product/21c/client/binon Linux) and add itsbinfolder to your system PATH, or navigate to it directly. - Determine your connection string format. For a remote database, use:
sqlplus username/password@hostname:port/service_name. For example:sqlplus hr/oracle@dbserver.example.com:1521/orclpdb. If using SID instead of service name, format ishostname:port:SID. For local connections via OS authentication, you may usesqlplus / as sysdbaif your OS user is in theORA_DBAgroup (Linux) orORA_DBA(Windows). - Enter the connection string at the prompt and press Enter. If the credentials are valid and the database is reachable, you will see the SQL*Plus prompt:
SQL>. If prompted for a password only (if you omitted it in the string), type it carefully—input will not be visible—and press Enter. - Once at the
SQL>prompt, you can execute SQL statements. For example, typeSELECT * FROM dual;and press Enter. You should see a result set with one row containing 'X'. - To run a multi-line statement, such as a PL/SQL block, begin typing and end with a forward slash (
/) on a new line. For example:SQL> BEGIN 2 DBMS_OUTPUT.PUT_LINE('Hello, World'); 3 END; 4 /Press Enter after each line; the slash tells SQL*Plus to execute the block. - To save your work, use the
SAVEcommand. For example,SAVE myquery.sqlwrites the current buffer to a file. To load a script later, useSTART myquery.sqlor@myquery.sql. - To format output for readability, use SQL*Plus commands like
SET LINESIZE 100,SET PAGESIZE 50, orCOLUMN column_name FORMAT A20. These affect how results are displayed. - When finished, exit SQL*Plus by typing
EXITorQUITand pressing Enter. This returns you to your operating system shell.
Common Mistakes
- Forgetting the forward slash (
/) after a PL/SQL block, causing SQL*Plus to wait for more input instead of executing. - Using the wrong connection string format—confusing service name with SID, or omitting the port when it's non-standard (not 1521).
- Assuming the
hraccount is unlocked by default; in many installations, sample schemas are locked and must be unlocked by a DBA. - Trying to use SQL*Plus without setting the Oracle environment variables (like
ORACLE_HOMEorPATH), leading to "command not found" errors. - Misreading error messages like ORA-12154 (TNS:could not resolve the connect identifier) as password issues when it's actually a networking or tnsnames.ora configuration problem.
- Including spaces around the @ symbol in the connection string (e.g.,
username / password @ host), which SQL*Plus does not accept. - Attempting to run SQL*Plus commands like
DESCRIBEorSETinside SQL scripts without recognizing they are SQL*Plus-specific, not SQL.
Tips
- Use
SET ECHO ONto see each line of a script as it's executed, helpful for debugging. - Leverage
SET AUTOTRACE ONto view execution plans and statistics after queries—requires the PLUSTRACE role granted to your user. - If you frequently connect to the same database, create a login.sql file in your SQL*Plus startup directory (or current directory) to automatically run preferred
SETcommands each time. - Use spooling to save query results to a file:
SPOOL output.txtbefore your query, thenSPOOL OFFafter. - For secure connections, consider using Oracle Wallet or external authentication instead of embedding passwords in scripts or command lines.
- On Linux/macOS, wrap passwords in quotes if they contain special characters:
sqlplus username/"p@ss!wrd"@db. - Check the
tnsnames.orafile (usually in$ORACLE_HOME/network/admin) for predefined connection aliases you can use instead of typing full connection strings.
Troubleshooting
- Error: "SP2-0640: Not connected" – You attempted to run a SQL command before establishing a connection. Reconnect using the
CONNECTcommand or restart SQL*Plus with proper credentials. - Error: "ORA-12154: TNS:could not resolve the connect identifier" – The service name or alias in your connection string doesn't match any entry in
tnsnames.oraor EZCONNECT syntax is incorrect. Verify spelling, domain, and that the listener is running on the target server. - Error: "ORA-01017: invalid username/password; logon denied" – Double-check credentials. Remember passwords are case-sensitive in Oracle 11g and later unless explicitly created otherwise. If using OS authentication, ensure your OS user is authorized.
- Error: "sqlplus: command not found" – The SQL*Plus executable is not in your PATH. Locate
sqlplus(orsqlplus.exe) in the Oracle clientbindirectory and add that path to your system PATH variable, or run it via full path. - No output from DBMS_OUTPUT – By default, SQL*Plus does not display server output. Enable it with
SET SERVEROUTPUT ONbefore running PL/SQL that usesDBMS_OUTPUT.PUT_LINE. - Slow connection or timeout – Test network connectivity with
tnsping service_nameortelnet hostname 1521. Check firewall rules and ensure the Oracle listener is active (lsnrctl statuson the server). - Blank screen or hanging after login – This can occur if the database session is stuck or if there's an issue with terminal settings. Try pressing Enter to see if the prompt appears, or interrupt with Ctrl+C and reconnect.
Frequently Asked Questions
Can I run SQL*Plus without installing the full Oracle client?
Yes, Oracle provides a lightweight "Instant Client" package that includes SQL*Plus. You still need to configure networking files like tnsnames.ora or use EZCONNECT syntax.
Is SQL*Plus the same as SQL Developer or SQLcl?
No. SQL*Plus is the original command-line interface. SQL Developer is a GUI tool, and SQLcl is a modern command-line interface with features like command history and editing—all three can connect to Oracle databases but differ in interface and capabilities.
How do I change my password in SQL*Plus?
Use the PASSWORD command. At the SQL> prompt, type PASSWORD, then follow the prompts to enter the old and new password. For another user (if you have privileges), use PASSWORD username.
Can I run SQL*Plus scripts from a batch file or shell script?
Yes. Invoke SQL*Plus non-interactively with: sqlplus -s username/password@db @script.sql. The -s flag enables silent mode, suppressing banners and prompts for cleaner automation.
Why does my date format look wrong in query results?
SQL*Plus displays dates based on the NLS_DATE_FORMAT session parameter. Change it with ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS' or set it globally in your login script.
Is it safe to include passwords in SQL*Plus command lines?
Generally no—on most systems, command-line arguments are visible to other users via process listing tools like ps or Task Manager. For better security, omit the password to be prompted, or use external authentication or Oracle Wallet.