1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu interacting with oracle docx

36 325 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 36
Dung lượng 287,6 KB

Nội dung

Interacting with Oracle 22 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć2 Schedule: Timing Topic 40 minutes Lecture 45 minutes Practice 85 minutes Total Class Management Note: Files required for lesson. Demonstration: l22cust.pls, l22newsd.pls, l22delr.pls Practice: None 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 Class Management Note: Remind students that they will combine the commands covered in this lesson with the subprograms learned in the previous lessons. 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. Class Management Note: DBMS_SQL package is a new package available in Oracle7 Release 7.2. For more information about DBMS_SQL package, see Oracle7 Server Application Developer’s Guide, Release 7.2. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć6 Class Management Note: The INTO clause is required in a SELECT statement in PL/SQL. This is in contrast to SQL, where the INTO clause is forbidden. Output variables are PL/SQL variables through which values pass from the database out to the PL/SQL block. The datatype of an output variable must be compatible with the datatype of the source column, although the source column may be an expression; in particular, Boolean variables are not permitted. Input variables are PL/SQL variables through which values pass from the PL/SQL block into the database. 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. Class Management Note: On the board, write: v_sum_salaries := SUM (s_emp.salary), and draw a line through it to emphasize that group functions must be used in a SQL statement. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć10 Class Management Note: Question: Ask students what the record structure looks like. Answer: Three fields, ID, NAME, and REGION_ID. Question: Ask students how to reference the value in the NAME field of the record. Answer: Reference the value as DEPT_RECORD.NAME. [...]... 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 with Oracle 22Ć15 22Ć16 Introduction to Oracle: ... Enter: print g_rows_deleted 6 Roll back the changes when done Interacting with Oracle 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... your stored subprogram was executed with unhandled exceptions WORK adds no functionality to these commands Interacting with Oracle 22Ć27 22Ć28 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Controlling Transactions continued SAVEPOINT Command Alter the transactional logic with Oracle7 Server savepoints based upon runtime conditions SAVEPOINT marks an intermediate point in the transaction... the INTO clause must be PL/SQL variables Only in the WHERE clause is there the possibility of confusion Interacting with Oracle 22Ć13 Class Management Note: Exceptions are covered in a later lesson 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... the retrieved values Class Management Note: Note for page 22-16 Question: Ask students how to change the code to avoid receiving the error Answer: Name the identifiers with the v_ prefix Interacting with Oracle 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... How many rows are inserted? Answer: 2 Question: Which savepoints are erased? Answer: c Question: Which savepoints are kept? Answer: a and b Interacting with Oracle 22Ć29 22Ć30 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Summary You can embed SQL within the PL/SQL block You can use the following valid commands: Statement Description SELECT Retrieves exactly one row into scalar variables... can use SQL implicit cursor attributes to verify the outcome of these statements Interacting with Oracle 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 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 Class Management Note: Duration: 45 minutes For additional exercises, see Practice 26, Exercises 1a and 1d Interacting with Oracle 22Ć33 22Ć34 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice 22 If you are not already connected... 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... 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 Interacting with Oracle 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 . Answer: Name the identifiers with the v_ prefix. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder22Ć12 Interacting with Oracle 22Ć13 Retrieving. combine the commands covered in this lesson with the subprograms learned in the previous lessons. Interacting with Oracle 22Ć5 Overview When you need to extract

Ngày đăng: 21/12/2013, 06:17

w