Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks...
-
Upload
evan-haynes -
Category
Documents
-
view
220 -
download
1
Transcript of Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks...
![Page 1: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/1.jpg)
Chapter 15Introduction to PL/SQL
Chapter 15Introduction to PL/SQL
![Page 2: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/2.jpg)
PL/SQLPL/SQL
Procedure Language SQL Advanced 4th generation programming
language
Procedure Language SQL Advanced 4th generation programming
language
![Page 3: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/3.jpg)
Advantages of PL/SQLAdvantages 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
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
![Page 4: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/4.jpg)
Types of BlocksTypes of Blocks
Function Procedure Anonymous block
Function Procedure Anonymous block
![Page 5: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/5.jpg)
FunctionFunction
Named block that is stored on the Oracle9i server
Accepts zero or more input parameters Returns one value
Named block that is stored on the Oracle9i server
Accepts zero or more input parameters Returns one value
![Page 6: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/6.jpg)
ProcedureProcedure
Named block Can process several variables Returns no values Interacts with application program using
IN, OUT, or INOUT parameters
Named block Can process several variables Returns no values Interacts with application program using
IN, OUT, or INOUT parameters
![Page 7: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/7.jpg)
Anonymous BlockAnonymous 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
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
![Page 8: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/8.jpg)
Basic Structure of a BlockBasic Structure of a Block
Has three sections:– Declarative– Executable– Exception-handling
Executable section is the only required section; the rest are optional
Has three sections:– Declarative– Executable– Exception-handling
Executable section is the only required section; the rest are optional
![Page 9: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/9.jpg)
Declarative SectionDeclarative 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
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
![Page 10: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/10.jpg)
Executable SectionExecutable Section
Identified by the BEGIN keyword Mandatory Can consist of several SQL and/or
PL/SQL statements
Identified by the BEGIN keyword Mandatory Can consist of several SQL and/or
PL/SQL statements
![Page 11: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/11.jpg)
Exception-handling SectionException-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
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
![Page 12: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/12.jpg)
END KeywordEND Keyword
Used to close a PL/SQL block Always followed by a semicolon
Used to close a PL/SQL block Always followed by a semicolon
![Page 13: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/13.jpg)
Example PL/SQL BlockExample PL/SQL Block
![Page 14: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/14.jpg)
Declaring a VariableDeclaring a Variable
Reserves a temporary storage area in the computer’s memory
Every variable must have:– A name– A datatype
Variables can be initialized
Reserves a temporary storage area in the computer’s memory
Every variable must have:– A name– A datatype
Variables can be initialized
![Page 15: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/15.jpg)
Variable NamesVariable Names
Variable name can consist of up to 30 characters, numbers, or special symbols
Variable name must begin with a character
Variable name can consist of up to 30 characters, numbers, or special symbols
Variable name must begin with a character
![Page 16: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/16.jpg)
ConstantsConstants
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
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
![Page 17: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/17.jpg)
PL/SQL DatatypesPL/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
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
![Page 18: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/18.jpg)
PL/SQL Scalar DatatypesPL/SQL Scalar Datatypes
![Page 19: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/19.jpg)
Variable InitializationVariable Initialization
Use DEFAULT keyword or (:=) assignment operator
Variable must be initialized if it is assigned a NOT NULL constraint
Use DEFAULT keyword or (:=) assignment operator
Variable must be initialized if it is assigned a NOT NULL constraint
![Page 20: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/20.jpg)
Variable Initialization ExamplesVariable Initialization Examples
![Page 21: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/21.jpg)
SELECT StatementSELECT Statement
Requires use of INTO clause to identify variable assigned to each data elementRequires use of INTO clause to identify variable assigned to each data element
![Page 22: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/22.jpg)
SELECT Statement ExampleSELECT Statement Example
![Page 23: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/23.jpg)
CursorsCursors
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
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
![Page 24: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/24.jpg)
Execution ControlExecution 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
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
![Page 25: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/25.jpg)
IF Statement SyntaxIF Statement Syntax
![Page 26: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/26.jpg)
IF Statement ExampleIF Statement Example
![Page 27: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/27.jpg)
Basic Loop SyntaxBasic Loop Syntax
![Page 28: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/28.jpg)
Basic Loop ExampleBasic Loop Example
![Page 29: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/29.jpg)
FOR Loop SyntaxFOR Loop Syntax
![Page 30: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/30.jpg)
FOR Loop ExampleFOR Loop Example
![Page 31: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/31.jpg)
WHILE Loop SyntaxWHILE Loop Syntax
![Page 32: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/32.jpg)
WHILE Loop ExampleWHILE Loop Example
![Page 33: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/33.jpg)
Nested LoopsNested 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
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
![Page 34: Chapter 15 Introduction to PL/SQL. Chapter Objectives Explain the benefits of using PL/SQL blocks versus several SQL statements Identify the sections.](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649f1a5503460f94c2f95b/html5/thumbnails/34.jpg)
Nested Loop ExampleNested Loop Example