0% found this document useful (0 votes)
12 views12 pages

PLSQL Tutorial Removed

Uploaded by

Aniket Thool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views12 pages

PLSQL Tutorial Removed

Uploaded by

Aniket Thool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

PL/SQL

Table of Contents
About the Tutorial .................................................................................................................................... i

Audience .................................................................................................................................................. i

Prerequisites ............................................................................................................................................ i

Copyright & Disclaimer............................................................................................................................. i

Table of Contents ................................................................................................................................... iii

PL/SQL — OVERVIEW .......................................................................................................... 1

Features of PL/SQL .................................................................................................................................. 1

Advantages of PL/SQL ............................................................................................................................. 1

PL/SQL — ENVIRONMENT SETUP ........................................................................................ 3

Text Editor............................................................................................................................................. 14

PL/SQL — BASIC SYNTAX ................................................................................................... 15

PL/SQL — DATA TYPES....................................................................................................... 19

PL/SQL Scalar Data Types and Subtypes ................................................................................................ 19

PL/SQL Numeric Data Types and Subtypes ............................................................................................ 20

PL/SQL Character Data Types and Subtypes .......................................................................................... 21

PL/SQL Boolean Data Types .................................................................................................................. 22

PL/SQL Datetime and Interval Types ..................................................................................................... 22

PL/SQL Large Object (LOB) Data Types .................................................................................................. 23

PL/SQL User-Defined Subtypes.............................................................................................................. 24

NULLs in PL/SQL .................................................................................................................................... 25

PL/SQL — VARIABLES ........................................................................................................ 26

Variable Declaration in PL/SQL .............................................................................................................. 26

Initializing Variables in PL/SQL .............................................................................................................. 27

Variable Scope in PL/SQL ...................................................................................................................... 28


ii
PL/SQL

Assigning SQL Query Results to PL/SQL Variables ................................................................................. 29

PL/SQL — CONSTANTS AND LITERALS ............................................................................... 31

Declaring a Constant ............................................................................................................................. 31

The PL/SQL Literals................................................................................................................................ 32

PL/SQL — OPERATORS ...................................................................................................... 34

Arithmetic Operators ............................................................................................................................ 34

Relational Operators ............................................................................................................................. 36

[Comparison Operators ......................................................................................................................... 39

Logical Operators .................................................................................................................................. 44

PL/SQL Operator Precedence ................................................................................................................ 46

PL/SQL — CONDITIONS ..................................................................................................... 49

IF-THEN Statement ................................................................................................................................ 50

IF-THEN-ELSE Statement........................................................................................................................ 53

IF-THEN-ELSIF Statement ....................................................................................................................... 55

CASE Statement .................................................................................................................................... 56

Searched CASE Statement ..................................................................................................................... 58

Nested IF-THEN-ELSE Statements .......................................................................................................... 60

PL/SQL — LOOPS ............................................................................................................... 62

Basic Loop Statement ............................................................................................................................ 63

WHILE LOOP Statement ........................................................................................................................ 65

FOR LOOP Statement ............................................................................................................................ 66

Reverse FOR LOOP Statement ............................................................................................................... 68

Nested Loops ........................................................................................................................................ 69

Labeling a PL/SQL Loop ......................................................................................................................... 71

The Loop Control Statements ................................................................................................................ 72

iii
PL/SQL

EXIT Statement ..................................................................................................................................... 73

The EXIT WHEN Statement .................................................................................................................... 75

CONTINUE Statement............................................................................................................................ 77

GOTO Statement ................................................................................................................................... 80

PL/SQL — STRINGS ............................................................................................................ 83

Declaring String Variables ..................................................................................................................... 83

PL/SQL String Functions and Operators ................................................................................................. 84

PL/SQL — ARRAYS ............................................................................................................. 89

Creating a Varray Type .......................................................................................................................... 89

PL/SQL — PROCEDURES .................................................................................................... 94

Parts of a PL/SQL Subprogram............................................................................................................... 94

Creating a Procedure ............................................................................................................................. 95

Executing a Standalone Procedure ........................................................................................................ 96

Deleting a Standalone Procedure .......................................................................................................... 97

Parameter Modes in PL/SQL Subprograms ............................................................................................ 98

Methods for Passing Parameters......................................................................................................... 100

PL/SQL — FUNCTIONS ..................................................................................................... 103

Creating a Function ............................................................................................................................. 103

Calling a Function ................................................................................................................................ 104

PL/SQL Recursive Functions ................................................................................................................ 106

PL/SQL — CURSORS......................................................................................................... 108

Implicit Cursors ................................................................................................................................... 108

Explicit Cursors .................................................................................................................................... 110

Declaring the Cursor ............................................................................................................................ 112

Opening the Cursor ............................................................................................................................. 112

iv
PL/SQL

Fetching the Cursor ............................................................................................................................. 112

Closing the Cursor ............................................................................................................................... 112

PL/SQL — RECORDS......................................................................................................... 114

Table-Based Records ........................................................................................................................... 114

Cursor-Based Records ......................................................................................................................... 115

User-Defined Records.......................................................................................................................... 116

PL/SQL — EXCEPTIONS .................................................................................................... 120

Syntax for Exception Handling ............................................................................................................. 120

Raising Exceptions ............................................................................................................................... 121

User-defined Exceptions ..................................................................................................................... 122

Pre-defined Exceptions ....................................................................................................................... 123

PL/SQL — TRIGGERS ........................................................................................................ 126

Creating Triggers ................................................................................................................................. 126

Triggering a Trigger ............................................................................................................................. 129

PL/SQL — PACKAGES ....................................................................................................... 130

Package Specification .......................................................................................................................... 130

Package Body ...................................................................................................................................... 130

Using the Package Elements ................................................................................................................ 131

PL/SQL — COLLECTIONS .................................................................................................. 138

Index-By Table .................................................................................................................................... 139

Nested Tables...................................................................................................................................... 141

Collection Methods ............................................................................................................................. 144

Collection Exceptions .......................................................................................................................... 145

v
PL/SQL

PL/SQL — TRANSACTIONS ............................................................................................... 147

Starting and Ending a Transaction ....................................................................................................... 147

Committing a Transaction ................................................................................................................... 147

Rolling Back Transactions .................................................................................................................... 148

Automatic Transaction Control ........................................................................................................... 149

PL/SQL — DATE & TIME................................................................................................... 150

Field Values for Datetime and Interval Data Types .............................................................................. 150

The Datetime Data Types and Functions ............................................................................................. 151

The Interval Data Types and Functions ................................................................................................ 155

PL/SQL — DBMS OUTPUT ................................................................................................ 157

DBMS_OUTPUT Subprograms ............................................................................................................. 157

PL/SQL — OBJECT-ORIENTED .......................................................................................... 160

Instantiating an Object ........................................................................................................................ 161

Member Methods ............................................................................................................................... 161

Using Map method .............................................................................................................................. 162

Using Order method ............................................................................................................................ 164

Inheritance for PL/SQL Objects ........................................................................................................... 166

Abstract Objects in PL/SQL .................................................................................................................. 168

vi
PL/SQL

Text Editor
Running large programs from the command prompt may land you in inadvertently losing some
of the work. It is always recommended to use the command files. To use the command files:

 Type your code in a text editor, like Notepad, Notepad+, or EditPlus, etc.

 Save the file with the .sql extension in the home directory.

 Launch the SQL*Plus command prompt from the directory where you created your
PL/SQL file.

 Type @file_name at the SQL*Plus command prompt to execute your program.

If you are not using a file to execute the PL/SQL scripts, then simply copy your PL/SQL code
and right-click on the black window that displays the SQL prompt; use the paste option to
paste the complete code at the command prompt. Finally, just press Enter to execute the
code, if it is not already executed.

20
PL/SQL — Basic Syntax PL/SQL

In this chapter, we will discuss the Basic Syntax of PL/SQL which is a block-structured
language; this means that the PL/SQL programs are divided and written in logical blocks of
code. Each block consists of three sub-parts:

Sr.
Sections & Description
No.

Declarations

This section starts with the keyword DECLARE. It is an optional section and
1 defines all variables, cursors, subprograms, and other elements to be used in
the program.

Executable Commands

This section is enclosed between the keywords BEGIN and END and it is a
mandatory section. It consists of the executable PL/SQL statements of the
2
program. It should have at least one executable line of code, which may be just
a NULL command to indicate that nothing should be executed.

Exception Handling

This section starts with the keyword EXCEPTION. This optional section
3
contains exception(s) that handle errors in the program.

Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within other
PL/SQL blocks using BEGIN and END. Following is the basic structure of a PL/SQL block:

DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;

21
PL/SQL

The 'Hello World' Example


DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/

The end; line signals the end of the PL/SQL block. To run the code from the SQL command
line, you may need to type / at the beginning of the first blank line after the last line of the
code. When the above code is executed at the SQL prompt, it produces the following result:

Hello World

PL/SQL procedure successfully completed.

The PL/SQL Identifiers


PL/SQL identifiers are constants, variables, exceptions, procedures, cursors, and reserved
words. The identifiers consist of a letter optionally followed by more letters, numerals, dollar
signs, underscores, and number signs and should not exceed 30 characters.

By default, identifiers are not case-sensitive. So you can use integer or INTEGER to
represent a numeric value. You cannot use a reserved keyword as an identifier.

The PL/SQL Delimiters


A delimiter is a symbol with a special meaning. Following is the list of delimiters in PL/SQL:

Delimiter Description

+, -, *, / Addition, subtraction/negation, multiplication, division

% Attribute indicator

' Character string delimiter

22
PL/SQL

. Component selector

(,) Expression or list delimiter

: Host variable indicator

, Item separator

" Quoted identifier delimiter

= Relational operator

@ Remote access indicator

; Statement terminator

:= Assignment operator

=> Association operator

|| Concatenation operator

** Exponentiation operator

<<, >> Label delimiter (begin and end)

/*, */ Multi-line comment delimiter (begin and end)

-- Single-line comment indicator

.. Range operator

<, >, <=, >= Relational operators

23
PL/SQL

<>, '=, ~=, ^= Different versions of NOT EQUAL

The PL/SQL Comments


Program comments are explanatory statements that can be included in the PL/SQL code that
you write and helps anyone reading its source code. All programming languages allow some
form of comments.

The PL/SQL supports single-line and multi-line comments. All characters available inside any
comment are ignored by the PL/SQL compiler. The PL/SQL single-line comments start with
the delimiter -- (double hyphen) and multi-line comments are enclosed by /* and */.

DECLARE
-- variable declaration
message varchar2(20):= 'Hello, World!';
BEGIN
/*
* PL/SQL executable statement(s)
*/
dbms_output.put_line(message);
END;
/

When the above code is executed at the SQL prompt, it produces the following result:

Hello World

PL/SQL procedure successfully completed.


[

PL/SQL Program Units


A PL/SQL unit is any one of the following:

 PL/SQL block

 Function

 Package

 Package body
24
PL/SQL

 Procedure

 Trigger

 Type

 Type body

Each of these units will be discussed in the following chapters.

25
PL/SQL

End of ebook preview


If you liked what you saw…
Buy it from our store @ https://store.tutorialspoint.com

26

You might also like