Вы находитесь на странице: 1из 11

Week 7– PL/SQL Fundamentals –1

Madhu Bhimaraju
– two methods used to implement conditional processing (if then else) within a
sql statement are the case expression and the decode function (True/False)

– NVL function is used to convert a null value to an actual value (True/False)

– single row functions can be nested to any level (True/False)

– what is the difference between year format of YY and RR

– What is the function that is used to convert a value of number data type to
PL/SQL Introduction
– PL/SQL stands for “procedural Language extensions to the
– PL/SQL several defining characteristics are:
– It is highly structured, readable, and accessible language.
– PL/SQL is a standard and portable language for Oracle
– PL/SQL is an embedded language.
– PL/SQL is a high performance, highly integrated database language.

– Key elements of PL/SQL structure and functionality are

– Tight integration with sql.
– Control and conditional logic (If and case, for loop, while loop, goto
– Powerful mechanism of raising and handling execeptions.
PL/SQL Fundamentals
– In pl/sql fundamental unit of organization is block, which is at core of key language
concepts (Modularization, scope).
– Modularization: Build modules such as procedures, functions at which in turn become applications.
– Scope: The block provides a scope or context for logically related objects.
– Sections of pl/sql block – 4 different sections only one of them is mandatory.
– Header: Used only for named blocks. The header determines the way the named block or program must be
called – Optional.
– Declaration section: Identifies variables, cursors, and sub-blocks that are referenced in execution and
exception section – Optional.
– Execution section: Statements the PL/SQL runtime engine will execute at runtime. – Mandatory.
– Exception section: Handles exceptions to normal processing (warnings and error conditions) – Opt.
Declaration Section
Execution Section
Exception Section
New Features of PL/SQL in Oracle

– Oracle9i New features of PL/SQL

– Record based DML (% rowtype, ni need t declare individual
– Table functions.
– New and improved data types (timestamps, timezone
management, calculation of time intervals).
– Inheritance of object types.
– Enhancements to pl/sql collections
– Native compilation of pl/sql.
Example of a pl/sql procedure
– Following is the example of pl/sql procedure that has all four
sections as explained in previous slide.
Procedure get_learn (ename_in IN VARCHAR2) --- Header
Hiredate date; --- Declaration
Hiredate := sysdate-2; --- Execution
Insert into emp (ename, hiredate)
Values (ename_in, hiredate);
Exception --- Exception
When dup_val_on_index
DBMS_OUTPUT.PUT_LINE('Cannot Insert');
Various forms of pl/sql blocks.
– Anonymous blocks – unnamed block with either declare or
begin, cann’t be called by any other block
– Examples of anonymous blocks in different environments are:
Client side trigger, Database trigger, Script and compiled
– Named blocks – popular, and used in majority of applications.
Examples of named blocks include package, procedure,
function etc.,
– Nested blocks: A block lives inside another block, generally
used to have good control on scope of variables, exceptions
and modules.
PL/SQL programming components
• A pl/sql program consists of sequence of statements, each made
up of one or more lines of text/characters. These characters are
grouped together into lexical units, also called atomics of the
language because they are smaller individual components. A
lexical unit is in pl/sql is any of the following:
• Identifier
• Literal
• Delimiter
• Comment
z An identifier is a name for pl/sql object, including any of
the following.
– Constant, Scalar variable, Composite variable, Exception,
Procedure, Function, Package, Type, Cursor, Reserved word, Label.
z Default properties of pl/sql identifiers are:
– Upto 30 characters in length
– Must start with letter
– Can include dollar sign, underscore and pound sign
– Can not contain spaces.
z Avoid reserve words (Language keywords, identifiers
from standard package).
z Use v$reserved_words view to check reserve words.
Literals, Delimiter, comments,
z A literal is a value that is not represented by identifier, it is a simply
a value
z Number, string, boolean
z Semicolon delimeter: A pl/sql program is made up of series of
declarations and statements. These are defined logically as
opposed physically. In other words there are not terminated with
the physical end of line of code; instead, they are termincated with
a semicolon (;).
z Comments: A single line comment is initiated with two hyphens (--
) and multiline comment start with slash-asterisk (/*) and end with
an asterisk-slash (*/)
z Lables: Using a lable we can name a anonymous block for
duration of execution. The lable format is <<identifier>> where
identifier is valid pl/sql identifier with no terminator as ;
Faculty Information

– Instructor: Madhu Bhimaraju

– Phone: (949) 582-4900, X-3280
– Class: CIM 287A
– Ticket No: 20715
– Email: mbhimaraju@saddleback.edu
– http://www.saddleback.edu/faculty/mbhimaraju/