Вы находитесь на странице: 1из 12

SESSION 14 SQL Support

What you will learn

Support for SQL


Queries
Transaction statements
SQL functions
Remote access

Introduction to Oracle 14 - 1
www.itdc.edu ITDC.010598v1.0
Session 14 SQL Support

Support for SQL

PL/SQL supports the following SQL statements for manipulating Oracle data:

SELECT...INTO
INSERT
UPDATE
DELETE

The transaction processing statements are also supported:

COMMIT
ROLLBACK
SAVEPOINT

In addition, PL/SQL supports:

All SQL functions


All SQL comparison operators

However, PL/SQL does NOT support DDL or DCL statements.

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:

SELECT col1, col2, ... INTO var1, var2 ...


FROM table_name(s) WHERE...

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

The INSERT Statement


The INSERT statement allows you to insert data into an ORACLE table. The PL/SQL
syntax is the same as the SQL syntax:

INSERT INTO table name [(col1, col2, ...)]


VALUES (val1, val2, ...);

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.

The UPDATE Statement


The UPDATE statement allows you to modify data in an ORACLE table. The PL/SQL
syntax is the same as the SQL syntax:

UPDATE table SET


col1 = val1 [, col2 = val2, ...]
WHERE condition;

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

The DELETE Statement


The DELETE statement allows you remove one or more records from an ORACLE
table. The PL/SQL syntax is the same as the SQL syntax:

DELETE FROM table


WHERE condition;

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.

The transaction control statements are:

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

PL/SQL supports all the SQL functions, including:

Numeric (e.g., ABS, MOD, POWER, ROUND, SQRT, TRUNC)


Character (e.g., CHR, INITCAP, LOWER, LPAD, SUBSTR)
Date (e.g., ADD_MONTHS, LAST_DAY, MONTHS_BETWEEN, NEW_TIME,
NEXT_DAY)
Group (e.g., AVG, COUNT, MAX, MIN, STDDEV, SUM)

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

Topics for review

14.1 List the SQL statements that PL/SQL supports.

14.2 List two types of queries.

14.3 List two transaction control statements.

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:

SQL> desc product


Name Null? Type
------------------------------- -------- ----
PRODID NUMBER(6)
DESCRIP VARCHAR2(30)
SQL> desc price
Name Null? Type
------------------------------- -------- ----
PRODID NOT NULL NUMBER(6)
STDPRICE NUMBER(8,2)
MINPRICE NUMBER(8,2)
STARTDATE DATE
ENDDATE DATE
SQL> desc ord
Name Null? Type
------------------------------- -------- ----
ORDID NOT NULL NUMBER(4)
ORDERDATE DATE
COMMPLAN VARCHAR2(1)
CUSTID NOT NULL NUMBER(6)
SHIPDATE DATE
TOTAL NUMBER(8,2)
SQL> desc item
Name Null? Type
------------------------------- -------- ----
ORDID NOT NULL NUMBER(4)
ITEMID NOT NULL NUMBER(4)
PRODID NUMBER(6)
ACTUALPRICE NUMBER(8,2)
QTY NUMBER(8)
ITEMTOT NUMBER(8,2)

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.

accept order for


start find numitems each
number
in item table item

message Y instock Y select from


instock
to temp +100 product table
< qty
table < qty
N N

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

Вам также может понравиться