Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PL SQL: Prepared By: Zeba Qureshi Assistant Professor, CSE Dept, AITR

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

PL SQL

DBMS SESSION 8
Prepared By: Zeba Qureshi
Assistant Professor, CSE Dept, AITR
PL/SQL 
PL/SQL stands for Procedural Language/SQL.

PL/SQL extends SQL by adding constructs found


in procedural languages, resulting in a structural
language that is more powerful than SQL.

The basic unit in PL/SQL is a block. All PL/SQL


programs are made up of blocks, which can be
nested within each other. Typically, each block
performs a logical action in the program
PL/SQL 

• PL/SQL is a combination of SQL along with the


procedural features of programming languages. It
was developed by Oracle Corporation in the early
90's to enhance the capabilities of SQL. PL/SQL is
one of three key programming languages
embedded in the Oracle Database, along with SQL
itself and Java.
Features of PL/SQL
• PL/SQL has the following features −
• PL/SQL is tightly integrated with SQL.
• It offers extensive error checking.
• It offers numerous data types.
• It offers a variety of programming structures.
• It supports structured programming through
functions and procedures.
• It supports object-oriented programming.
• It supports the development of web applications
and server pages.
Difference between SQL and PL/SQL
PL/SQL - Environment Setup
• 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
PL/SQL - Basic Syntax
• PL/SQL programs are divided and written in logical blocks of code.
Each block consists of three sub-parts −
• 1. Declarations
This section starts with the keyword DECLARE. It is an optional
section and defines all variables, cursors, subprograms, and other
elements to be used in the program.
• 2. 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 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.
• 3. Exception Handling
This section starts with the keyword EXCEPTION. This optional
section contains exception(s) that handle errors in the program.
PL/SQL - Basic Syntax
The 'Hello World' Example
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
We need to execute "set
serveroutput on" if we need to see
the output of the code.

NOTE: We need to execute "set serveroutput on" if we need to


see the output of the code.
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 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;
/
PL/SQL - Variables
The syntax for declaring a variable is −
<variable_name> <datatype> := <default_value>;

For example −
• sales number(10, 2);
• name varchar2(25);
• address varchar2(100);
• name VARCHAR2(50) := ‘ZEBA';
PL/SQL Variable Constraints

NOT NULL
Can not be empty

CONSTANT
Can not be changed
PL/SQL Variables Examples with
and without Constraints
Age number;
Last char ( 10 );
DVal Date := Sysdate;
SID number not null;
Adjust constant number := 1;
CanLoop boolean := true
Program to Add 2 Numbers
Declare
Var1 integer;
Var2 integer;
Var3 integer;
Begin
Var1:=&var1;
Var2:=&var2;
Var3:=var1+var2;
Dbms_output.put_line('the sum of numbers is '|| var3);
End;
/
PL/SQL Control Structure
PL/SQL has a number of control structures which
includes:
Conditional controls
Iterative or loop controls.
Exception or error controls

It is these controls, used singly or together, that


allow the PL/SQL developer to direct the flow of
execution through the program.
PL/SQL Conditional Statements

PL/SQL IF-THEN Statement


PL/SQL IF-THEN-ELSE Statement
PL/SQL IF-THEN-ELSIF Statement
PL/SQL CASE Statement
PL/SQL IF-THEN Statement
Syntax:
IF condition THEN
Statements
END IF;
Example

declare
var1 integer;
begin
var1:=&var1;
if var1>20 then
dbms_output.put_line('Nu
mber is Greater Than 20');
end if;
end;
/
PL/SQL IF-THEN-ELSE Statement

Syntax:
IF condition THEN
Statements
ELSE
Statements
END IF
Example
declare
var1 integer;
begin
var1:=&var1;
if var1>10 and var1<20 then
dbms_output.put_line('Numbe
r is between 10 and 20');
else
dbms_output.put_line('Numbe
r is out of range');
end if;
end;
/
PL/SQL IF-THEN-ELSIF Statement

Syntax:
IF condition THEN
Statements
ELSEIF condition THEN
Statements
ELSE
Statements
END IF
PL/SQL CASE Statement

Syntax:
CASE selector
WHEN 'value1' THEN Statement1;
WHEN 'value2' THEN Statement2;
ELSE Statement
END CASE;
PL/SQL Loops

PL/SQL General Loop


PL/SQL FOR Loop
PL/SQL WHILE Loop
PL/SQL Nested Loop
PL/SQL General Loop

Syntax:
LOOP
Statement1;
Statement2;
EXIT;
END LOOP;
Example
declare
var1 integer;
begin
var1:=&var1;
loop
dbms_output.put_line(var1);
var1:=var1+1;
exit when var1>5;
end loop;
end;
/
PL/SQL FOR Loop

Syntax:
FOR counter IN var1....var2
LOOP statement1;
statement2;
END LOOP;
Example

declare
var1 integer;
begin
var1:=&var1;
for var1 in 1..5
loop
dbms_output.put_line(va
r1);
end loop;
end;
/
PL/SQL WHILE Loop

Syntax:
WHILE <condition>
LOOP statement1;
statement2;
END LOOP;
Example
declare
var1 integer;
begin
var1:=&var1;
while var1<5
loop
dbms_output.put_line(va
r10);
var1:=var1+1;
end loop;
end;
/

You might also like