SQL Extensions   «Prev  Next»

Lesson 4 Using a Text Editor with SQL*Plus
Objective Change a Query by using a Text Editor.

Using a Text Editor with SQL*Plus

In the previous lesson you learned how to make quick fixes to a query using the SQL*Plus in-line editor. That approach works well for short statements and minor changes. In this lesson, you will learn how to use a full text editor to change a query stored in the SQL*Plus buffer, which is the preferred method when your query is longer, more complex, or you want the comfort of a modern editor.

SQL*Plus can launch an external text editor, write the current buffer to a temporary file, and then reload your changes when you save and close the editor. On Windows, the default editor is typically Notepad; on Linux and UNIX it is often vi or another console editor. You can also configure SQL*Plus to use editors such as Notepad++, VS Code, or any other tool installed on your system.

How SQL*Plus Uses a Text Editor

To send the current SQL*Plus buffer to your system editor, use the EDIT command:


EDIT

When you run EDIT:

For quick, one-off changes, you may continue to use the in-line editor. For multi-line queries, complex joins, or scripts you want to keep, using a text editor is faster, safer, and easier to maintain.

Step-by-Step: Changing a Query with a Text Editor

The following 12 images walk through a realistic workflow: starting from an existing query, opening it in a text editor, revising it, correcting an error, and finally running the successful version in SQL*Plus.

1) Starting query shown in SQL*Plus from previous lesson; this time the query will change significantly so a text editor is better.

SELECT FIRSTNAME, LASTNAME
FROM CUSTOMER C
WHERE C.CUST_ID
IN (1,2,3,4)
    
1) The query from the previous lesson is loaded in the SQL*Plus buffer. Because the query will be changed more extensively, using a text editor is more efficient than using the in-line editor.

Oracle SQL
2) Invoke the text editor by typing EDIT at the SQL prompt.
2) To invoke the text editor, type EDIT at the SQL prompt and press Enter.
3) SQL*Plus opens the current query in the configured text editor.
3) SQL*Plus writes the current query to a temporary file and opens it with your configured text editor (for example, Notepad on Windows or vi on Linux/UNIX).
4) Modify SELECT list and add additional lines in the text editor.
4) You can now freely edit the statement in the text editor—adding columns, changing predicates, and formatting the code for readability. When the changes are complete and ready to test, close the editor window.
5) Text editor prompts to save changes; choose Yes to persist your edits.
5) The editor prompts you to save your changes. Confirm (for example, click Yes) to save the file and return control to SQL*Plus.
6) SQL*Plus reloads the modified query into the buffer and displays it.
6) SQL*Plus reads the updated file back into the buffer and echoes the revised query in the SQL*Plus window so you can visually confirm the changes.
7) Execute the modified query by typing a forward slash at the SQL prompt.
7) To execute the modified query, type a forward slash (/) at the SQL prompt and press Enter.
8) An error appears; use EDIT again to reopen the query in the editor.
8) If the query produces an error (for example, a typo or incorrect column name), use EDIT again to reopen the statement in your text editor and correct the problem.
9) Make corrections in the text editor and prepare to save again.
9) Apply the necessary corrections in the text editor—such as fixing column names, join conditions, or GROUP BY clauses—then close the editor when finished.
10) Confirm save to persist the corrected query.
10) When prompted, save your changes again (for example, click Yes) to ensure SQL*Plus reloads the corrected version of the query.
11) SQL*Plus displays the final version, ready for execution.
11) SQL*Plus refreshes the buffer with the corrected query and displays it. The statement is now ready for execution. Run it again with /.
12) The query runs successfully and returns results.
12) The query executes successfully and the result set is displayed, confirming that you have successfully changed the query using a text editor.

Starting SQL*Plus and Connecting to a Database

To use a text editor with SQL*Plus, you must first connect to an Oracle database. Common connection patterns include:

  1. Open a terminal (Linux/UNIX) or command prompt (Windows) and start SQL*Plus:
    
    sqlplus
    
    When prompted, enter your Oracle username and password.
  2. Alternatively, specify the username directly:
    
    sqlplus username
    
    SQL*Plus will then prompt for your password.
  3. To connect to a specific database service (for example, in a multi-database or cloud environment), use a connect identifier:
    
    sqlplus username@connect_identifier
    
    You will be prompted for your password and, on success, SQL*Plus displays the SQL> prompt.

Once you see the SQL> prompt, you can enter SQL, PL/SQL, and SQL*Plus commands, and you can use EDIT to modify queries in your chosen text editor.

SQL Style and Readability

SQL*Plus itself is case-insensitive for keywords and object names that are not quoted. The following two statements are equivalent:


SeLect feaTURE, section, PAGE FROM newsPaPeR;

select Feature, Section, Page from NEWSPAPER;

Although case does not affect execution (unless you use quoted identifiers), consistent styling improves readability for you and your team. Common conventions include:

Adopting consistent style guidelines in your text editor makes it easier to review, share, and maintain scripts over time.

Using Line and Text Editors Together

In practice, you will often use both the in-line editor and a text editor:

A typical workflow is:

  1. Run a query from the SQL*Plus prompt and review the output or error message.
  2. Use EDIT to open the statement in your text editor.
  3. Refactor or extend the query (for example, add COUNT, SUM, or additional join conditions).
  4. Save and close the editor so SQL*Plus reloads the updated query into the buffer.
  5. Execute the revised query with / and review the results.

SQL*Plus Date Format

The way dates are displayed in SQL*Plus is controlled by the database NLS_DATE_FORMAT parameter. In some environments this may still use a two-digit year, which is not ideal for modern reporting. You can override the display format within your queries using:

When you are editing scripts in a text editor, it is good practice to standardize on an unambiguous date format such as 'YYYY-MM-DD' or an ISO-like format ('YYYY-MM-DD HH24:MI:SS') so that reports remain clear across regions and time zones.

Writing Scripts with a System Editor

Instead of editing only the current buffer, you can also use your system editor to maintain reusable SQL and PL/SQL scripts that you run regularly from SQL*Plus.

You can tell SQL*Plus which editor to use by setting the special _EDITOR variable:


DEFINE _EDITOR = vi

On Windows you might set this to notepad, notepad++, or code (for VS Code), depending on what you have installed. Include this definition in your site or user profile so that SQL*Plus always launches your preferred editor.

To create or edit a script file, use EDIT followed by a filename:


EDIT SALES

If you omit a file extension, SQL*Plus adds .SQL, so the script is saved as SALES.SQL. Inside the editor, you can compose any combination of SQL*Plus, SQL, and PL/SQL statements.

Example: Using a System Editor to Create a Script

Suppose you want to review salesperson performance each month. You can write a reusable script in your system editor:


COLUMN LAST_NAME        HEADING 'LAST NAME'
COLUMN SALARY           HEADING 'MONTHLY SALARY' FORMAT $99,999
COLUMN COMMISSION_PCT   HEADING 'COMMISSION %'   FORMAT 90.90

SELECT LAST_NAME,
       SALARY,
       COMMISSION_PCT
FROM   EMP_DETAILS_VIEW
WHERE  JOB_ID = 'SA_MAN';

Save this as SALES.SQL. To run the script from SQL*Plus, simply execute:


SQL> @SALES

By combining SQL*Plus with a modern text editor, you can quickly change individual queries, maintain a library of scripts, and align your workflow with current best practices for Oracle Database development and administration.


SEMrush Software