Saving and Retrieving the Buffer

SQL*Plus allows you to save the contents of the buffer to a file and to read that file back again. If you have built up a long and complicated SQL statement, you can save it for later reuse and save yourself the bother of figuring it all out again. Two commands, SAVE and GET, are provided for this purpose.

SAVE

Example 2-15 shows the SAVE command being used to save the contents of a long SQL query to a file. First, the query is entered into the buffer without being executed; then the SAVE command is used to write the buffer to a file.

Example 2-15. Writing the current buffer contents to a file

SQL> SELECT employee_name, project_name
  2  FROM employee JOIN project_hours
  3       ON employee.employee_id = project_hours.employee_id
  4  JOIN project
  5       ON project_hours.project_id = project.project_id
  6  AND employee_billing_rate IN (
  7         SELECT MAX(employee_billing_rate)
  8         FROM employee)
  9  GROUP BY employee_name, project_name
 10
SQL> SAVE highest_billed_emp_projects
Created file highest_billed_emp_projects.sql

The SAVE command in Example 2-15 creates a new file, with the default extension of .sql, and writes the contents of the buffer to that file. After writing the buffer contents, SQL*Plus writes a trailing forward slash on a line by itself, so the resulting output file looks like this:

SELECT employee_name, project_name FROM employee JOIN project_hours ON employee.employee_id = project_hours.employee_id JOIN project ON project_hours.project_id = project.project_id ...

Get Oracle SQL*Plus: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.