SQL Support
SQL Support
Introduction to Oracle 14 - 1
www.itdc.edu ITDC.010598v1.0
Session 14 SQL Support
PL/SQL supports the following SQL statements for manipulating Oracle data:
SELECT...INTO
INSERT
UPDATE
DELETE
COMMIT
ROLLBACK
SAVEPOINT
14 - 2 Introduction to Oracle
ITDC.010598v1.0 www.itdc.edu
Session 14 SQL Support
Queries
SELECT...INTO
SELECT...INTO is a special form of SELECT that allows you store the query results in
PL/SQL variables. The syntax is:
For example:
DECLARE
employee_name emp.ename%TYPE;
manager emp.mgr%TYPE;
my_job emp.job%TYPE;
BEGIN
SELECT ename, mgr, job INTO employee_name, manager,
my_job
FROM emp WHERE empno = 7201;
/* manipulate the variables */
END;
Note that the query must return exactly ONE row. If multiple rows or no rows are
returned, then an error occurs. (Cursors solve this problem and are discussed later.)
Introduction to Oracle 14 - 3
www.itdc.edu ITDC.010598v1.0
Session 14 SQL Support
For example:
DECLARE
employee_name CHAR(20) := SMITH;
salary NUMBER(8,2) := 1300;
BEGIN
INSERT INTO emp (empno, ename, job, hiredate, sal,
deptno)
VALUES (7020, employee_name, COOK, 20-DEC-93, salary,
30);
END;
Note that you can specify PL/SQL variables in the value list.
For example:
DECLARE
employee_name CHAR(20) := SMITH;
manager NUMBER(4) := 7980;
my_job CHAR(10) := VIP;
BEGIN
UPDATE emp SET mgr = manager
WHERE ename = employee_name OR job = my_job;
END;
Remember that an UPDATE without a WHERE clause updates all the rows in the
table.
14 - 4 Introduction to Oracle
ITDC.010598v1.0 www.itdc.edu
Session 14 SQL Support
For example:
DECLARE
job_title CHAR(10) := SALESMAN;
BEGIN
DELETE FROM emp
WHERE job = job_title;
END;
Remember that a DELETE without a WHERE deletes all the rows in a table.
Introduction to Oracle 14 - 5
www.itdc.edu ITDC.010598v1.0
Session 14 SQL Support
Transaction Control
PL/SQL supports the transaction control statements. These statements allow you
control when and if changes to ORACLE data are made permanent.
COMMIT [WORK];
Makes all changes made during the current transaction permanent. A transaction
is defined as everything done since the last commit. A COMMIT ends the current
transaction.
ROLLBACK [WORK];
Undoes all changes made during the current transaction. The changes do not
become permanent. A ROLLBACK ends the current transaction.
There are several conditions under which a commit or rollback occurs implicitly. For
example, if you exit SQL*Plus, any pending changes are committed.
Using Savepoints
A normal ROLLBACK rolls back all changes since the last commit. If you want to roll
back only part of a transaction, you must use savepoints. The commands are:
SAVEPOINT marker_name;
Marks and names a point in the current transaction to which you can later roll
back.
ROLLBACK [WORK] TO [SAVEPOINT] marker_name;
Undoes all changes made to the database since the specified savepoint.
Example:
BEGIN
INSERT INTO temp VALUES ...
SAVEPOINT A;
DELETE FROM temp WHERE ...
SAVEPOINT B;
UPDATE temp SET ....
SAVEPOINT C;
...
ROLLBACK TO SAVEPOINT B;
COMMIT; /* commits everything but the UPDATE */
END;
14 - 6 Introduction to Oracle
ITDC.010598v1.0 www.itdc.edu
Session 14 SQL Support
SQL Functions
Note that the group functions are only available on SQL statements. However, you
can use most of the single-row functions independent of SQL.
DECLARE
len NUMBER;
str CHAR(30);
BEGIN
str:= How long is this string;
len:= LENGTH(str);
END;
Introduction to Oracle 14 - 7
www.itdc.edu ITDC.010598v1.0
Session 14 SQL Support
Remote Access
PL/SQL allows you to access data on a remote ORACLE database. For example:
BEGIN
SELECT ename, job INTO my_name, my_job
FROM emp@phoenix
WHERE empno = my_empno;
...
Note that the DBA must have correctly configured SQL*Net for this query to work.
You can also use the %TYPE and %ROWTYPE attributes against a remote database. For
example:
DECLARE
emp_id emp.empno@phoenix%TYPE;
dept_rec dept@phoenix%ROWTYPE;
14 - 8 Introduction to Oracle
ITDC.010598v1.0 www.itdc.edu
Session 14 SQL Support
Introduction to Oracle 14 - 9
www.itdc.edu ITDC.010598v1.0
Session 14 SQL Support
Exercises
12.1 The ACME SPORTING GOODS COMPANY provides sporting goods and supplies to
its customers. The Order Entry System at ACME is an application that involves 4
tables in the database. These tables are described below:
14 - 10 Introduction to Oracle
ITDC.010598v1.0 www.itdc.edu
Session 14 SQL Support
Write a PL/SQL block that prompts the user for a valid order number (ORDID
between 601 and 621). For that order, find the corresponding number of ordered
items (NUMITEMS) in the ITEM table. Search the ITEM table for each product id
(PRODID) for the given order. With each PRODID and its corresponding ordered
quantity (QTY), access the PRODUCT table to deliver the order and to update the
PRODUCT table using the following rules:
If the product amount in stock (INSTOCK) is big enough to satisfy the order
(INSTOCK >= QTY), subtract QTY from INSTOCK and update the PRODUCT table.
If INSTOCK is less than QTY, replenish INSTOCK by adding 100 items to the stock
figure. If INSTOCK is still too low, write a message to that effect to the TEMP table. A
flow graph for this program illustrates the rules better.
instock = instock =
instock + 100 instock - qty
- qty
Introduction to Oracle 14 - 11
www.itdc.edu ITDC.010598v1.0
Session 14 SQL Support
14 - 12 Introduction to Oracle
ITDC.010598v1.0 www.itdc.edu