Oracle SQL Developer Command Line is a powerful tool for executing SQL scripts, running queries, and debugging connections—all without needing a full IDE. In SQL Developer 18.2, this command-line interface simplifies automation, testing, and troubleshooting, but requires proper setup to avoid common failures. Below is a complete walkthrough for first-time users.
What You'll Need
- A computer running Windows, macOS, or Linux with Oracle SQL Developer 18.2 installed (download from Oracle's official site)
- An Oracle database connection (TNS listener configured if remote access is needed)
- Basic familiarity with SQL syntax (optional, but helpful)
- Text editor or IDE (VS Code, Notepad++, etc.) for script preparation
Step-by-Step
- Open SQL Developer Command Line
Launch SQL Developer, then click Tools > SQL Developer Command Line (or press Ctrl+Shift+R if using the command palette). The prompt will appear in a separate window, ready for commands. - Establish a Connection
The command line connects to the database using a credential file or direct input. Use one of these methods:- For local connections, type:
sqlplus / as sysdba(Note: This requires admin privileges and may not work on all systems.) - For connection strings (username/password), use:
sqlplus username/password@hostname:port/SIDReplace values with your database credentials (e.g.,scott/tiger@ora.example.com:1521/ORCL). - For credential files (recommended for security), place a file at
~/.sqldeveloper/sqlplus.conf(Linux/macOS) or%USERPROFILE%\.sqldeveloper\sqlplus.conf(Windows). Example entry:[default] user = scott password = tiger host = ora.example.com port = 1521 service_name = ORCLRun the command line with:sqlplus(without arguments to auto-load).
- For local connections, type:
- Execute SQL Commands
Once connected, enter SQL statements directly. Examples:- Run a simple query:
SELECT * FROM employees; - Execute a stored procedure:
EXECUTE emp_package.my_procedure('test'); - Save a script to a file (e.g.,
employees.sql):spool employees_results.txtSELECT * FROM employees;spool off
- Run a simple query:
- Run Scripts from Files
Use thesqlplus -sflag to execute scripts line-by-line:sqlplus -s username/password@host:port/SID @path/to/script.sqlExample (on Linux/macOS):sqlplus -s scott/tiger@ora.example.com:1521/ORCL @scripts/employees.sqlOn Windows, use:sqlplus -s scott/tiger@ora.example.com:1521/ORCL scripts\employees.sql - Debugging Interactivity
Use thedbms_output.put_linefunction to print dynamic results:BEGIN FOR i IN 1..10 LOOP dbms_output.put_line('Iteration ' || i); END LOOP; END;Enable output with:SET serveroutput ON SIZE 1000000(Press Ctrl+Shift+Enter in SQL Developer to run a block.) - Exit Safely
Close the command line window or type:exitorquitto end the session without disconnections.
Common Mistakes
- Connection failures
Common causes: wrong credentials, misconfigured TNS listener, or port blocking. Verify with:telnet ora.example.com 1521(Replace with your database details; should show "Connected." if working.) - Permission errors
Use/ as sysdbaonly on local databases (not remote or production). For remote, use a dedicated schema. The spoolcommand writes results to a file. Ensure the file exists or redirect to a fixed path:spool ./results.txt(Use forward slashes on Linux/macOS, backslashes on Windows.)
Tips
- Use
SET LINESIZE 100to view long output in one line. - Pipe SQL output to other tools:
sqlplus @script.sql | grep invalid(Shows only invalid rows in the script.) - For large scripts, break them into chunks and run selectively.
- Use
SET HEADING OFFto suppress default column headers. - Store credentials securely in
~/.sqldeveloper/sqlplus.conf(not in scripts).
Troubleshooting
If the command line fails to connect or run commands, check these steps in order:
- Restart SQL Developer
Sometimes the UI and CLI get out of sync. Close and reopen the IDE. - Verify database accessibility
Test the connection manually via command line:sqlplus -v username/password@host:port/SID(Replace with real credentials.) - Check TNS listener status
On Linux/macOS:tnsping ora.example.comOn Windows:tnsping ora.example.comIf "TNS: no listener," the listener is down. Restart Oracle services. - Reinstall SQL Developer
If all else fails, uninstall and reinstall the application.
Note: If you encounter Oracle error 12593, the TNS listener is misconfigured. Verify the listener.ora file on the database server.
Frequently Asked Questions
How do I connect to a remote Oracle database?
Use the full connection string (username/password@hostname:port/SID) or a TNS alias. Example:
sqlplus scott/tiger@ora.example.com:1521/ORCL
If TNS is configured correctly, it will resolve the hostname to the database.
Can I use SQL Developer Command Line without the full IDE?
Yes, the command line operates independently of SQL Developer's GUI. You can use it directly after installation.
What if my script includes dynamic SQL?
Use the DBMS_SQL package for dynamic queries. Example:
DECLARE
v_cursor SYS_REFCURSOR;
BEGIN
OPEN v_cursor FOR EXECUTE IMMEDIATE 'SELECT * FROM dynamic_table';
FOR rec IN v_cursor LOOP
DBMS_OUTPUT.PUT_LINE(rec.column_name);
END LOOP;
END;
How do I handle errors in scripts executed via the command line?
Enable detailed error messages with:
SET ERRORSIZE 5000
Then run the script. Errors will appear in the command line output.
Can I export results from the command line directly to CSV?
No direct export exists, but you can use a pipeline:
sqlplus @script.sql | while read line; do echo "$line" | awk '{print $1","$2"..."}' > results.csv; done
What’s the difference between sqlplus and SQL Developer Command Line?
sqlplus is Oracle’s native command-line tool; SQL Developer Command Line extends it with features like spooling, stored procedures, and easier credential management. Use sqlplus for basic tasks if you lack SQL Developer.
For advanced use, consider integrating SQL Developer Command Line into scripts with Batch files (Windows) or shell (Linux/macOS).