Producing Readable Output With SQL Plus: Objectives
Producing Readable Output With SQL Plus: Objectives
Producing Readable Output With SQL Plus: Objectives
with SQL*Plus
Chapter 8
Objectives
Lesson Aim
In this lesson, you will leam how to include SQL*Plus commands to produce
more readable SQL output.
You can create a command file containing a WHERE clause to restrict the rows
displayed. To change the condition each time the command file is m n. you use
substitution variable. Substitution variables can replace values in the WHERE
clause, a text string, and even a column or a table name.
1
Substitution Variables
2
Using the & Substitution Variable
Use a variable prefixed with an ampersand (&) to prompt the user
for a value.
SELECT empno, ename, sal, deptno
FROM emp
WHERE empno = &employee_num ;
3
Using the SET VERIFY Command
Toggling the display of the text of a command before and after
SQL*Plus replaces substitution variables with values.
SET VERiFY ON
SELECT empno, ename, sal, deptno
FROM emp
WHERE empno = &employee_num;
4
Character and Date Values
with
Substitution Variables
Use single quotation marks for date and character values.
In a WHERE clause, date and character values must be enclosed within single
quotation marks. The same rule applies to the substitution variables.
5
Specifying Character and Date Values
with Substitution Variables
To avoid entering quotation marks at runtime, enclose the variabie in single
quotation marks within the SQL statement itself.
The slide shows a query to retreive the employee name, department number, and
annual salary of all employees based on the job title entered at the prompt by the
user.
6
Using Functions with Substitution Variables (1)
You can also use functions such as UPPER and LOWER with the ampersand.
FROM emp
Use UPPER ( ‘&job_title’ ) so that the user does not have to enter the job title
in uppercase.
7
Using Functions with Substitution Variables (2)
Substitution variables may also be enclosed with single quotation marks inside
the UPPER and LOWER functions.
FROM emp
The slide example displays the use of UPPER ( ‘&job_title’ ). In this case the
user does not have to enter the job title in uppercase.
8
Specifying Column Names, Expressions, and Text at
Runtime
Use substitution variables to supplement the following:
•WHERE condition
•ORDER BY clause
•Column expression
•Table name
•Entire SELECTstatement
Not only can you use the substitution variable, but the WHERE clause of a SQL
statement, but also these variables can be used to substitute column names,
expressions, or text.
Example
Display the employee number and any other column and any condition of
employees.
EMPNO JOB
7782 MANAGER
7839 PRESIDENT
7934 CLERK
9
Specifying Column Names,
The slide example displays the employee number, name, job title, and any other column
specifıed by the user at runtime, from the EMP table. The user can also specify the condition
for retrieval of rows and the column name by which the resultant data has to be ordered.
10
Using the && Substitution Variable
Use the double-ampersand (&&) if you want to reuse the
variable value without prompting the user each time.
14 rows selected.
SQL*Plus stores the value supplied by using the DEFINE command: it will use it again
whenever you reference the variable name. Once a user variable is in place, you need to use
the UNDEFINE command to delete it.
11
SELECT empno, ename, job, &&column_name
FROM emp
ORDER BY &name ;
14 rows selected.
12
Defining User Variables
* You can predefine variables using one
of two SQL*Plus commands:
-DEFINE: Create a CHAR datatype
user variable
-ACCEPT: Read user input and store it
in a variable
• If you need to predefine a variable that
includes spaces, you must enclose the
value within single quotation marks
when using the DEFINE command.
Commad Description
DEFINE variable = value Creates a CHAR datatype user
variable and assigns a value to it
DEFINE variable Displays the variable, its value, and its
datatype
DEFINE Displays all user variables with value
and datatype
ACCEPT (see syntax on next slide) Reads aline of user input and stores it
in a variable
13
The ACCEPT Command
variable is the name of the variable that stores the value (If it does
not exist, SQL*Plus creates it.)
FOR[MAT] format specifies the format model – for example, A10 or 9.999
PROMPT text displays the text before the user can enter the value.
Note: Do not prefix the SQL*Plus substitution parameter with the ampersand
(&) when referencing the substitution parameter in the ACCEPT command.
14
Using the ACCEPT Command
ACCEPT dept PROMPT ‘ Provide the department name: ‘
SELECT *
FROM dept
WHERE dname = UPPER( ‘&dept’ ) ;
The ACCEPT command reads in a variable named DEPT. The prompt that it displays when
asking the user for the variable is ‘ Provide the department name: ‘. The SELECT statement
then takes the department value that the user enters and uses it to retrieve the appropriate row
from the DEPT table.
If the user enters a valid value for department name, the SELECT statemsnt executes in the
same way as any other SELECT statement, taking the user-entered value and using it in the
WHERE clause to compare with DNAME.
Note that the & character does not appear with the DEPT variable in the ACCEPT command.
The & appears only in the SELECT statement.
15
Using the DEFINE Command
DEFINE occupation=clerk
SELECT *
FROM emp
WHERE job = UPPER( '&occupation' )
Guidelines
The ACCEPT and DEFINE commands will create a variable if the variable does not exist;
these commands will automatically redefine a variable if it exists.
When using the DEFINE command, use single quotation marks (' ') to enclose a string that
contains an embedded space.
16
DEFINE and UNDEFINE Commands
• A variable remains defined until you either:
-Use the UNDEFINE command to clear it
-Exit SQL*Plus
•You can verify your changes with the
DEFINE command.
•To define variables for every session,
modify your login.sql file so that the
variables are created at startup.
When you undefine variables, you can verify your changes with the DEFINE
command. When you exit SQL*Plus, variables defined during that session are
lost. To define those variables for every session, modify your login.sql file so
that those variables are created at startup.
17
Using the DEFINE Command
Create a variable to hold the department name.
SQL> DEFINE deptname = sales
SELECT *
FROM dept
WHERE dname = UPPER('&deptname') ;
You can use the DEFINE cornmand to create a variable and then use the
variables as you would any other variable. The example on the slide creates a
variable DEPTNAME that contains the department name SALES. The SQL
statement then uses this variable to display the number and location of the sales
department.
UNDEFINE deptname
DEFINE deptname
18
Customizing the SQL*Plus Environment
• Use SET commands to control current session.
In the syntax:
You can verify what you have set by using the SHOW command. The SHOW
command on the slide checks whether ECHO had been set on or off.
To see all SET variable values, use the SHOW ALL command.
19
SET Command Variables
•ARRAYSIZE {20 | n}
•COLSEP {_ | text}
•FEEDBACK {6 | n |OFF | ON}
•HEAD1NG (OFF l ON>
•LINESIZE {80 | n}
•LONG {80 | n}
•PAGESIZE {24 | n}
•PAUSE (OFF | ON | text}
•TERMOUT {OFF | ON}
20
Saving Customizations
in the login.sql File
The login.sql file contains standard SET and other SQL*Plus
commands that are implemented at login.
You can modify login.sql to contain additional SET commands.
The login.sql file contains standard SET and other SQL*Plus commands that yon
may require for every session. The file is read and commands are implemented at
login. When you log out of your session, all customized settings are lost.
The settings implemented by login.sql can be changed during the current session.
Changes made are current only for that session. As soon as you log out, those
settings are lost.
21
SQL*Plus Format Commands
TTITLE [texf|OFF|ON]
BTITLE [texf|OFF|ON]
22
The COLUMN Command
Controls display of a column
COL[UMN] [{column | alias} [option] ]
•CLE[AR]: Clears any column formats
•FOR[MAT] format Changes the display
of the column using a format model
•HEA[DING] text Sets the column
heading
•JUS[TIFY] {align}: Aligns the column
heading to be left, center, or right
23
Using the COLUMN Command
Createcolumn headings.
24
COLUMN Format Models
The Oracle Server displays a string of pound sıgns (#) in place of a whole
number whose digits exceed the number of digils provided in the format model.
It also displays pound signs in place of a value whose format model is
alphanumeric but whose actual value is numeric.
25
Using the BREAK Command
• To suppress duplicates
SQL> BREAK ON ename ON job
26
Using the TTITLE and BTITLE
Commands
Display headers and footers.
TTI[TLE] [text \OFF|ON]
27
Creating a Script File
to Run a Report
1.Create the SOL SELECT statement.
2.Save the SELECT statement to a script file.
3.Load the script file into an editör.
4.Add formatting commands before the
SELECT statement.
5.Verify that the termination character
follovvs the SELECT statement.
You can either enter each of the SQL*Plus commands at the SQL prompt or put
all the commands, includmg the SELECT statement, in a command (or script)
file. A typical script consists of at least one SELECT statement and several
SQL*Plus commands.
1. Create the SQL SELECT statement at the SQL prompt. Ensure that the data
required for the report is accurate before you save the statement to a file and
apply formatting commands. Ensure that the relevant ORDER BY clause is
included if you intend to use breaks
28
Creating a Script File
to Run a Report
6.Clear formatting commands after the
SELECTstatement.
7.Save the script file.
8.Enter "START filename" to run the script.
8 In SQL*Plus, run the script file by entering START file name or @filename.
This command is required to read and execute the script file.
Guidelines
Include reset commands at the end of the file to restore the original
SQL*Plus environment.
29
Summary
•Use SQL*Plus substitution variables to
temporarily store values.
•Use SET commands to control current
SQL*Plus environment.
•Use the COLUMN command to control
the display of a column.
•Use the BREAK command to suppress
duplicates and section rows.
•Use TTITLE and BTITLE to display
headers and footers.
30
Practice 8
31
Saving Customizations in the login.sql File
32
The ACCEPT Command
33
Using the && Substitution Variable
Use the double-ampersand (&&) if you want to reuse the
variable value vvithout prompting the user each time.
34