2. Introduction to PLSQLBlock–
• PLSQL is oracle’s procedural language which provide extension to
SQL language.
• For accessing data in relational database PLSQL block is made up of
3 sections-
• 1) Declarative section (Optional)
• 2) Executable section(Required)
• 3) Exception handling(Optional)
• Only Begin and End keywords are required.
• Block structure of PLSQL
Declare (Optional)
We can declare variables, cursor,user defined
exceptions etc.
Begin(Required)
SQL statements or PLSQL statements.
Exception(Optional)
Action to be performed when error occurs.
End(Requird)
3. • The oracle engine can process entire PL/SQL block.
• Plsql blocks are sent to oracle engine, where procedural
statements executed in sql statements. which is sent to sql engine
in the oracle.
• [plsql engine reisde in oracle engine , this will improve speed of
operation.]
• PL/SQL runtime Enviroment:
PLSQL Execution envoroment
4. Pl/sql built in data types:
• 1) Numeric Datatypes:
• For this we used int datatype and numeric data type
• For ex: number(I,j);
• Where: i: Total no. of decimal digits.
j:Total no. of digits after digits.
ex: num(2,3)
• 2) Character String data type:
• For fixed length we used char(n): where blank spaces are
considered.
• Varchar(n): where no blank spaces are considered.
5. PL/SQL Code block
• A function can be used as a part of SQL expression i.e. we can
use them with select/update/merge commands.
• One most important characteristic of a function is that unlike
procedures, it must return a value.
• Syntax to create a function:
• Create[or replace] function function_name (arguments) returns
type as’
Declare
declarations;
[…….]
Begin
statements;
[…….]
End;
6. • Where,
• function-name specifies the name of the function.
• [OR REPLACE] option allows the modification of an existing
function.
• The optional parameter list contains name, mode and types of
the parameters. IN represents the value that will be passed
from outside and OUT represents the parameter that will be
used to return a value outside of the procedure.
• The function must contain a return statement.
• The RETURN clause specifies the data type you are going to
return from the function.
• function-body contains the executable part.
• The AS keyword is used instead of the IS keyword for creating
a standalone function.
7. • 1) Dclaring simple function to calculate the area of circle.
• Create or replace function circle_area() returns float as’
declare
pi float :=3.14;
r float default 5;
area float;
begin
area:=pi*r*r;
return area;
End;
‘language ‘plpgsql’;
• Now above is the sample program that you should follow
following steps to run on postgress are as follows-
• 1) Open the text editor
• 2) write above code , and save the file as filename.sql
• 3) now open the terminal and type the following commands-
8. • #] su – postgres
• #] createdb demo
• #] createlang plpgsql demo
• #]Psql demo
• #]i progname.sql
• #] select circle_area();
• Above given statements are for postgresql.
• While writing su – postgres it will connect to bash prompt
• After creating database by using createdb demo.
• createlang plpgsql demo will create language so that you are
connected to plpgsql language and then type psql demo , so it will
connects to the demo database.
• After that by using i progname .sql it will run that program and by
using select circle_area() it will display the output.
• Then the output will look like-
• Area
• --------
• 23
9. Attribute:%TYPE and%ROWTYPE
• To handle with database objects we use % type and
% rowtype attribute.
• %TYPE: It is used to declare variable with same type as the
database object structure.
• The most powerful advantage is that if in future the datatype
of database object changed then, the declared variable in
program will also changes automatically.
• Syntax: variable_name table_name.column_name%type
10. • For example:
• Create or replace function emp_dept(int) returns text as’
• Declare
• empid alias for $1;
• tname employee.ename%type;
• Begin
• select into tname ename from emp where eid=empid;
• return tname;
• End;
• ‘language’plpgsql’;
• %Rowtype: It is used to declare variable with the structure same
as row structure of table.
• Syntax: variable_name table_name.%rowtype
11. • For accessing the data of particular field of table –
• Syntax: variablename.fieldname
• Create function empdept(integer)returns text as’
• Declare
• empid alias for $1;
• emp employee %rowtype;
• Begin
• select into emp * from employee where eid=empid;
• return emp.empdpt;
• End;
• ‘language’plpgsql’;
12. Functions with Loop
• 3 types of loop in PLSQL
• 1) basic loop
• 2) while loop
• 3) for loop
• Programs for all above 3 loops are as follows-
• 1) Basic Loop:
• Create or replace function sum1() returns int as’
• Declare
• sum int default 0;
• num int default 1;
• Begin
• Loop
• sum:=sum+num;
13. • num:=num+1;
• Exit when num=5;
• End loop;
• Return sum;
• End;
• ‘language’plpgsql’;
• In above program , we are going to print 1 to 5 numbers
• And default keyword is used to set default value as 0 and 1 for sum and num
variable .
• 2) While Loop:
• Create or replace function sum1() returns int as’
• Declare
• sum int default 0;
• num int default 1;
• Begin
• While num<5 loop
• sum:=sum+num;
• num:=num+1;
• End loop;
• Return sum;
• End;
• ‘language’plpgsql’;
14. ForLoop:programtoprinttheaverageof firstfive
numbers
• Create or replace function sum1() returns int as’
• Declare
• i int default 0;
• avg int default 1;
• Begin
• For i in 1..5 loop
• avg:=avg+i;
• End loop;
• Avg:=avg/5;
• Return avg;
• End;
• ‘language’plpgsql’;