0% found this document useful (0 votes)
58 views

PL /SQL

The document provides an overview of PL/SQL including its introduction, advantages, block structure, data types, variables, constants, server output, and basic and advanced concepts like condition and looping control, transactions, exceptions, functions, procedures, packages and triggers.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

PL /SQL

The document provides an overview of PL/SQL including its introduction, advantages, block structure, data types, variables, constants, server output, and basic and advanced concepts like condition and looping control, transactions, exceptions, functions, procedures, packages and triggers.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Introduction PL/SQL

PL/SQL Advantages PL/SQL Block Structure PL/SQL Data Types PL/SQL Variables PL/SQL Constants PL/SQL Server Output PL/SQL Comments

Basic PL/SQL

PL/SQL Condition Control PL/SQL Looping Control PL/SQL Sequnce Control PL/SQL Case Statements PL/SQL Cursors Implicit Cursors Explicit Cursors For Loop Cursors Parameterized Cursors

PL/SQL Transaction PL/SQL Exception Built-in Exception User-Defined Exception User-Named Exceptions

Advance PL/SQL

PL/SQL Functions Functions Syntax Functions Drop Functions Examples

PL/SQL Procedures Procedures Syntax Procedures Drop Procedures Examples

PL/SQL Packages Packages Advantages Packages Syntax Packages Examples Packages Alter

PL/SQL Triggers Triggers How to Work Triggers Type Triggers Syntax Triggers Examples

Introduction PL/SQL
What is PL/SQL?
PL/SQL is a product of the Oracle. PL/SQL stands by "Programming Language" extension of SQL. PL/SQL is Specially Designed for Database Oriented Activities. PL/SQL is Very Usefully Language and Tools of Oracle to Manipulate, Control, Validate, and Restricted the Unauthorized Access of Data From the Database.PL/SQL can improve the Performance of an Application and It is dealing with Error and return User Friendly Error Message.We can easily show Multiple Records of the Multiple Table on s the Same Time.PL/SQL Sends an entire Block of Statements execute to the oracle engine at One Time.

Advantages PL/SQL

Procedural Language Supported: PL/SQL is a development tools that not only supported data Manipulation but also Provide the Condition, Checking, Looping or Branching Operation. Reduces Network Traffic: PL/SQL is same entire block of SQL statement execute to the oracle engine at all at once so it's benefit to reduce the Network Traffic. Error Handling: PL/SQL also permit during with Error Handling as required facility to Display User Friendly Error Message where error are encounter. Declare Variable: PL/SQL allow to declaration and use of variable in a block of code which variable will use to store intermediate result of query for later processing. Intermediate Calculation: PL/SQL calculations done quickly and efficient without the use of oracle engines and improve the transaction. Portable Application: Application are written in PL/SQL is portable in any computer or hardware for any system means Application independence to run any computer.

PL/SQL Block Structure


PL/SQL Block Structure divide by four parts but two parts BEGIN and END part are compulsory, other two part DECLARE and EXCEPTION is optional part.

Figure - Structure of PL/SQL block

DECLARE: Variable and constants are declared within this section and we may initialize them with value. BEGIN: It contains the PL/SQL statements which implement the actual programming logic. This section contains conditional statements (IF..ELSE), looping statements (FOR, WHILE) and Branching Statements (GOTO) etc. EXCEPTION: Exception block handling the error and show the user friendly message. Error can arise due to syntax, logical or validation rules. Note: 1. BEGIN and END; are compulsory sections of any PL/SQL program. 2. DECLARE and EXCEPTION are optional sections.

PL/SQL Data Types


PL/SQL support following SQL basic data type like CHAR, VARCHAR2, LONG, NUMBER, NUMBER(p,s), and DATE.
PL/SQL support Basic Data Types:

Data Type

Description

Storage(Maximum)

CHAR(size)

This data type is use to store character strings value of fixed length. 255 Character It can contain letters, numbers, and special characters. This data type is use to store variable length alphanumeric data. 4000 Character It is more flexible form if the CHAR data type. It can contain letters, numbers, and special characters. This data type is use to represent data and time. Standard format is DD-MON-YY. -

VARCHAR(size)/ VARCHAR2(size)

Date()

Number(P,S)

This data type is use to store number (fixed or floating point). P means Precision, Maximum length of digit. 38 Digit Precision S means Scale, number of decimal places by default 0. This data type is use to store variable length character string contain up to 2GB. This data type is use to store Binary data such as Digitized, Picture or Image. This data type is use to store Binary data such as Digitized, Picture or Image. 2GB

LONG

RAW

255 Bytes

LONG RAW

2GB

PL/SQL can support the special Data Types like CLOB, BLOB, BFile, %Type, %RowType and RowID all are describe here.

PL/SQL support Advance Data Types:


Data Type Description This data type is use to store Binary character like songs, video, image, constructured data type and hold the upto 4 GB. Storage(Maximum)

BLOB

4 GB

CLOB

This data type is use to store Character variable and 4 GB hold the up to 4 GB. This data type is use to store value in Pointer 4 GB variable. type of pointer, reference of external file. This data type is use to store value unknown data type column in a table. column is identified by %type data type. Eg. emp.eno%type emp name is table, eno is a unknown data type column and %Type is data type to hold the value. This data type is use to store value unknown data type all column in a table. All column is identified by %RowType datatype. Eg. emp%rowtype emp name is table, all column type is %rowtype. RowID is Data Type. RowID is Two Type Extended or Restricted. Extended return 0 and Restricted return 1 otherwise return the row number. Function of Row ID:

BFile

%Type

%RowType

%RowID

Function RowID ROWID_Verify ROWID_Type ROWID_Block_Number

Description Verify if the rowid can be extended. 0 = rowid, 1 = extended. Block number that contain the record return 1 extended. Object Number of the object that contain record. Relative File Number that contain record. Row Number of the Record.

ROWID_Object

ROWID_Relative_FNumber ROWID_Row_Number

ROWID_To_Absolute_FNumber Return the Absolute file number. ROWID_To_Extended ROWID_To_Restricted Convert the Extended format. Convert the Restricted format.

PL/SQL Variables Declaration and Variables Scope


PL/SQL Variable Declaration is use to store value into declared value.

Declare Variable Syntax


The General Syntax to declare a variable is: Variable_Name DataType[Size] [NOT NULL] := [ Value ];

Variable_Name is the predefined name of the variable. DataType is a valid PL/SQL datatype. Size is an optional specification of datatype size to hold maximum size value. NOT NULL is an optional specification on the variable. Value is also an Optional Specification, where you can Initialize or later initialize a variable. Each variable declaration is terminated by a semicolon.

PL/SQL Placeholders
Placeholders means value store temporary in storage area. Placeholders can be any of Variables, Constants. Oracle defines placeholders to store data temporarily, which are used to manipulate data during the execution of a PL SQL block. Number(p,s), Number(n), Char, Varchar2, Date, Long, Raw, Blob, Clob, Bfile datatypes used to define placeholders.

Variable Declaration Example


1. Store the employee No is NOT NULL(compulsory) and employee Name using the Variable. Exmple Code: DECLARE eno number(5) NOT NULL := 2 // NOT NULL means value initialize as same time declaration. ename varchar2(15) := Jay Patel; // value is initialize as now and later initialize BEGIN dbms_output.put_line('Declared Value:'); dbms_output.put_line(' Eno: ' || eno || ' EName: ' || ename); END; / Backward slash '/' is indicates to execute the above PL/SQL Block Program. Example Result: Declared Value: Eno: 2 Ename: Jay Patel

Variables Scope

Local variables - Declared in a Inner block and cannot be referenced by outside Blocks. Global variables - Declared in a outer block and can be referenced by itself and by its inner blocks.

Variable Scope Example


First we declare the two variable ('num1' and 'num2') in Global variable and third variable declare (num_addition) in local variable. variable 'num_addition' is declared in the inner block, so cannot be accessed in the outer block i.e. it cannot be accessed after inner end block. and 'num1' and 'num2' can be accessed anywhere in the block. Example Code: DECLARE num1 number := 10; num2 number := 20; BEGIN DECLARE num_addition number; BEGIN num_addition := num1 + num2; dbms_output.put_line('Addition is: ' || num_addition); END; END; / Example Result: Addition is: 30

PL/SQL Constants Declaration


PL/SQL Constants Declaration is use to constant value can not be change in PL/SQL Block.

PL/SQL Constants Declaration Syntax


The General Syntax to declare a variable is: Constant_Name CONSTANT DataType[Size] := Value;

Constant_Name is the predefined name of the constant (similar to a variable name). CONSTANT is a reserved word and the value does not change. DataType is a valid PL/SQL datatype. Size is an optional specification of datatype size to hold maximum size value. Value must be assigned to a constant when it is declared. not assign the later.

Each constant declaration is terminated by a semicolon.

PL/SQL Constant Declaration Example


1. Store the employee No is NOT NULL(compulsory), employee Name and constant the employee Department fixed on Web Developer using the Variable. Example Code: DECLARE eno number(5) NOT NULL := 2 ename varchar2(15) := Jay Patel; edept CONSTANT varchar2(15) := Web Developer; BEGIN dbms_output.put_line('Declared Value:'); dbms_output.put_line(' Eno: ' || eno || ' EName: ' || ename); dbms_output.put_line('Constant Declared:'); dbms_output.put_line(' EDept: ' || edept); END; / Backward slash '/' is indicates to execute the above PL/SQL Block Program. Example Result: Declared Value: Eno: 2 Ename: Jay Patel EDept: Web Developer Constant benefit is to not all time assign the same value in the same variable its fixed and not change during the program block.

PL/SQL Server Output


PL/SQL is a work on the server result so we always required to server output result is show in the screen otherwise result can not be display. Whenever start the Oracle at that time required to write the input command "set serveroutput on".

How to on the PL/SQL Server Result SQL> set serveroutput on Set serveroutput on Example Store the employee No is NOT NULL(compulsory), employee Name and constant the employee Department fixed on Web Developer using the Variable. Exmple Code: SQL> set serveroutput on SQL> DECLARE eno number(5) NOT NULL := 2 ename varchar2(15) := Jay Patel; edept CONSTANT varchar2(15) := Web Developer; BEGIN dbms_output.put_line('Declared Value:'); dbms_output.put_line(' Eno: ' || eno || ' EName: ' || ename); dbms_output.put_line('Constant Declared:'); dbms_output.put_line(' EDept: ' || edept); END; / Result show only on when execute the first "set serveroutput on" command. Example Result: Declared Value: Eno: 2 Ename: Jay Patel EDept: Web Developer

PL/SQL Comments
Comments in PL/SQL can take one of two forms:

Multi-line comments and Single line comments.

Multi-line comments are delimited with /*....*/ and single line comments starts with two dashes --.

Comments can begin in any column on any line. If you are embedding comments in SQL that will be embedded in PL/SQL you need to be careful but just about any other use is easy understand. PL/SQL Multi line and Single line Comments SQL>-- Single Line Comment SQL>/* Multiline Comment line 1 Multiline Comment line2

*/

Comment Example
Store the employee No is NOT NULL(compulsory), employee Name and constant the employee Department fixed on Web Developer using the Variable. Exmple Code: SQL> set serveroutput on SQL> DECLARE --assign value into eno variable eno number(5) NOT NULL := 2; ename varchar2(15) := Jay Patel; /* Constant value is fixed for edept value is "Web Deloper" is fixed all program not required declare all times. */ edept CONSTANT varchar2(15) := Web Developer; BEGIN dbms_output.put_line('Declared Value:'); dbms_output.put_line(' Eno: ' || eno || ' EName: ' || ename); dbms_output.put_line('Constant Declared:'); dbms_output.put_line(' EDept: ' || edept); END; / Example Result: Declared Value: Eno: 2 Ename: Jay Patel EDept: Web Developer

PL/SQL If Statements
PL/SQL IF statement is used to transfer the control of program depending on a specified condition. If we want to know greater of given 2 numbers then we can use the IF statement for comparing the 2 values. Simple IF Statement Syntax Simplest IF statement takes the following format: IF <condition> THEN <statements>; END IF; IF..ELSE Statement Syntax IF statement have a ELSE part: IF <condition> THEN <statements>; ELSE <statements>; END IF; Nested IF Statement Syntax Nested IF statement have a IF..ELSE part: IF <condition-1> THEN <statements>; ELSE IF <condition-2> THEN <statements>; ELSE <statements>; END IF; END IF;

Alternative Nested IF Syntax Alternative Nested IF have a IF..ELSE part: IF <condition-1> THEN <statements>; ELSIF <condition-2> THEN <statements>; ELSIF <condition-3> THEN <statements>; .................... .................... .................... ELSE <statements>; END IF;

PL/SQL Loops/Iterative
When we need to execute the block of statements we required to repeatedly for a certain number of times then we haveto use loops. PL/SQL support three looping control: SIMPLE Loop, WHILE Loop, FOR Loop. Simple Loop Syntax: LOOP <Sequence of statements> EXIT; {or EXIT WHEN condition;} END LOOP; Example Code: SQL>DECLARE i number; BEGIN LOOP dbms_output.put_line('Hello'); i:=i+1; EXIT WHEN i>5; END LOOP; END;

/ Example Result:
Hello Hello Hello Hello

While Loop Syntax: WHILE <condition> LOOP <action> END LOOP; Example Code: SQL>DECLARE i number; BEGIN WHILE i<5 LOOP dbms_output.put_line('Hello'); i:=i+1; END LOOP; END; / Example Result: Hello Hello Hello Hello

FOR Loop Syntax: FOR variable IN [REVERSE] start..end LOOP <action> END LOOP; Example Code: SQL>DECLARE BEGIN FOR i IN 1..5 LOOP dbms_output.put_line('Hello'); END LOOP; END; / Example Result: Hello Hello Hello Hello

PL/SQL GOTO Statement

GOTO statement transfers the control of program unconditionally. GOTO statement is transfers the control of program to any position in the PL/SQL block. GOTO Syntax

GOTO <<Code_Block_Name>> --------------------<<Code_Block_Name>> ---------------------

Example Code: SQL>DECLARE BEGIN FOR i IN 1..5 LOOP dbms_output.put_line(i); IF i=4 THEN GOTO label1; END IF; END LOOP; <<label1>> dbms_output.put_line(Row Filled); END; / Example Result: 1 2 3 4

PL/SQL Switch Case Statements PL/SQL SWITCH case statement is important control to use the selected case statement will be execute.

SWITCH Statement Syntax

CASE WHEN <condition> THEN <statement> WHEN <condition> THEN <statement> ELSE <statement> END CASE

Example Code: SQL>DECLARE a number := 5; BEGIN CASE WHEN a = 5 THEN dbms_output.put_line('Execute Me'); WHEN a = 4 THEN dbms_output.put_line('Not Execute Me'); ELSE dbms_output.put_line('Else Statements'); END CASE; END; / Example Result: Execute Me

You might also like