Producing Readable Output
with iSQL*Plus
Objectives
After completing this lesson, you should be abke to do the following
•Producing queries that require a substitutuin variable
•Customize the iSQL*Plus environment
•Produce more readable output
•Create and Execute script file
Subtitution Variables
I want to query
different values.
…SAL = ?
…DEPTNO =?
..,JOB=?
Using iSQL*Plus, you can create reports that prompt the user to supply their own
values to restrict the range of data returned by using substitution variables.
You can embed substitution variables in a command file or in a single SQL statement.
A varaible can be thought as a container in which the values are temporarily stored.
When the statement is run, the value is substituded.
Use iSQL*Plus substitution variables to :
Store values temporarily
Single ampersand &
Double ampersand &&
DEFINE command
Pass variable values between SQL statements.
Dynamically alter headers and footers
Using the & substitution variable
Use a variable prefixed with an ampersand (&) to prompt the user for a value
Number Values with Substitution Variables
SELECT empno,ename,sal,deptno
FROM emp Variable as a number
WHERE empno=&employee number;
Character and Date Values with Substitution Variables
SELECT empno,ename,sal,deptno
FROM emp Variable as a Character or Date
WHERE job='&job';
Specifying Column Names, Expressions, and Text
Use substitution variables to supplement the
following :
•WHERE conditions
•ORDER BY clauses
•Column Expressions
•Table names
•Entire SELECT statements
Not only can you use the substitution variables in the WHERE clause of an SQL
Statement, but these variables can also be used to substitute for column names,
Expressions, or text.
SELECT empno,&COLUMN_NAME
FROM emp
WHERE &CONDITION ;
SELECT empno,ename,job,&COLUMN_NAME
FROM emp
WHERE &CONDITION
ORDER BY &ORDER_COLUMN;
SELECT empno,ename,job,&COLUMN_NAME
FROM emp
WHERE &CONDITION
ORDER BY &ORDER_COLUMN;
Defining Substitution Variables
You can predefine variables using the iSQL*Plus DEFINE command.
DEFINE variable = value creates a user variable with the CHAR data type
If you need to predefine a variable that includes spaces, you must enclose
the value within single quotation marks when using the DEFINE command.
A Define variable is available for the session.
Command Description
DEFINE variable=value Create a user variable with the CHAR data and
assigns a value to it.
DEFINE variable Displays the variable, its value,and its data type
DEFINE Displays all user variables with their values,and data
types
DEFINE SALARY=800
SELECT ENAME,SAL FROM EMP WHERE SAL=&SALARY
UNDEFINE command
Variables are defined until you either :
•Issue the UNDEFINED command on a variable,
•Exit iSQL*Plus
Using the && Substitution Variable
Use the double-ampersand (&&) if you want to reuse the variable value without
prompting the user each time.
SELECT empno, ename,&&COLUMN_NAME FROM emp order by &COLUMN_NAME;
Using the VERIFY Command
SET VERIFY ON
SELECT empno,ename,sal,deptno
FROM emp
WHERE empno=&employee_number;
This is displayed if
VERIFY is ON
Using the VERIFY Command
SET VERIFY OFF
SELECT empno,ename,sal,deptno
FROM emp
WHERE empno=&employee_number;
iSQL*Plus Format Commands
COLUMN
TTITLE
BTITLE
BREAK
The COLUMN command
OPTIONS DESCRIPTIONS
CLEAR Clears any column formats
HEADING Sets the column heading
FORMAT Changes the display of the column data
NOPRINT Hide the columns
NULL Specifies text to be displayed for null values
PRINT Shows the columns
COLUMN FORMAT MODEL
ELEMENT DESCRIPTION EXAMPLE
9 Single Zero-suppression digit 999999
0 Enforces leading zero 099999
$ Floating dollar sign $99999
L Local currency L99999
. Position of decimal point 9999.99
, Thousand seperator 9,9999
Using BREAK command
Use the BREAK command to suppress duplicates.
Break on deptno ;
Select * from emp order by deptno ;
CLEAR BREAK
HEADERS AND FOOTERS
DISPLAY HEADERS AND FOOTERS
TITLE ON BTITLE ON
TTITLE OFF BTITLE OFF
Set the Report Header
TTITLE ‘EMPLOYEE LIST’
Set the Report Footer
BTITLE ‘CONFIDENTAL’
TTITLE 'EMPLOYEES LIST'
BTITLE 'CONFIDENTAL'
SELECT * FROM EMP;
TASKS
1-Determine whether the following two statements are true or false
DEFINE & X =100
TRUE FALSE
2-DEFINE command is an SQL command
TRUE FALSE
3-Write a script to display the ename, job , and hiredates for all employees who started
within a given range of dates. Concatenate the name and job together, separated by a
space and comma, and label the column Employees. Use the DEFINE command to
provide the two ranges. Use the format DD/MON/YYYY
DEFINE lowdate=01/JAN/1980
DEFINE highdate=01/JAN/1981
4-Write a script to display the ename, job, and department names for every employee in
a given location. Search condition should allow for case-intensive searches of the
department location.
5-Modiyf the code of task 4 to create a report containing the department name,
employee name, hiredate, salary and, and ANNUAL SALARY.