SlideShare a Scribd company logo
1 of 33
Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
16
Declaring Variables
16-2 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Objectives
After completing this lesson, you should
be able to do the following:
• List the benefits of PL/SQL
• Recognize the basic PL/SQL block and
its sections
• Describe the significance of variables in
PL/SQL
• Declare PL/SQL variables
• Execute a PL/SQL block
16-3 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
About PL/SQL
• PL/SQL is an extension to SQL with
design features of programming
languages.
• Data manipulation and query
statements of SQL are included within
procedural units of code.
16-4 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Benefits of PL/SQL
Integration
Application
Oracle Server
Shared
library
16-5 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Benefits of PL/SQL
Application Other DBMSs
Application
Oracle with
PL/SQL
SQL
SQL
SQL
SQL
SQL
IF...THEN
SQL
ELSE
SQL
END IF;
SQL
Improve performance
16-6 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
PL/SQL Block Structure
• DECLARE – Optional
Variables, cursors, user-defined exceptions
• BEGIN – Mandatory
– SQL statements
– PL/SQL statements
• EXCEPTION – Optional
Actions to perform when errors occur
• END; – Mandatory
DECLARE
BEGIN
EXCEPTION
END;
16-7 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
PL/SQL Block Structure
DECLARE
v_variable VARCHAR2(5);
BEGIN
SELECT column_name
INTO v_variable
FROM table_name;
EXCEPTION
WHEN exception_name THEN
...
END;
DECLARE
BEGIN
EXCEPTION
END;
16-8 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Block Types
Anonymous Procedure Function
[DECLARE]
BEGIN
--statements
[EXCEPTION]
END;
PROCEDURE name
IS
BEGIN
--statements
[EXCEPTION]
END;
FUNCTION name
RETURN datatype
IS
BEGIN
--statements
RETURN value;
[EXCEPTION]
END;
16-9 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Program Constructs
Anonymous
block
Application
trigger
Stored
procedure/
function
Database
trigger
Application
procedure/
function
Packaged
procedure/
function
DECLARE
BEGIN
EXCEPTION
END;
16-11 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Use of Variables
Use variables for:
• Temporary storage of data
• Manipulation of stored values
• Reusability
• Ease of maintenance
16-12 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Handling Variables in PL/SQL
• Declare and initialize variables in the
declaration section.
• Assign new values to variables in the
executable section.
• Pass values into PL/SQL blocks through
parameters.
• View results through output variables.
16-13 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Types of Variables
• PL/SQL variables:
– Scalar
– Composite
– Reference
– LOB (large objects)
• Non-PL/SQL variables: Bind and host
variables
16-15 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
TRUE
Types of Variables
25-OCT-99
Atlanta
“Four score and seven years ago
our fathers brought forth upon
this continent, a new nation,
conceived in LIBERTY, and dedicated
to the proposition that all men
are created equal.”
256120.08
16-16 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Declaring PL/SQL Variables
Syntax
Examples
identifier [CONSTANT] datatype [NOT NULL]
[:= | DEFAULT expr];
Declare
v_hiredate DATE;
v_deptno NUMBER(2) NOT NULL := 10;
v_location VARCHAR2(13) := 'Atlanta';
c_comm CONSTANT NUMBER := 1400;
16-17 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Declaring PL/SQL Variables
Guidelines
• Follow naming conventions.
• Initialize variables designated as NOT
NULL and CONSTANT.
• Initialize identifiers by using the
assignment operator (:=) or the
DEFAULT reserved word.
• Declare at most one identifier per line.
16-18 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Naming Rules
• Two variables can have the same name,
provided they are in different blocks.
• The variable name (identifier) should
not be the same as the name of table
columns used in the block.
DECLARE
empno NUMBER(4);
BEGIN
SELECT empno
INTO empno
FROM emp
WHERE ename = 'SMITH';
END;
16-19 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Assigning Values to Variables
v_ename := 'Maduro';
v_hiredate := '31-DEC-98';
Syntax
Examples
Set a predefined hiredate for new
employees.
Set the employee name to Maduro.
identifier := expr;
16-20 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Variable Initialization and
Keywords
Using:
• Assignment operator (:=)
• DEFAULT keyword
• NOT NULL constraint
16-21 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Scalar Datatypes
• Hold a single value
• Have no internal components
25-OCT-99
Atlanta
“Four score and seven years
ago our fathers brought
forth upon this continent, a
new nation, conceived in
LIBERTY, and dedicated to
the proposition that all men
are created equal.”
TRUE
256120.08
16-22 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Base Scalar Datatypes
• VARCHAR2 (maximum_length)
• NUMBER [(precision, scale)]
• DATE
• CHAR [(maximum_length)]
• LONG
• LONG RAW
• BOOLEAN
• BINARY_INTEGER
• PLS_INTEGER
16-24 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Scalar Variable Declarations
v_job VARCHAR2(9);
v_count BINARY_INTEGER := 0;
v_total_sal NUMBER(9,2) := 0;
v_orderdate DATE := SYSDATE + 7;
c_tax_rate CONSTANT NUMBER(3,2) := 8.25;
v_valid BOOLEAN NOT NULL := TRUE;
Examples
16-25 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
The %TYPE Attribute
• Declare a variable according to:
– A database column definition
– Another previously declared variable
• Prefix %TYPE with:
– The database table and column
– The previously declared variable
name
16-26 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Declaring Variables
with the %TYPE Attribute
Examples
...
v_ename emp.ename%TYPE;
v_balance NUMBER(7,2);
v_min_balance v_balance%TYPE := 10;
...
16-27 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Declaring Boolean Variables
• Only the values TRUE, FALSE, and
NULL can be assigned to a Boolean
variable.
• The variables are connected by the
logical operators AND, OR, and NOT.
• The variables always yield TRUE,
FALSE, or NULL.
• Arithmetic, character, and date
expressions can be used to return a
Boolean value.
16-28 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
1 5000
2 2345
3 12
4 3456
1 SMITH
2 JONES
3 NANCY
4 TIM
PL/SQL table structure PL/SQL table structure
BINARY_INTEGER
VARCHAR2
BINARY_INTEGER
NUMBER
PL/SQL Record Structure
TRUE 23-DEC-98 ATLANTA
16-29 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
LOB Datatype Variables
Book
(CLOB)
Photo
(BLOB)
Movie
(BFILE)
NCLOB
16-30 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Bind Variables
Server
O/S
Bind variable
16-32 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Referencing Non-PL/SQL
Variables
Store the annual salary into a SQL*Plus
host variable.
• Reference non-PL/SQL variables as
host variables.
• Prefix the references with a colon (:).
:g_monthly_sal := v_sal / 12;
16-33 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
DBMS_OUTPUT.PUT_LINE
• An Oracle-supplied packaged procedure
• An alternative for displaying data from a
PL/SQL block
• Must be enabled in SQL*Plus with
SET SERVEROUTPUT ON
16-34 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Summary
• PL/SQL blocks are composed of
the following sections:
– Declarative (optional)
– Executable (required)
– Exception handling (optional)
• A PL/SQL block can be an
anonymous block, procedure, or
function.
DECLARE
BEGIN
EXCEPTION
END;
16-35 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Summary
• PL/SQL identifiers:
– Are defined in the declarative section
– Can be of scalar, composite,
reference, or LOB datatype
– Can be based on the structure of
another variable or database object
– Can be initialized
16-36 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Practice Overview
• Determining validity of declarations
• Developing a simple PL/SQL block
16-40 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.

More Related Content

What's hot

Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentationNITISH KUMAR
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Proceduresrehaniltifat
 
SQL Queries
SQL QueriesSQL Queries
SQL QueriesNilt1234
 
pl/sql online Training|sql online Training | iTeknowledge
pl/sql online Training|sql online Training | iTeknowledgepl/sql online Training|sql online Training | iTeknowledge
pl/sql online Training|sql online Training | iTeknowledgeMasood Khan
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries LectureFelipe Costa
 
Inner join and outer join
Inner join and outer joinInner join and outer join
Inner join and outer joinNargis Ehsan
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQLrehaniltifat
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptgourav kottawar
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...rehaniltifat
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING ARADHYAYANA
 

What's hot (20)

Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Procedures
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
pl/sql online Training|sql online Training | iTeknowledge
pl/sql online Training|sql online Training | iTeknowledgepl/sql online Training|sql online Training | iTeknowledge
pl/sql online Training|sql online Training | iTeknowledge
 
Oracle Index
Oracle IndexOracle Index
Oracle Index
 
PLSQL
PLSQLPLSQL
PLSQL
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
Advanced sql
Advanced sqlAdvanced sql
Advanced sql
 
Inner join and outer join
Inner join and outer joinInner join and outer join
Inner join and outer join
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
 
MYSQL join
MYSQL joinMYSQL join
MYSQL join
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) ppt
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil
 
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
 

Similar to Les16[1]Declaring Variables

Les17[1] Writing Executable Statements
Les17[1] Writing Executable StatementsLes17[1] Writing Executable Statements
Les17[1] Writing Executable Statementssiavosh kaviani
 
Dbms oracle
Dbms oracle Dbms oracle
Dbms oracle Abrar ali
 
Les20[1]Working with Composite Datatypes
Les20[1]Working with Composite DatatypesLes20[1]Working with Composite Datatypes
Les20[1]Working with Composite Datatypessiavosh kaviani
 
Les08[1] Producing Readable Output with SQL*Plus
Les08[1] Producing Readable Output with SQL*PlusLes08[1] Producing Readable Output with SQL*Plus
Les08[1] Producing Readable Output with SQL*Plussiavosh kaviani
 
Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Salman Memon
 
Les18[1]Interacting with the Oracle Server
Les18[1]Interacting with  the Oracle ServerLes18[1]Interacting with  the Oracle Server
Les18[1]Interacting with the Oracle Serversiavosh kaviani
 
122 sql for-beginners
122 sql for-beginners122 sql for-beginners
122 sql for-beginnerssuzzanj1990
 
Less07 schema
Less07 schemaLess07 schema
Less07 schemaImran Ali
 
Database management system chapter5
Database management system chapter5Database management system chapter5
Database management system chapter5Pranab Dasgupta
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSalman Memon
 
Les03 Single Row Functions in Oracle and SQL.ppt
Les03 Single Row Functions in Oracle and SQL.pptLes03 Single Row Functions in Oracle and SQL.ppt
Les03 Single Row Functions in Oracle and SQL.pptDrZeeshanBhatti
 
Les10[1]Creating and Managing Tables
Les10[1]Creating and Managing TablesLes10[1]Creating and Managing Tables
Les10[1]Creating and Managing Tablessiavosh kaviani
 
Manage schema object.ppt
Manage schema object.pptManage schema object.ppt
Manage schema object.pptAhmadUsman79
 

Similar to Les16[1]Declaring Variables (20)

Les17[1] Writing Executable Statements
Les17[1] Writing Executable StatementsLes17[1] Writing Executable Statements
Les17[1] Writing Executable Statements
 
Cursores.ppt
Cursores.pptCursores.ppt
Cursores.ppt
 
plsql Les09
 plsql Les09 plsql Les09
plsql Les09
 
Dbms oracle
Dbms oracle Dbms oracle
Dbms oracle
 
Les20[1]Working with Composite Datatypes
Les20[1]Working with Composite DatatypesLes20[1]Working with Composite Datatypes
Les20[1]Working with Composite Datatypes
 
Intro.pptx
Intro.pptxIntro.pptx
Intro.pptx
 
Les08[1] Producing Readable Output with SQL*Plus
Les08[1] Producing Readable Output with SQL*PlusLes08[1] Producing Readable Output with SQL*Plus
Les08[1] Producing Readable Output with SQL*Plus
 
Les09.ppt
Les09.pptLes09.ppt
Les09.ppt
 
Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base
 
Les18[1]Interacting with the Oracle Server
Les18[1]Interacting with  the Oracle ServerLes18[1]Interacting with  the Oracle Server
Les18[1]Interacting with the Oracle Server
 
122 sql for-beginners
122 sql for-beginners122 sql for-beginners
122 sql for-beginners
 
Less07 schema
Less07 schemaLess07 schema
Less07 schema
 
Database management system chapter5
Database management system chapter5Database management system chapter5
Database management system chapter5
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data base
 
Les03 Single Row Functions in Oracle and SQL.ppt
Les03 Single Row Functions in Oracle and SQL.pptLes03 Single Row Functions in Oracle and SQL.ppt
Les03 Single Row Functions in Oracle and SQL.ppt
 
DBMS Chapter-3.ppsx
DBMS Chapter-3.ppsxDBMS Chapter-3.ppsx
DBMS Chapter-3.ppsx
 
Les09
Les09Les09
Les09
 
Les10[1]Creating and Managing Tables
Les10[1]Creating and Managing TablesLes10[1]Creating and Managing Tables
Les10[1]Creating and Managing Tables
 
ZekeLabs PLSQL slides
ZekeLabs PLSQL slidesZekeLabs PLSQL slides
ZekeLabs PLSQL slides
 
Manage schema object.ppt
Manage schema object.pptManage schema object.ppt
Manage schema object.ppt
 

More from siavosh kaviani

Introduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdfIntroduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdfsiavosh kaviani
 
Attaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptxAttaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptxsiavosh kaviani
 
Short CV CTO version 2.pdf
Short CV CTO version 2.pdfShort CV CTO version 2.pdf
Short CV CTO version 2.pdfsiavosh kaviani
 
Short CV Marketing version 2.pdf
Short CV Marketing version 2.pdfShort CV Marketing version 2.pdf
Short CV Marketing version 2.pdfsiavosh kaviani
 
Short CV prof version 2.pdf
Short CV prof version 2.pdfShort CV prof version 2.pdf
Short CV prof version 2.pdfsiavosh kaviani
 
Siavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdfSiavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdfsiavosh kaviani
 
SiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdfSiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdfsiavosh kaviani
 
Les14[1]Controlling User Access
Les14[1]Controlling User AccessLes14[1]Controlling User Access
Les14[1]Controlling User Accesssiavosh kaviani
 
Les23[1]Handling Exceptions
Les23[1]Handling ExceptionsLes23[1]Handling Exceptions
Les23[1]Handling Exceptionssiavosh kaviani
 
Les22[1]Advanced Explicit Cursor Concepts
Les22[1]Advanced Explicit Cursor ConceptsLes22[1]Advanced Explicit Cursor Concepts
Les22[1]Advanced Explicit Cursor Conceptssiavosh kaviani
 
Les21[1]Writing Explicit Cursors
Les21[1]Writing Explicit CursorsLes21[1]Writing Explicit Cursors
Les21[1]Writing Explicit Cursorssiavosh kaviani
 
Les19[1]Writing Control Structures
Les19[1]Writing Control StructuresLes19[1]Writing Control Structures
Les19[1]Writing Control Structuressiavosh kaviani
 
Les13[1]Other Database Objects
Les13[1]Other Database ObjectsLes13[1]Other Database Objects
Les13[1]Other Database Objectssiavosh kaviani
 
Les11[1]Including Constraints
Les11[1]Including ConstraintsLes11[1]Including Constraints
Les11[1]Including Constraintssiavosh kaviani
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Datasiavosh kaviani
 

More from siavosh kaviani (20)

Introduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdfIntroduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdf
 
Attaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptxAttaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptx
 
Short CV BA.pdf
Short CV BA.pdfShort CV BA.pdf
Short CV BA.pdf
 
Faegh Omidi Resume.pdf
Faegh Omidi Resume.pdfFaegh Omidi Resume.pdf
Faegh Omidi Resume.pdf
 
Short CV CTO version 2.pdf
Short CV CTO version 2.pdfShort CV CTO version 2.pdf
Short CV CTO version 2.pdf
 
Short CV Marketing version 2.pdf
Short CV Marketing version 2.pdfShort CV Marketing version 2.pdf
Short CV Marketing version 2.pdf
 
Short CV prof version 2.pdf
Short CV prof version 2.pdfShort CV prof version 2.pdf
Short CV prof version 2.pdf
 
Siavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdfSiavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdf
 
SiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdfSiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdf
 
apex security demo.ppsx
apex security demo.ppsxapex security demo.ppsx
apex security demo.ppsx
 
Les14[1]Controlling User Access
Les14[1]Controlling User AccessLes14[1]Controlling User Access
Les14[1]Controlling User Access
 
Les23[1]Handling Exceptions
Les23[1]Handling ExceptionsLes23[1]Handling Exceptions
Les23[1]Handling Exceptions
 
Les22[1]Advanced Explicit Cursor Concepts
Les22[1]Advanced Explicit Cursor ConceptsLes22[1]Advanced Explicit Cursor Concepts
Les22[1]Advanced Explicit Cursor Concepts
 
Les21[1]Writing Explicit Cursors
Les21[1]Writing Explicit CursorsLes21[1]Writing Explicit Cursors
Les21[1]Writing Explicit Cursors
 
Les19[1]Writing Control Structures
Les19[1]Writing Control StructuresLes19[1]Writing Control Structures
Les19[1]Writing Control Structures
 
Les15[1]SQL Workshop
Les15[1]SQL WorkshopLes15[1]SQL Workshop
Les15[1]SQL Workshop
 
Les13[1]Other Database Objects
Les13[1]Other Database ObjectsLes13[1]Other Database Objects
Les13[1]Other Database Objects
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
 
Les11[1]Including Constraints
Les11[1]Including ConstraintsLes11[1]Including Constraints
Les11[1]Including Constraints
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
 

Recently uploaded

URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 

Recently uploaded (20)

URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 

Les16[1]Declaring Variables

  • 1. Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. 16 Declaring Variables
  • 2. 16-2 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Objectives After completing this lesson, you should be able to do the following: • List the benefits of PL/SQL • Recognize the basic PL/SQL block and its sections • Describe the significance of variables in PL/SQL • Declare PL/SQL variables • Execute a PL/SQL block
  • 3. 16-3 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. About PL/SQL • PL/SQL is an extension to SQL with design features of programming languages. • Data manipulation and query statements of SQL are included within procedural units of code.
  • 4. 16-4 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Benefits of PL/SQL Integration Application Oracle Server Shared library
  • 5. 16-5 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Benefits of PL/SQL Application Other DBMSs Application Oracle with PL/SQL SQL SQL SQL SQL SQL IF...THEN SQL ELSE SQL END IF; SQL Improve performance
  • 6. 16-6 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. PL/SQL Block Structure • DECLARE – Optional Variables, cursors, user-defined exceptions • BEGIN – Mandatory – SQL statements – PL/SQL statements • EXCEPTION – Optional Actions to perform when errors occur • END; – Mandatory DECLARE BEGIN EXCEPTION END;
  • 7. 16-7 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. PL/SQL Block Structure DECLARE v_variable VARCHAR2(5); BEGIN SELECT column_name INTO v_variable FROM table_name; EXCEPTION WHEN exception_name THEN ... END; DECLARE BEGIN EXCEPTION END;
  • 8. 16-8 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Block Types Anonymous Procedure Function [DECLARE] BEGIN --statements [EXCEPTION] END; PROCEDURE name IS BEGIN --statements [EXCEPTION] END; FUNCTION name RETURN datatype IS BEGIN --statements RETURN value; [EXCEPTION] END;
  • 9. 16-9 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Program Constructs Anonymous block Application trigger Stored procedure/ function Database trigger Application procedure/ function Packaged procedure/ function DECLARE BEGIN EXCEPTION END;
  • 10. 16-11 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Use of Variables Use variables for: • Temporary storage of data • Manipulation of stored values • Reusability • Ease of maintenance
  • 11. 16-12 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Handling Variables in PL/SQL • Declare and initialize variables in the declaration section. • Assign new values to variables in the executable section. • Pass values into PL/SQL blocks through parameters. • View results through output variables.
  • 12. 16-13 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Types of Variables • PL/SQL variables: – Scalar – Composite – Reference – LOB (large objects) • Non-PL/SQL variables: Bind and host variables
  • 13. 16-15 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. TRUE Types of Variables 25-OCT-99 Atlanta “Four score and seven years ago our fathers brought forth upon this continent, a new nation, conceived in LIBERTY, and dedicated to the proposition that all men are created equal.” 256120.08
  • 14. 16-16 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Declaring PL/SQL Variables Syntax Examples identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]; Declare v_hiredate DATE; v_deptno NUMBER(2) NOT NULL := 10; v_location VARCHAR2(13) := 'Atlanta'; c_comm CONSTANT NUMBER := 1400;
  • 15. 16-17 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Declaring PL/SQL Variables Guidelines • Follow naming conventions. • Initialize variables designated as NOT NULL and CONSTANT. • Initialize identifiers by using the assignment operator (:=) or the DEFAULT reserved word. • Declare at most one identifier per line.
  • 16. 16-18 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Naming Rules • Two variables can have the same name, provided they are in different blocks. • The variable name (identifier) should not be the same as the name of table columns used in the block. DECLARE empno NUMBER(4); BEGIN SELECT empno INTO empno FROM emp WHERE ename = 'SMITH'; END;
  • 17. 16-19 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Assigning Values to Variables v_ename := 'Maduro'; v_hiredate := '31-DEC-98'; Syntax Examples Set a predefined hiredate for new employees. Set the employee name to Maduro. identifier := expr;
  • 18. 16-20 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Variable Initialization and Keywords Using: • Assignment operator (:=) • DEFAULT keyword • NOT NULL constraint
  • 19. 16-21 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Scalar Datatypes • Hold a single value • Have no internal components 25-OCT-99 Atlanta “Four score and seven years ago our fathers brought forth upon this continent, a new nation, conceived in LIBERTY, and dedicated to the proposition that all men are created equal.” TRUE 256120.08
  • 20. 16-22 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Base Scalar Datatypes • VARCHAR2 (maximum_length) • NUMBER [(precision, scale)] • DATE • CHAR [(maximum_length)] • LONG • LONG RAW • BOOLEAN • BINARY_INTEGER • PLS_INTEGER
  • 21. 16-24 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Scalar Variable Declarations v_job VARCHAR2(9); v_count BINARY_INTEGER := 0; v_total_sal NUMBER(9,2) := 0; v_orderdate DATE := SYSDATE + 7; c_tax_rate CONSTANT NUMBER(3,2) := 8.25; v_valid BOOLEAN NOT NULL := TRUE; Examples
  • 22. 16-25 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. The %TYPE Attribute • Declare a variable according to: – A database column definition – Another previously declared variable • Prefix %TYPE with: – The database table and column – The previously declared variable name
  • 23. 16-26 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Declaring Variables with the %TYPE Attribute Examples ... v_ename emp.ename%TYPE; v_balance NUMBER(7,2); v_min_balance v_balance%TYPE := 10; ...
  • 24. 16-27 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Declaring Boolean Variables • Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable. • The variables are connected by the logical operators AND, OR, and NOT. • The variables always yield TRUE, FALSE, or NULL. • Arithmetic, character, and date expressions can be used to return a Boolean value.
  • 25. 16-28 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. 1 5000 2 2345 3 12 4 3456 1 SMITH 2 JONES 3 NANCY 4 TIM PL/SQL table structure PL/SQL table structure BINARY_INTEGER VARCHAR2 BINARY_INTEGER NUMBER PL/SQL Record Structure TRUE 23-DEC-98 ATLANTA
  • 26. 16-29 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. LOB Datatype Variables Book (CLOB) Photo (BLOB) Movie (BFILE) NCLOB
  • 27. 16-30 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Bind Variables Server O/S Bind variable
  • 28. 16-32 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Referencing Non-PL/SQL Variables Store the annual salary into a SQL*Plus host variable. • Reference non-PL/SQL variables as host variables. • Prefix the references with a colon (:). :g_monthly_sal := v_sal / 12;
  • 29. 16-33 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. DBMS_OUTPUT.PUT_LINE • An Oracle-supplied packaged procedure • An alternative for displaying data from a PL/SQL block • Must be enabled in SQL*Plus with SET SERVEROUTPUT ON
  • 30. 16-34 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Summary • PL/SQL blocks are composed of the following sections: – Declarative (optional) – Executable (required) – Exception handling (optional) • A PL/SQL block can be an anonymous block, procedure, or function. DECLARE BEGIN EXCEPTION END;
  • 31. 16-35 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Summary • PL/SQL identifiers: – Are defined in the declarative section – Can be of scalar, composite, reference, or LOB datatype – Can be based on the structure of another variable or database object – Can be initialized
  • 32. 16-36 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Practice Overview • Determining validity of declarations • Developing a simple PL/SQL block
  • 33. 16-40 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.