chapter 15 introduction to pl/sql. chapter objectives explain the benefits of using pl/sql blocks...

34
Chapter 15 Introduction to PL/SQL

Upload: evan-haynes

Post on 04-Jan-2016

223 views

Category:

Documents


1 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Nested Loop ExampleNested Loop Example