PL/SQL - Lec 14

General Stucture:

content_copy
declare
	declaration statements
begin
	execuatable statements
exception
	to customise error msgs
end;
/

Example: Program to find the months between two predefined dates

content_copy
declare
	i int := 0; 	-- ":=" is assignment operator 
	j int;

	dt date := sysdate;
	dt_1 date := '12-oct-1970';
begin
	i := months_between(dt, dt_1);
	dbms_output.put_line('Months between ' || dt || ' and ' || dt_1 || ' = ' || i);
end;
/

User Input

content_copy
declare
	i int := 0; 	-- ":=" is assignment operator 
	j int;

	dt date := :dt;
	dt_1 date := :dt;
begin
	i := months_between(dt, dt_1);
	dbms_output.put_line('Months between ' || dt || ' and ' || dt_1 || ' = ' || i);
end;
/

Conditional Statements

content_copy
declare 
	i int;
	j int;

	dt_1 date := '1-mar-2002'
	dt_2 date := '1-feb-2003'
begin
	i = monts_between(dt_1, dt_2);
	if(i < 10) then
		dbms_output.put_line('Gap less than 10');
	elseif(i < 20) then
		dbms_output.put_line('Gap less than 20 but > 10');
	else
		dbms_output.put_line('Gap greater than 20');
end;
/

Loop

content_copy
declare
	i int;
begin
	for i in 1..10 loop
		dbms_output.put_line(j);
	end loop;
	
	-- to reverse the loop

	for i in reverse 1..10 loop
		dbms_output.put_line(j);
	end loop;

	-- while
	
	i := 0;
	while ( j<= 9) loop
		j := j + 1;
		dbms_output.put_line(j);
	end loop
end;
/

Record Variables and Cursor

Defining a cursor and record variable

Cursor syntax

Record Variable syntax

content_copy
declare
	cursor c_batsman is select * from batsmen;
	r_batsman c_batsman%rowtype;
begin
	open c_batsman;
	loop
		if(c_batsman%notfound) then
			exit;
		end if;

		fetch c_batsman into r_batsman;
		dbms_output.put_line(r_batsman.name);
	end loop;
	close c_batsman;
end;
/

Cursor for loop

content_copy
declare
	cursor c_batsman is select * from batsmen;
	r_batsman c_batsman%rowtype;
begin
	for r_batsman in c_batsman loop
		dbms_output.put_line(r_batsman.name);
	end loop;
end;
/

Advantages of cursor

Procedure

Parameters in procedure

content_copy
create procedure <procedure_name>(... paramslist) as
content_copy
exec <procedure_name>(... paramslist)

PL/SQL - Lec 15

Creating a function:

content_copy
create function <function_name>(... params list) return <return_type> as
	-- declaration statements
begin
	-- executable statemsnts with a return statement
end;
/
content_copy
select func_name(... params) from dual;

Example: Count number of entries in batman table

content_copy
create function count_batsman return int as
	cursor c_batsman is select count(*) cnt from batsman;
	r_batsman c_batsman%rowtype;
begin
	open c_batsman;
	fetch c_batsman into r_batsman;
	return r_batsman.cnt;
	close c_batsman;
end;
/

Returning multiple records using a funciton

Nested Cursor

content_copy
declare
	cursor c_batsman is select * from batsman;
	r_batsman c_batsman%rowtype;

	-- parameterized constructor
	cursor c_rating(b_id batsman.batsman_id&type) is select * form rating where batsman_id=b_id;
	r_rating c_rating%rowtype;
begin
	open c_batsman;
	loop
		fetch c_batsman into r_batsman;

		-- exit condition to be returned after fetching
		
		if c_batsman%notfound the
			exit;
		end if;
		dbms_output.put_line('Rating of:' r_batsman.name);

		open c_rating(r_batsman.batsman_id);
		loop
			fetch c_rating into r_rating;
			if c_rating%notfound then
				exit;
			end if;
			dbms_output.put_line(r_batsman.rating_in||' '||r_batsman.ratings);
		end loop;
		close c_rating;
	end loop;
	close c_batsman;

PL/SQL - Lec 16

Triggers

Advantages / Use cases

Types

  1. Statement Level Trigger
    • Gets fired only when a statement is executed
  2. Row Level Trigger
    • Gets fired for each row
    • we add (for each row) while creating the table
    • We have two inbuilt variables for row level trigger
      1. :new
        • Example trigger is created on insert into emp values(1, 'abc', '12-dec-1999);
        • If we want to refer this values we can use them as :new.emp_id
      2. :old
        • This can be used when we are deleting something
        • While updating we may need both :new and :old

Example

content_copy
create trigger check_title before insert or update on <table_name>
for each row
begin
	if(:new.field_name is null) then
		-- in built procedure to raise error
		raise_application_error(code, 'error-msg');
	end if
end;
/