• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

SQL*Plus Editing Commands

by admin

SQL is a command language that is used for communication with the Oracle server from any tool or application. Oracle SQL contains many extensions. When you enter a SQL statement, it is stored in a part of memory called the SQL buffer and remains there until you enter a new SQL statement. SQL*Plus is an Oracle tool that recognizes and submits SQL statements to the Oracle9i Server for execution. It contains its own command language.

SQL*Plus Editing Commands

Table below lists the most commonly used SQL*Plus editing commands:

Command Description
A[PPEND] text Adds text to the end of the current line
C[HANGE] / old / new Changes old text to new in the current line
C[HANGE] / text / Deletes text from the current line
CL[EAR] BUFF[ER] Deletes all lines from the SQL buffer
DEL Deletes current line
DEL n Deletes line n
DEL m n Deletes lines m to n inclusive
Note: You can enter only one SQL*Plus command for each SQL prompt. SQL*Plus commands are not stored in the buffer. To continue a SQL*Plus command on the next line, end the first line with a hyphen (-).

Using LIST, n, and APPEND

1. Use the L[IST] command to display the contents of the SQL buffer. The asterisk (*) beside line 2 in the buffer indicates that line 2 is the current line. Any edits that you made apply to the current line.

LIST
1 SELECT last_name
2* FROM employees

2. Change the number of the current line by entering the number (n) of the line that you want to edit. The new current line is displayed.

1
1* SELECT last_name

3. Use the A[PPEND] command to add text to the current line. The newly edited line is displayed. Verify the new contents of the buffer by using the LIST command.

A , job_id
1* SELECT last_name, job_id
Note: Many SQL*Plus commands, including LIST and APPEND, can be abbreviated to just their first letter. LIST can be abbreviated to L; APPEND can be abbreviated to A.

Using the CHANGE Command

1. Use L[IST] to display the contents of the buffer:

LIST
1* SELECT * from employees

2. Use the C[HANGE] command to alter the contents of the current line in the SQL buffer.In this case, replace the employees table with the departments table. The new current line is displayed:

c/employees/departments
1* SELECT * from departments

3. Use the L[IST] command to verify the new contents of the buffer:

LIST
1* SELECT * from departments

SQL*Plus File Commands

SQL statements communicate with the Oracle server. SQL*Plus commands control the environment, format query results, and manage files. You can use the commands described in the following table:

Command Description
SAV[E] filename [.ext] [REP[LACE]APP[END]] Saves the current contents of the SQL buffer to a file. Use APPEND to add to an existing file; use REPLACE to overwrite an existing file. The default extension is .sql.
GET filename [.ext] Writes the contents of a previously saved file to the SQL buffer. The default extension for the file name is .sql.
STA[RT] filename [.ext] Runs a previously saved command file
@ filename Runs a previously saved command file (same as START)
ED[IT] Invokes the editor and saves the buffer contents to a file named afiedt.buf
ED[IT] [filename[.ext]] Invokes the editor to edit the contents of a saved file
SPO[OL] [filename[.ext]| OFF|OUT] Stores query results in a file. OFF closes the spool file. OUT closes the spool file and sends the file results to the printer.
EXIT Quits SQL*Plus

Using the SAVE, START Commands

SAVE
Use the SAVE command to store the current contents of the buffer in a file. Thus, you can store frequently-used scripts for use in the future.

START
Use the START command to run a script in SQL*Plus. You can also, alternatively, use the symbol @ to run a script.

@my_query

Examples:

LIST
1 SELECT last_name, manager_id, department_id
2* FROM employees
SAVE my_query
Created file my_query
START my_query
LAST_NAME                 MANAGER_ID DEPARTMENT_ID
------------------------- ---------- -------------
King                                        90
Kochhar                       100           90
...
107 rows selected.

SERVEROUTPUT Command

Most of the PL/SQL programs perform input and output through SQL statements, to store data in database tables or query those tables. All other PL/SQL input/output is done through APIs that interact with other programs. For example, the DBMS_OUTPUT package has procedures, such as PUT_LINE. To see the result outside of PL/SQL requires another program, such as SQL*Plus, to read and display the data passed to DBMS_OUTPUT.

Use the SET SERVEROUT[PUT] command to control whether to display the output of stored procedures or PL/SQL blocks in SQL*Plus. The DBMS_OUTPUT line length limit is increased from 255 bytes to 32767 bytes. The default size is now unlimited. Resources are not preallocated when SERVEROUTPUT is set. Because there is no performance penalty, use UNLIMITED unless you want to conserve physical memory.

SET SERVEROUT[PUT] {ON | OFF} [SIZE {n | UNL[IMITED]}]
[FOR[MAT] {WRA[PPED] | WOR[D_WRAPPED] | TRU[NCATED]}]

SQL*Plus does not display DBMS_OUTPUT data unless you first issue the SQL*Plus command SET SERVEROUTPUT ON as follows:

SET SERVEROUTPUT ON
Note: SIZE sets the number of bytes of the output that can be buffered within the Oracle Database server. The default is UNLIMITED. n cannot be less than 2000 or greater than 1,000,000.

Using the SQL*Plus SPOOL Command

The SPOOL command stores query results in a file or optionally sends the file to a printer. The SPOOL command has been enhanced. You can now append to, or replace an existing file, where previously you could use SPOOL only to create (and replace) a file. REPLACE is the default.

Syntax:

SPO[OL] [file_name[.ext] [CRE[ATE] | REP[LACE] |
APP[END]] | OFF | OUT]

Spool Command Options:

Option Description
file_name[.ext] Spools output to the specified file name
CRE[ATE] Creates a new file with the name specified
REP[LACE] Replaces the contents of an existing file. If the file does not exist, REPLACE creates the file.
APP[END] Adds the contents of the buffer to the end of the file that you specify
OFF Stops spooling
OUT Stops spooling and sends the file to your computer’s standard (default) printer

To spool the output generated by commands in a script without displaying the output on screen, use SET TERMOUT OFF. SET TERMOUT OFF does not affect the output from commands that run interactively.

You must use quotation marks around file names that contain white space. To create a valid HTML file using SPOOL APPEND commands, you must use PROMPT or a similar command to create the HTML page header and footer. The SPOOL APPEND command does not parse HTML tags. SET SQLPLUSCOMPAT[IBILITY] to 9.2 or earlier to disable the CREATE, APPEND, and SAVE parameters.

Using the AUTOTRACE Command

It displays a report after the successful execution of SQL DML statements such as SELECT, INSERT, UPDATE, or DELETE. The report can now include execution statistics and the query execution path.

Syntax:

SET AUTOT[RACE] {ON | OFF | TRACE[ONLY]} [EXP[LAIN]]
[STAT[ISTICS]]
SET AUTOTRACE ON
-- The AUTOTRACE report includes both the optimizer
-- execution path and the SQL statement execution
-- statistics

EXPLAIN shows the query execution path by performing an EXPLAIN PLAN. STATISTICS displays SQL statement statistics. The formatting of your AUTOTRACE report may vary depending on the version of the server to which you are connected and the configuration of the server. The DBMS_XPLAN package provides an easy way to display the output of the EXPLAIN PLAN command in several predefined formats.

Filed Under: oracle, PL/SQL

Some more articles you might also be interested in …

  1. SQL query error when Using Shell Script
  2. What is spfile in Oracle Database
  3. Oracle Database 18c New Feature – Memoptimized Rowstore
  4. How To Calculate The Required Network Bandwidth Transfer Of Redo In Data Guard Environments
  5. How To Cancel A SQL Query In Oracle Database 18c
  6. Basics of client connectivity in Oracle Data Guard configuration
  7. Oracle RAC: How to modify private hostname, Private network IP & MTU
  8. Script to verify the Oracle DataPump Data Dictionary Catalog
  9. Oracle GoldenGate: Manager Sample Parameter File
  10. Starting, Stopping, and Checking the Status of the EM Cloud Control OMS

You May Also Like

Primary Sidebar

Recent Posts

  • qm Command Examples in Linux
  • qm wait Command Examples in Linux
  • qm start Command Examples in Linux
  • qm snapshot Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright