| Lesson 5 - Part 2 | Working with Query Files |
| Objective | Save a query to a file, replace a file, and edit a file. |
In earlier lessons you worked directly in the SQL*Plus buffer, either running statements immediately or editing them with the in-line editor or a text editor. In this lesson, you will learn how to save queries as script files, replace or append to existing files, and edit those files for reuse. Being able to store queries in files is essential for repeatable reporting, automation, and version-controlled development.
SQL*Plus provides several file-handling commands that interact with the SQL buffer and with scripts stored on disk. You will use these commands to:
The following commands form the foundation of working with query files in SQL*Plus. Modern SQL*Plus best practices favor START and @ for executing files, and SAVE for creating and maintaining script files.
| Command | Description |
| GET filename[.ext] [LIS[T] | NOL[IST]] |
Reads the contents of a script file into the SQL*Plus buffer.
If no option is specified, the file is listed as it is loaded.
Use NOLIST to load the file silently.
If you omit .ext, SQL*Plus assumes .sql by default.
|
| REM[ARK] text or -- textor /* text */ |
Adds comments to your script.
|
RUN |
Re-executes the current contents of the SQL*Plus buffer.
Modern SQL*Plus does not use RUN filename; to execute a file, use START or @ as shown below.
|
| SAV[E] filename[.ext] [CRE[ATE] | REP[LACE] | APP[END]] |
Writes the current SQL*Plus buffer to a file.
.sql is used by default.
|
|
STA[RT] filename[.ext] or @filename[.ext] or @@filename[.ext] |
Executes the commands in a script file.
|
SQL*Plus always keeps your most recent SQL or PL/SQL statement in memory in an area called the SQL buffer. You can display the buffer with the LIST command:
SQL> L
1 select *
2* from employees
SQL>
Typical workflows involve:
SAVE.START or @.GET and a text editor.Remember that each new statement you enter overwrites the SQL buffer. Once you begin working with files, your goal is to keep reusable logic in scripts rather than relying on the buffer alone.
By default, SQL*Plus saves and loads files in the
current directory
used by your SQL*Plus session. You can specify a full or relative path in the SAVE, GET, or START command to work with files in other directories.
The following example illustrates how to save a query to a script, verify that the file exists, and then run it:
PR_CUST.sql, enter:
SQL> SAVE PR_CUST
SQL*Plus automatically appends .sql if you omit the extension.
HOST (Windows) or ! or HOST (Linux/UNIX) to open a shell:
SQL> HOST
In the new shell, you can issue OS-level commands such as:
dir PR* -- Windows
ls PR* -- Linux/UNIX
When finished, exit the shell (for example, EXIT on Windows or exit on Linux/UNIX) to return to SQL*Plus.
SQL> START PR_CUST
or:
SQL> @PR_CUST
SQL*Plus assumes .sql if no extension is provided. The file is read, executed, and the results are displayed.
Use the SAVE command to protect useful statements from being lost when you overwrite the buffer:
PMT Process modeling techniques DSG 1
RSD Relational system design DSG 2
PRO Prototyping DSG 5
GEN System generation DSG 4
10 rows selected.
SQL> SAVE result1
SP2-0540: File "result1.sql" already exists.
Use "SAVE filename[.ext] REPLACE".
SQL> SAVE result1 REPLACE
Created file result1.sql
Key points:
SAVE filename creates it.REPLACE — overwrite the existing file with the current buffer.APPEND — add the buffer to the end of the existing file, useful when building scripts incrementally..sql extension. You can change the default extension using the SUFFIX setting (for example, to use .sqlplus or another convention), but .sql remains the most common choice.Once a script is saved, you can open it in a system text editor, make revisions, save the changes, and then reuse the script across environments and sessions. This pattern helps you move from ad-hoc experimentation to controlled, repeatable execution.
After a script has been created, you can edit it either from outside SQL*Plus (for example, using Notepad, Notepad++, VS Code, or vi) or by invoking the editor from SQL*Plus with EDIT filename:
SQL> EDIT PR_CUST
This opens PR_CUST.sql in your configured editor. After you save and close the editor, the file is updated on disk. You then run the updated script as usual:
SQL> @PR_CUST
When combined with source control systems (such as Git), this approach gives you a modern, auditable way to maintain database scripts while still leveraging the lightweight power of SQL*Plus.
In the next lesson, you will begin exploring SQL*Plus environment settings to control output formatting, pagination, and reporting options for the scripts you create.