3. PL/SQL Block
PL/SQL Block has three parts
Declaration
Execution
Exception
Structure of PL/SQL Block
Declare
Variables;
Begin
Statements;
Exception
Statements;
End;
3
4. PL/SQL Data Types
Same Data Types which SQL supports like char, varchar2, number, long,
raw, lobs etc
Composite Data Types
%type
If the declaration variable width is not suitable width then we can get an error called
numeric or value error. To over come this we can declare the variable type as
%type.
For example to declare one variable as job attribute type of employee table we can use as
j1 employee.job%type
%rowtype
To retrieve the information from the table which is having more number of attributes
instead of declaring more number of variable we can declare only one variable as
%rowtype. For example let us take employee table having 7 attributes instead of
declaring 7 variables we can declare only one variable as employee%rowtype
4
5. Programming constructions
Programming constructions represent how the program is going to be
executed. There are three programming constructions
Sequence
Without any conditions if statements are executed that is called Sequence.
Selection
Based on the condition if the execution control is changed from one place to another
place. For this purpose we can use conditional statements if, select case
Syntax (IF-THEN) Syntax(IF-THEN-ELSE)
IF condition THEN IF condition THEN
Statements; Statements;
END IF; ELSE
Statements;
END IF;
5
IF condition THEN {...statements to execute when condition is TRUE...} END IF;
6. Programming Constructions
Syntax (IF-THEN-ELSIF-THEN-ELSE) Syntax (Case-Selector)
IF condition THEN Case Expression
Statements; when value then statement
ELSIF condition THEN when value then statement
Statements; else
ELSE End Case
Statements;
END IF
6
7. Programming Constructions
Iteration or Repetition
Executing some set of statements number of time is called Iteration or Repetition.
For this purpose we can use loops.
Syntax For .. Loop
For variable in Starting number . . Ending number Loop
Statements;
End Loop;
Syntax While .. Loop
While condition Loop
Statements;
End Loop;
Syntax for Repeat .. Until Loop (same as Do . . While)
Loop
Statements;
EXIT when condition;
End Loop;
7