Upload
kenan
View
215
Download
0
Embed Size (px)
Citation preview
8/9/2019 Week 2b Where
1/25
Producing Readable Outputwith iSQL*Plus
8/9/2019 Week 2b Where
2/25
Objectives
After completing this lesson, you should be abke to do the following
Producing queries that require a substitutuin variableCustomize the iSQL*Plus environment
Produce more readable outputCreate and Execute script file
8/9/2019 Week 2b Where
3/25
Subtitution Variables
SAL = ?D EPTNO =?..,JOB=?
I want to querydifferent values.
8/9/2019 Week 2b Where
4/25
U sing iSQL*Plus, you can create reports that prompt the user to supply their ownvalues 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.
U se iSQL*Plus substitution variables to :
Store values temporarilySingle ampersand &D ouble ampersand &&D EFINE command
Pass variable values between SQL statements.Dynamically alter headers and footers
8/9/2019 Week 2b Where
5/25
U sing the & substitution variable
U se a variable prefixed with an ampersand (&) to prompt the user for a value
SELECT empno,ename,sal,deptnoFROM empWHERE empno= &employee number;
Variable as a number
Number Values with Substitution Variables
8/9/2019 Week 2b Where
6/25
SELECT empno,ename,sal,deptnoFROM empWHERE job= '& job ';
Character and D ate Values with Substitution Variables
Variable as a Character or D ate
8/9/2019 Week 2b Where
7/25
Specifying Column Names, Expressions, and Text
U se substitution variables to supplement the
following :
WHERE conditionsORDER BY clauses
Column ExpressionsTable namesEntire SELECT statements
Not only can you use the substitution variables in the WHERE clause of an SQLStatement, but these variables can also be used to substitute for column names,Expressions, or text.
8/9/2019 Week 2b Where
8/25
SELECT empno ,&COLU MN_NAMEFROM empWHERE &COND ITION ;
8/9/2019 Week 2b Where
9/25
SELECT empno,ename,job,&COL U MN_NAMEFROM empWHERE &COND ITIONOR D ER BY &OR D ER_COL U MN;
8/9/2019 Week 2b Where
10/25
SELECT empno,ename,job,&COL U MN_NAMEFROM empWHERE &COND ITIONOR D ER BY &OR D ER_COL U MN;
8/9/2019 Week 2b Where
11/25
Defining Substitution Variables
You can predefine variables using the iSQL*Plus D EFINE command.
D EFINE variable = value creates a user variable with the CHAR data type
If you need to predefine a variable that includes spaces, you must enclosethe value within single quotation marks when using the D EFINE command.
A D efine variable is available for the session.
Command Description
D EFINE variable=value Create a user variable with the CHAR data andassigns a value to it.
D EFINE variable D isplays the variable, its value,and its data type
D EFINE D isplays all user variables with their values,and datatypes
8/9/2019 Week 2b Where
12/25
D EFINE SALARY=800
SELECT ENAME,SAL FROM EMP WHERE SAL= &SALARY
8/9/2019 Week 2b Where
13/25
UN DEFI N E command
Variables are defined until you either :
Issue the U ND EFINE D command on a variable,
Exit iSQL*Plus
8/9/2019 Week 2b Where
14/25
U sing the && Substitution Variable
U se the double-ampersand (&&) if you want to reuse the variable value withoutprompting the user each time.
SELECT empno, ename ,&&COLU MN_NAME FROM emp order by &COLU MN_NAME;
8/9/2019 Week 2b Where
15/25
U sing the VERIFY Command
SET VERIFY O NSELECT empno,ename,sal,deptno
FROM empWHERE empno=&employee_number;
This is displayed if VERIFY is ON
8/9/2019 Week 2b Where
16/25
U sing the VERIFY Command
SET VERIFY OFFSELECT empno,ename,sal,deptno
FROM empWHERE empno=&employee_number;
8/9/2019 Week 2b Where
17/25
iSQL*Plus Format Commands
COL UMN
TTITLE
BTITLE
BREAK
8/9/2019 Week 2b Where
18/25
The COL U MN command
OPTION
S DESCRIPTION
SCLEAR Clears any column formatsHEAD ING Sets the column headingFORMAT Changes the display of the column dataNOPRINT Hide the columnsNU LL Specifies text to be displayed for null valuesPRINT Shows the columns
8/9/2019 Week 2b Where
19/25
8/9/2019 Week 2b Where
20/25
COLU MN FORMAT MO D EL
ELEMENT D ESCRIPTION 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
8/9/2019 Week 2b Where
21/25
U sing BREAK command
U se the BREAK command to suppress duplicates.
Break on deptno ;
Select * from emp order by deptno ;
CLEAR BREAK
8/9/2019 Week 2b Where
22/25
HEAD ERS AN D FOOTERS
D ISPLAY HEA D ERS AN D FOOTERS
TITLE O N BTITLE O NTTITLE OFF BTITLE OFF
Set the Report Header
TTITLE EM PLOYEE LIST
Set the Report Footer
BTITLE CO N FIDE N TAL
8/9/2019 Week 2b Where
23/25
TTITLE 'E M PLOYEES LIST'BTITLE 'CO N FIDE N TAL'SELECT * FRO M EM P;
8/9/2019 Week 2b Where
24/25
8/9/2019 Week 2b Where
25/25
4 -Write a script to display the ename, job, and department names for every employee ina given location. Search condition should allow for case-intensive searches of thedepartment location.
5-Modiyf the code of task 4 to create a report containing the department name,employee name, hiredate, salary and, and ANN U AL SALARY.