declare
declaration statements
begin
execuatable statements
exception
to customise error msgs
end;
/
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;
/
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;
/
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;
/
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;
/
cursor <c_name> is <select_query>;
cursor name should be c_short_name_for_table
General Steps:
<r_name> <c_name>%rowtype; -- afaiu, declares i.e. makes the datatype same as that of the record
Example
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;
/
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;
/
create procedure <procedure_name>(... paramslist) as
exec <procedure_name>(... paramslist)
create function <function_name>(... params list) return <return_type> as
-- declaration statements
begin
-- executable statemsnts with a return statement
end;
/
select func_name(... params) from dual;
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;
/
r_batsman.cnt
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;
:new.emp_id
:new
and
:old
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;
/
before or after