Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 36 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
36
Dung lượng
236,19 KB
Nội dung
Interacting with Oracle
22
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć2
Interacting withOracle 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 withOracle 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 withOracle 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 withOracle 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
[...]... exception (Oracle7 Server error number -1422) The SELECT statement does not identify NO_DATA_FOUND exception (Oracle7 any rows 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 Interacting withOracle 22Ć15 22Ć16 Introduction to Oracle: ... (TO_CHAR(v_rows_deleted) ||’ rows deleted.’); END del_rows; Interacting withOracle 22Ć25 22Ć26 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Controlling Transactions Control the logic of transactions with COMMIT and ROLLBACK SQL commands, rendering some groups of database changes permanent, while discarding others As with Oracle7 , DML transactions start at the first command to follow a COMMIT... Syntax COMMIT [WORK]; where: WORK is for compliance with ANSI standards ROLLBACK Command ROLLBACK ends the current transaction by discarding all pending changes Syntax ROLLBACK [WORK]; where: WORK is for compliance with ANSI standards Note: A transaction is defined as a sequence of SQL statements Interacting withOracle 22Ć27 22Ć28 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Controlling... logic with Oracle7 Server savepoints based upon runtime conditions SAVEPOINT marks an intermediate point in the transaction processing Syntax SAVEPOINT savepoint_name; ROLLBACK TO SAVEPOINT discards pending changes made after the savepoint was marked Syntax ROLLBACK [WORK] TO [SAVEPOINT] savepoint_name; where: savepoint_name Interacting withOracle is a PL/SQL identifier 22Ć29 22Ć30 Introduction to Oracle: ... 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... 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 Interacting withOracle 22Ć13 22Ć14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 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... use SQL implicit cursor attributes to verify the outcome of these statements Interacting withOracle 22Ć31 22Ć32 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice Overview In this practice, you create three procedures to input, update, and delete information in a table, all using DML statements within a PL/SQL block Practice Contents D Creating a procedure to insert data into a table... Creating a procedure to update data in a table D Creating a procedure to delete a record from a table D Verifying your changes to the table using Oracle Procedure Builder built-ins and implicit cursor attributes Interacting withOracle 22Ć33 22Ć34 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice 22 If you are not already connected to the database, be sure to connect now 1 Create a... 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 Interacting withOracle 22Ć11 22Ć12 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Retrieving Data Using PL/SQL continued Avoid ambiguity in the WHERE clause by adhering to a naming convention that distinguishes database... Oracle7 Server error number +1403 In the “Processing Queries by Using Explicit Cursors” and “Error Handling” lessons, options for addressing these exceptions are addressed Interacting withOracle 22Ć17 22Ć18 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 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 . Interacting with Oracle
22
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć2
Interacting with Oracle 22Ć3
Objectives
In. 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