380 likes | 461 Views
Chapter 15 Introduction to PL/SQL. Chapter Objectives. Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections of a PL/SQL block and describe their contents Identify the mandatory and optional sections of a PL/SQL block
E N D
Chapter 15Introduction to PL/SQL Oracle9i: SQL
Chapter Objectives • Explain the benefits of using PL/SQL blocks versus several SQL statements • Identify the sections of a PL/SQL block and describe their contents • Identify the mandatory and optional sections of a PL/SQL block • Identify an anonymous block and its use Oracle9i: SQL
Chapter Objectives • Describe how to execute a PL/SQL block • Explain the purpose of a variable • Explain the difference between a constant and a variable • Identify valid variable names • List the valid datatypes for PL/SQL variables Oracle9i: SQL
Chapter Objectives • Assign a dynamic datatype for a PL/SQL variable • Initialize a PL/SQL variable • Use DML statements in a PL/SQL block • Determine when it is appropriate to use an IF statement • Identify all the clauses of an IF statement, and state when they should be used Oracle9i: SQL
Chapter Objectives • Create an IF statement • Identify the purpose of a loop, and name the types of loops available in Oracle9i • Create a basic loop • Create a FOR loop • Create a WHILE loop Oracle9i: SQL
PL/SQL • Procedure Language SQL • Advanced 4th generation programming language Oracle9i: SQL
Advantages of PL/SQL • Can include error handling and control structures • Can be stored and used by various application programs or users • Allows for tighter security by granting privileges for executing stored procedures rather than directly on database object Oracle9i: SQL
Types of Blocks • Function • Procedure • Anonymous block Oracle9i: SQL
Function • Named block that is stored on the Oracle9i server • Accepts zero or more input parameters • Returns one value Oracle9i: SQL
Procedure • Named block • Can process several variables • Returns no values • Interacts with application program using IN, OUT, or INOUT parameters Oracle9i: SQL
Anonymous Block • Not stored since it cannot be referenced by a name • Usually embedded in an application program, stored in a script file, or manually entered when needed Oracle9i: SQL
Basic Structure of a Block • Has three sections: • Declarative • Executable • Exception-handling • Executable section is the only required section; the rest are optional Oracle9i: SQL
Declarative Section • Identified by the DECLARE keyword • Used to define variables and constants referenced in the block • Forward execution – variable and constants must be declared before they can be referenced Oracle9i: SQL
Executable Section • Identified by the BEGIN keyword • Mandatory • Can consist of several SQL and/or PL/SQL statements Oracle9i: SQL
Exception-handling Section • Identified by the EXCEPTION keyword • Used to display messages or identify other actions to be taken when an error occurs • Addresses errors that occur during a statement’s execution Oracle9i: SQL
END Keyword • Used to close a PL/SQL block • Always followed by a semicolon Oracle9i: SQL
Example PL/SQL Block Oracle9i: SQL
Declaring a Variable • Reserves a temporary storage area in the computer’s memory • Every variable must have: • A name • A datatype • Variables can be initialized Oracle9i: SQL
Variable Names • Variable name can consist of up to 30 characters, numbers, or special symbols • Variable name must begin with a character Oracle9i: SQL
Constants • Variables that have a value that does not change during the execution of the block • Optional CONSTANT keyword can be used to designate a constant in the block’s declarative section Oracle9i: SQL
PL/SQL Datatypes • Scalar – holds a single value • Composite – collection of grouped data treated as one unit • Reference – holds pointers to other program items • Large Object (LOB) – holds location of large objects Oracle9i: SQL
PL/SQL Scalar Datatypes Oracle9i: SQL
Variable Initialization • Use DEFAULT keyword or (:=) assignment operator • Variable must be initialized if it is assigned a NOT NULL constraint Oracle9i: SQL
Variable Initialization Examples Oracle9i: SQL
SELECT Statement Requires use of INTO clause to identify variable assigned to each data element Oracle9i: SQL
SELECT Statement Example Oracle9i: SQL
Cursors • Implicit cursor – created for DML operations or a SELECT statement that retrieves only one row of results • Explicit cursor – required for SELECT statements retrieving more than one row of results Oracle9i: SQL
Execution Control • IF statement – executes statements based on a condition • Basic loop – executes statements until condition in EXIT clause is TRUE • FOR loop – uses counter • WHILE loop – executes statements until condition is FALSE Oracle9i: SQL
IF Statement Syntax Oracle9i: SQL
IF Statement Example Oracle9i: SQL
Basic Loop Syntax Oracle9i: SQL
Basic Loop Example Oracle9i: SQL
FOR Loop Syntax Oracle9i: SQL
FOR Loop Example Oracle9i: SQL
WHILE Loop Syntax Oracle9i: SQL
WHILE Loop Example Oracle9i: SQL
Nested Loops • Any type of loop can be nested inside another loop • Execution of the inner loop must be completed before control is returned to the outer loop Oracle9i: SQL
Nested Loop Example Oracle9i: SQL