Download Experiment 1.6 How to implement parameterized SQL scripts

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

History of writing wikipedia , lookup

Writing system wikipedia , lookup

Blackletter wikipedia , lookup

Screenwriting wikipedia , lookup

Transcript
.6
CSCI315 Database Design and Implementation
Experiment 1.6
How to implement parameterized SQL scripts ?
Experimented and described by Dr. Janusz R. Getta
School of Computer Science and Software Engineering, University of Wollongong, Australia,
Bldg. 3, room 210, phone +61 02 42214339, fax +61 02 42214170,
e-mail: [email protected], Web: http://www.uow.edu.au/∼jrg, Msn: jgetta, Skype: jgetta007
Table of contents
Step 0 How to begin and what you need to know before you start ?
Step 1 How to create a simple parameterized SQL script ?
Step 2 How to execute a parameterized SQL script ?
Step 3 How to trace the parameter substitutions in SQL script ?
Step 4 How to create a parameterized SQL script with a positional parameter ?
Step 5 How to end the experiment ?
References
Actions
Step 0 How to begin and what you need to know before you start ?
A printable copy of this experiment in pdf format is available here .Dowload and uncompress
SQL scripts used in Homework 1. Use cd command to navigate to a folder where the
downloaded and uncompressed SQL scripts are located. Start SQL*Plus client in a way
described in either Experiment 1.1 for XP operating system or in Experiment 1.2 for Linux
operating system. Connect as a user STUDENT . A password is: student .In this experiment
we shall use a sample relational database that consists of a relational table EMPLOYEE . The
0
These are the specifications of the homeworks in a subject Database Design and Implementation
(CSCI315) delivered in January 2009 at Singapore Institute of Management by Dr. Janusz R. Getta
1-1
Experiment 1.6: How to implement parameterized SQL scripts ?
1-2
relational table contains information about the employees. Remain connected as a user
STUDENT . To create and to load data into a sample database execute a script dbcreate16.sql .Next, execute a statement:
DESCRIBE EMPLOYEE
and familiarize yourself with a schema of EMPLOYEE table. To list the contents of the relational table execute a script select1.sql .
Step 1 How to create a simple parameterized SQL script ?
Sometimes SQL script must be executed several times, each time with a bit different values
of numeric or string constants. For example, we need information about an employee whose
number (E#) is 7839 . Next, we need information about an employee whose number is
7844 and so on. If the modification of a script takes too much time then a simple solution
is to replace a constant with a parameter. The following statements of SQL and SQL*Plus
implement a script select2.sql that displays a number and name of an employee with a given
number:
ACCEPT ENUM CHAR PROMPT ’EMPLOYEE NUMBER>’
SELECT E#, ENAME
FROM EMPLOYEE
WHERE E# = &ENUM;
A statement of SQL*Plus:
ACCEPT ENUM NUMBER PROMPT ’Employee number>’
displays a prompt EMPLOYEE NUMBER> and waits until a user enters a value. Then, the value
typed in (try 7900 ) by a user substitutes all occurrences of a parameter &ENUM in a script.
After all substitutions are made the script is executed. SQL*Plus statement:
SET VERIFY OFF
sets off the tracing of the parameter substitutions in a running script.
Step 2 How to execute a parameterized SQL script ?
While connected as a user STUDENT , execute a script select2.sql . The system should display
a prompt:
Experiment 1.6: How to implement parameterized SQL scripts ?
1-3
Employee number>
Type in a number, e.g. 7934 or 7839 or 7788 and press Enter. The system should display a number and name of an employee with a number just typed in.
Step 3 How to trace the parameter substitutions in SQL script ?
While connected as a user STUDENT , edit a script select2.sql . Replace a line:
SET VERIFY OFF
with a line:
SET VERIFY ON
and re-run the script. The system should report all substitutions of a paremeter &ENUM with
a value typed in by a user.
Step 4 How to create a parameterized SQL script with a positional parameter ?
SQL script does not necessarily need to prompt a user about a value of runtime parameter.
A value of parameter can be provided in ”inline” mode when starting a script. For example
the statements listed below implement a script select3.sql that finds a number and name of
an employee whose name contains a given string of characters.
SET VERIFY OFF
SELECT E#, ENAME
FROM EMPLOYEE
WHERE ENAME LIKE ’%&1%’;
Remain connected as a user STUDENT . To execute the script type:
@select3 JONES
Execution of the script finds the numbers and names of all employees whose name contains a
string ’JONES’ The positional parameters are denoted inside a script with &1, &2, ... and
so on. If the actual values of parameters are not provided within an execution line then the
Experiment 1.6: How to implement parameterized SQL scripts ?
1-4
system takes the values from the previous executions or automatically prompts about the
missing values. For example, execute the script in the following way:
@select3
Step 5 How to end the experiment ?
Remain connected as a user STUDENT . To conclude the experiment execute a script dbdrop16.sql to drop a sample database.
References
SQL*Plus User’s Guide and Reference, ACCEPT
SQL*Plus User’s Guide and Reference, SET VERIFY
SQL*Plus User’s Guide and Reference, Substitution variables