Programming in Oracle With PL/SQL
Programming in Oracle With PL/SQL
Programming in Oracle
with PL/SQL
2. Named Blocks:
Procedures
Functions
BEGIN (mandatory)
/* Here you define the executable statements (what the block DOES!)*/
EXCEPTION (optional)
/* Here you define the actions that take place if an exception is thrown during
the run of this block */
END; (mandatory)
/
Always put a new line with only a / at the end A correct completion of a block will generate
of a block! (This tells Oracle to run the block) the following message:
PL/SQL procedure successfully completed
DCIPL, Pune, India 3
DCIPL, Pune, India 4
DECLARE
Syntax
identifier [CONSTANT] datatype [NOT NULL]
[:= | DEFAULT expr];
reserves_record Reserves%ROWTYPE;
Syntax Example:
sailorData is a
DECLARE variable that can
hold a ROW from
sailorData sailors%ROWTYPE;
f BEGIN
e 6 open rad_cursor;
fetch rad_cursor into rad_val;
t
c 8 area:=pi*power(rad_val.radius,2);
insert into AREAS values (rad_val.radius, area);
h close rad_cursor;
END;
Rad_val /
AREAS
Radius Area
3 28.27
DECLARE DECLARE
… …
cursor rad_cursor is select * from RAD_VALS; cursor rad_cursor is select * from RAD_VALS;
rad_value rad_cursor%ROWTYPE; rad_value RAD_VALS.radius%TYPE;
BEGIN BEGIN
open rad_cursor; open rad_cursor;
fetch rad_cursor into rad_val; fetch rad_cursor into rad_val;
area:=pi*power(rad_val.radius,2); area:=pi*power(rad_val,2);
insert into AREAS values (rad_val.radius, insert into AREAS values (rad_val, area);
area); …
…
DECLARE DECLARE
… …
cursor rad_cursor is select * from RAD_VALS; cursor rad_cursor is select radius from
rad_value RAD_VALS%ROWTYPE; RAD_VALS;
BEGIN rad_value RAD_VALS.radius%TYPE;
open rad_cursor; BEGIN
fetch rad_cursor into rad_val; open rad_cursor;
area:=pi*power(rad_val.radius,2); fetch rad_cursor into rad_val;
insert into AREAS values (rad_val.radius, area:=pi*power(rad_val,2);
area); insert into AREAS values (rad_val, area);
… …
Explicit Cursor Attributes
If <cond> If <cond>
then <command> then
elsif <cond2> if <cond2>
then <command2> then
else <command1>
<command3> end if;
end if; else <command2>
end if;
IF-THEN-ELSIF Statements
...
IF rating > 7 THEN
v_message := 'You are great';
ELSIF rating >= 5 THEN
v_message := 'Not bad';
ELSE
v_message := 'Pretty bad';
END IF;
...