introduction to pl/sql. class agenda introduction introduction to pl/sql declaring pl/sql variable...
TRANSCRIPT
![Page 1: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/1.jpg)
INTRODUCTION TO PL/SQL
![Page 2: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/2.jpg)
Class Agenda• Introduction• Introduction to PL/SQL•Declaring PL/SQL Variable•Creating the Executable Section• Interacting with the Oracle Database Server•Writing Control Structures•Working with Composite Data Types•Using Explisit Cursors• Including Exception Handling•Creating Stored Proedures and Functions
![Page 3: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/3.jpg)
Objective
After completing this lesson, you should be able to do the following :•Explain the need for PL/SQL•Explain the benefits of PL/SQL•Identify the different type of PL/SQL blocks•Use iSQL*Plus as a development environment for PL/SQL•Output messages in PL/SQL
![Page 4: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/4.jpg)
PL/SQL Block StructureDECLARE (Optional)
Variables, cursors, user-defined exceptions
BEGIN (Mandatory)- SQL statements- PL/SQL statements
EXCEPTION (Optional)Actions to perform when errors occur
END; (Mandatory)
![Page 5: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/5.jpg)
Block Types
[DECLARE]
BEGIN--statements
[EXCEPTION]
END;
Anonymous
PROCEDURE name
IS
BEGIN
--statements
[EXCEPTION]
END;
Procedure
FUNCTION name
RETURN datatype
IS
BEGIN
--statements
[EXCEPTION]
END;
Function
![Page 6: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/6.jpg)
Program ConstructTools Construct:
• Anonymous blocks• Application procedures or
functions• Application Packages• Application triggers• Object Types
Database Server Construct:
• Anonymous blocks• Stored procedures or
functions• Stored packages• Database triggers• Object types
![Page 7: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/7.jpg)
Use of Variables•Temporary storage of data•Manipulation of stored values•Reusability•Ease of maintenance
![Page 8: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/8.jpg)
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
![Page 9: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/9.jpg)
Types of Variables•PL/SQL variables
▫Scalar▫Composite▫Reference▫LOB (large objects)
•Non-PL/SQL variables▫Bind variables▫Host variables
![Page 10: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/10.jpg)
Using iSQL*Plus Variables•PL/SQL does not have input or output
capability of its own
•You can reference substitution variables within a PL/SQL block with a preceding ampersand
• iSQL*Plus host (or “bind”) variables can be used to pass run time values out of the PL/SQL block back to the iSQL*Plus environment
![Page 11: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/11.jpg)
Declaring PL/SQL VariablesSyntax:
Identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr];
![Page 12: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/12.jpg)
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
• The names of the variables must not be longer than 30 characters. The first character must be a letter; the remaining characters can be letters, numbers, or special symbols
![Page 13: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/13.jpg)
Scalar Data Types•Hold a single value•Have no internal components•Classified into 4 categories:
•NUMBER•CHARACTER•DATE•BOOLEAN
![Page 14: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/14.jpg)
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
Syntax: Identifier Table.column_name%TYPE;
![Page 15: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/15.jpg)
Declaring Boolean Variables•Only the values TRUE, FALSE, and NULL
can be assigned to a Boolean variable•The variables are compared by the logical
operators AND, OR, and NOT.•The variables always yield TRUE, FALSE,
or NULL.•Arithmatic, character, and date
expressions can be used to return a Boolean value
![Page 16: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/16.jpg)
Composite Data Types•Have internal components that can be
manipulated individually•Composite data types are of TABLE,
RECORD, NESTED TABLE, and VARRAY types
![Page 17: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/17.jpg)
LOB Data Type Variables•CLOB Book•BLOB Photo•BFILE Movie•NCLOB NCHAR
![Page 18: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/18.jpg)
Bind Variables• A variable that declare in a host environment• Can be used to pass run-time values• To reference in PL/SQL, you must prefix its name
with a colon (:)
Example:
VARIABLE g_salary NUMBERBEGINSELECT salary INTO :g_salary FROM empWHERE emp_id = 178;
END;/PRINT g_salary
![Page 19: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/19.jpg)
DBMS_OUTPUT.PUT_LINE•An Oracle-supplied packaged procedure•An alternative for displaying data from a
PL/SQL block•Must be enabled in iSQL*Plus with •SET SERVEROUTPUT ON
![Page 20: INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the](https://reader036.vdocument.in/reader036/viewer/2022081811/56649ed25503460f94be1742/html5/thumbnails/20.jpg)
Example
SET SERVEROUTPUT ONDEFINE p_annual_sal = 60000
DECLAREv_sal NUMBER(9,2) := &p_annual_sal;
BEGINv_sal := v_sal/12;DBMS_OUTPUT.PUT_LINE (‘The monthly salary is ‘||TO_CHAR(v_sal));
END;/