Co3 Session 17
Co3 Session 17
Session-17
[ declare
declarations ] /* Declaration section: declare all variables here to be used within the body
section.
begin /* body section: contains set of PL/pgSQL statements. Each statement end with ;
statements;
... /* Blocks can be nested. A
nested block is a block placed
inside the body of another
end [ label ]; block.
2
DECLARATIONS
3
DECLARATIONS..
row_variable table_name%ROWTYPE;
row_variable view_name%ROWTYPE;
• To access the individual field of the row variable, we can use the dot notation (.)
row_variable.field_name
We can use row type variables (%ROWTYPE) to hold a row of a result set returned by the select
into statement.
• PostgreSQL provides a “type” called the record that is like the row-type. It can hold only one row
of a result set. Unlike a row-type variable, a record variable does not have a predefined structure.
The structure of a record variable is determined when the select or for statement assigns an
actual row to it.
variable_name record;
4
CODE EXAMPLES OF ROWTYPE AND RECORD TYPE VARIABLES
/* This example shows the first name and last name of the actor id
do $$ 10.
do
declare $$
declare
selected_actor actor%rowtype;
rec record;
begin begin
for rec in select title, length from film where
-- select actor with id 10 length > 50
select * from actor into selected_actor where actor_id = 10; order by length
loop
-- show the number of actor raise notice '% (%)', rec.title,
rec.length; end loop;
raise notice 'The actor name is % %',
end;
selected_actor.first_name, $$
selected_actor.last_name;
end; $$
5
PL/pgSQL Select Into statement
Control structures: if
statement
PL/pgSQL provides you with three forms of the if statements.
•if then Syntax if then elif
•if then else if condition_1 then
•if then elsif statement_1;
Syntax if then elsif condition_2 then
if condition then statement_2
statements; ...
end if; elsif condition_n then
Syntax if then else
statement_n;
if condition then
else
statements;
else-statement;
else
end if;
alternative-statements;
end if;
6
CODE EXAMPLES
do $$ do $$
declare declare
selected_film film%rowtype; selected_film film%rowtype;
input_film_id film.film_id%type := 100; input_film_id film.film_id%type := 0;
begin begin
select * from film select * from film
into selected_film into selected_film
where film_id = input_film_id; where film_id = input_film_id;
if not found then if not found then
raise notice 'The film % could not be found', raise notice 'The film % could not be
input_film_id; found',
else input_film_id;
raise notice 'The film title is %', selected_film.title; end if;
end if; end $$
end $$
The found is a global variable that is available in PL/pgSQL procedure language.
The select into statement sets the found variable if a row is assigned or false if no row is returned.
7
CONTROL STRUCTURES: CASE
STATEMENT
If the case statement
The case statement has two forms: cannot find any
• Simple case statement match, it will execute
• Searched case statement the else section.
case statement selects a section to execute based on condition.
The else section is
Syntax of simple case statement: Syntax of Searched case
optional.
case search-expression statement:
when expression_1 [, expression_2, ...] then case
when-statements when boolean-expression-1
[ ... ] then
[else statements
else-statements ] [ when boolean-expression-2
END case; then
statements
... ]
[ else
statements ]
8 end case;
CODE EXAMPLES
do $$ else
declare price_segment = 'Unspecified';
rate film.rental_rate%type; end case;
price_segment varchar(50); raise notice '%', price_segment;
begin end if;
-- get the rental rate end; $$
select rental_rate into rate
from film
where film_id = 100;
-- assign the price segment
if found then
case rate
when 0.99 then
price_segment = 'Mass';
when 2.99 then
price_segment = 'Mainstream';
when 4.99 then
price_segment = 'High End';
9
CONTROL STRUCTURES: LOOPS
-- executes a block of code repeatedly based on a condition.
While loop Syntax: For loop Syntax:
[ <<label>> ]
[ <<label>> ]
for loop_counter in [ reverse ] from.. to [ by step ]
while condition loop
statements;
loop
end loop; statements
do $$ end loop [ label ];
10
LOOP USED TO ITERATE THE RESULTSET OF A QUERY: CODE EXAMPLE.
11
STORED PROCEDURES AND FUNCTIONS IN
PLPGSQL
•Procedures and functions are very similar to those found in most high-level
programming languages and have the same advantages:
•aid abstraction
•In databases, many applications can access the procedure stored on the server.
Server acts as a single point of change.
12
CREATE AND CALL PROCEDURE IN PLPGSQL
14
CREATE AND CALL FUNCTION IN PLPGSQL
16
PL/PGSQL PARAMETER MODES AND CODE EXAMPLE
17