Tải bản đầy đủ (.pdf) (36 trang)

Tài liệu interating with oracle ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (236.19 KB, 36 trang )

Interacting with Oracle
22
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć2
Interacting with Oracle 22Ć3
Objectives
In this lesson, you access the database and control transactions through SQL
statements in PL/SQL.
At the end of this lesson, you should be able to
D Use SELECT, INSERT, UPDATE, and DELETE commands in PL/SQL
subprograms.
D Determine the outcome of SQL statements by using implicit cursor attributes.
D Control transactions within PL/SQL.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć4
Interacting with Oracle 22Ć5
Overview
When you need to extract information from or apply changes to the database, you
must use SQL. PL/SQL supports full data manipulation language and transaction
control commands within SQL. You can use SELECT statements to populate
variables with values queried from a row in a table. Your DML commands can
process multiple rows.
Comparing SQL and PL/SQL Statement Types
D A PL/SQL block is not a transaction unit. Commits, savepoints, and rollbacks are
independent of blocks, but you can issue these commands within a block.
D PL/SQL does not support data definition language (DDL), such as CREATE
TABLE, ALTER TABLE, or DROP TABLE.
D PL/SQL does not support data control language (DCL), such as GRANT or
REVOKE.
D DBMS_SQL package allows you to issue DDL and DCL statements.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć6
Interacting with Oracle 22Ć7
Retrieving Data Using PL/SQL


Use the SELECT statement to retrieve data from the database. The SELECT
statement contains an additional mandatory clause: the INTO clause. In the INTO
clause, list the output variables for receiving the data. The SELECT statement must
return exactly one row or an error will occur.
Abridged Syntax
SELECT select_list
INTO variable_name | record_name
FROM table
WHERE condition;
where: select_list is a list of at least one column, and can include
SQL expressions, row functions, or group
functions.
variable_name is the scalar variable to hold the retrieved value.
record_name is the PL/SQL RECORD to hold the retrieved
values.
table specifies the database table name.
condition is composed of column names, expressions,
constants, and comparison operators, including
PL/SQL variables and constants.
Take advantage of the full range of Oracle7 Server syntax for the SELECT statement.
Guidelines
D Terminate each SQL statement with a semicolon (;).
D Assign values into PL/SQL tables in a loop by declaring an explicit cursor.
D The INTO clause is required for the SELECT statement when it is embedded
within PL/SQL.
D The WHERE clause is optional, and can be used to specify input variables,
constants, literals, or PL/SQL expressions.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć8
Interacting with Oracle 22Ć9
Retrieving Data Using PL/SQL continued

Guidelines
D Specify the same number of output variables in the INTO clause as database
columns in the SELECT clause. Be sure that they correspond positionally and that
their datatypes are compatible.
D Ensure that the datatype of the identifiers match the datatype of the columns by
using the %TYPE attribute. The datatype and number of variables in the INTO
clause match those in the SELECT list.
D Terminate the PL/SQL block with the END statement. You can add the name of
the subprogram after the keyword END for clarity.
D Include at least one RETURN statement in a function.
D Use group functions, such as SUM, in a SQL statement since group functions
apply to groups of rows in a table.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć10
Interacting with Oracle 22Ć11
Retrieving Data Using PL/SQL continued
A PL/SQL RECORD can be used to easily create fields that match a database table’s
columns. Each field has the same name and datatype as a column in the table. When
retrieving all columns from a table, use a PL/SQL RECORD to hold the retrieved
values.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć12
Interacting with Oracle 22Ć13
Retrieving Data Using PL/SQL continued
Avoid ambiguity in the WHERE clause by adhering to a naming convention that
distinguishes database column names from PL/SQL variable names.
Example
Retrieve the order date and the ship date for the specified order. This example raises
an unhandled runtime exception.
PROCEDURE order_info
(id s_ord.id%TYPE)
IS

date_ordered s_ord.date_ordered%TYPE;
date_shipped s_ord.date_shipped%TYPE;
BEGIN
SELECT date_ordered, date_shipped
INTO date_ordered, date_shipped
FROM s_ord
WHERE id = id;

END order_info;
PL/SQL> order_info (100);
ERROR 0 at line 1, column 0
Unhandled exception ORA-01422: exact fetch returns
more than requested number of rows
ORA-06512: at line 7
PL/SQL checks whether an identifier is a column in the database; if not, it is assumed
to be a PL/SQL identifier.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć14
Interacting with Oracle 22Ć15
SELECT Exceptions
SELECT statements within a PL/SQL block fall into the “Embedded SQL”ANSI
classification. Be sure that the SELECT statement retrieves exactly one row;
otherwise an exception is raised. An exception is an error that terminates a PL/SQL
block.
SELECT Exceptions
Condition
Exception
The SELECT statement identifies more
than one row.
TOO_MANY_ROWS exception
(Oracle7 Server error number -1422).

The SELECT statement does not identify
any rows.
NO_DATA_FOUND exception (Oracle7
Server error number +1403).
Note: Handle the raised exceptions with exception-handling routines, which will be
covered in a later lesson. Alternatively, fetch multiple rows one-by-one in a
loop by declaring an explicit cursor.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć16
Interacting with Oracle 22Ć17
SELECT Exceptions continued
TOO_MANY_ROWS Exception
When more than one record is identified with a SELECT statement, Oracle7 Server
raises an error number -1422, also referred to as TOO_MANY_ROWS, which is the
predefined exception name.
NO_DATA_FOUND Exception
When no rows are identified with a SELECT statement, the NO_DATA_FOUND
exception is raised, which is also Oracle7 Server error number +1403.
In the “Processing Queries by Using Explicit Cursors” and “Error Handling” lessons,
options for addressing these exceptions are addressed.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć18
Interacting with Oracle 22Ć19
Manipulating Data Using PL/SQL
Manipulate data in the database by using the DML commands.
D INSERT statement adds new rows of data to the table.
D UPDATE statement modifies existing rows in the table.
D DELETE statement removes unwanted rows from the table.
Inserting Data
When adding rows to a table, you can eliminate unnecessary IN arguments.
D Use SQL function, such as USER and SYSDATE.
D Generate primary key values by using database sequences.

D Derive values in the PL/SQL block.
D Add column default values.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć20
Interacting with Oracle 22Ć21
Manipulating Data Using PL/SQL continued
Updating and Deleting Data
There may be ambiguity in the SET clause of the UPDATE statement because
although the identifier on the left of the assignment operator is always a database
column, the identifier on the right can be either a database column or a PL/SQL
variable.
Remember that the WHERE clause is used to determine which rows are affected. If
no rows are modified, no error occurs, unlike the SELECT statement in PL/SQL.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć22
Interacting with Oracle 22Ć23
SQL Cursor
Whenever you issue a SQL command, the Server opens an area of memory in which
the command is parsed and executed. This area is called a cursor.
When the executable part of a block issues a SQL command, PL/SQL creates an
implicit cursor, which has the SQL identifier. PL/SQL manages this cursor
automatically. An explicit cursor is explicitly declared and named by the programmer.
There are four attributes available in PL/SQL that can be applied to cursors.
Note: More about explicit cursors will be covered in Lesson 24.
SQL Cursor Attributes
You can use these attributes in PL/SQL statements as you would functions, but not in
SQL commands. They are useful to evaluate the result of a DML operation. PL/SQL
does not consider a data manipulation language statement that affects no rows to have
failed, unlike the SELECT statement, which returns an exception.
Attribute
Description
SQL%ROWCOUNT Number of rows affected by the most recent SQL

statement (an integer value).
SQL%FOUND Boolean attribute that evaluates to TRUE if the most
recent SQL statement affects one or more rows.
SQL%NOTFOUND Boolean attribute that evaluates to TRUE if the most
recent SQL statement does not affect any rows.
SQL%ISOPEN Always evaluates to FALSE because PL/SQL closes
implicit cursors immediately after they are executed.
For more information, see
PL/SQL User’s Guide and Reference, Release 2.3, Chapter 4, “Implicit Cursor
Attributes” section.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć24
Interacting with Oracle 22Ć25
SQL Cursor Attributes continued
Example
Write a procedure to delete rows that have the specified order number from the item
table. Print the number of rows deleted to the screen.
PROCEDURE del_rows
(v_ord_id NUMBER)
IS
v_rows_deleted NUMBER;
BEGIN
DELETE FROM s_item
WHERE ord_id = v_ord_id;
v_rows_deleted := SQL%ROWCOUNT;
TEXT_IO.PUT_LINE (TO_CHAR(v_rows_deleted)
||’ rows deleted.’);
END del_rows;

×