12 PLSQLIntro
12 PLSQLIntro
■ objectives :
–list the limitations of sql
–describe the need for pl/sql
–list pl/sql features
–list advantages of pl/sql
–list pl/sql character set, operators, symbols and data
types
–explain what is a variable
–describe variable declaration
–state variable naming rules
–explain what is constant
–describe pl/sql program structure
–construct a pl/sql block
sql : summary
■ sql
–the most powerful & comprehensive database language
■you can create, organise, retrieve and maintain data using sql
–flexible
and efficient language designed to manipulate
and examine relational data
–4 gl
■insulatesuser from data structure & algorithms
■describes what to do and not, how to do it
–but it does not provide programming language
features
advantages of pl / sql
■support for sql :
–supports all the functionalities of sql
–efficient database handling
■improved performance :
–sql processing: single statement
–pl/sql processing: block of statements
■reduces overheads
■improves performance
■block structure :
–pl/sql is a block structured language
–programs can divided into logical blocks of code
■higher productivity :
–procedural constructs
–pl/sql can be used with
■oracle database server
■oracle tool like forms, reports etc.
■common programming language for back-end and front-end
■portability :
–applications written in pl/sql are portable to
■any computer hardware
■any os environment
where oracle rdbms is running
pl/sql language
■character set
■reserve words
■arithmetic operators
■relational operators
■special operators
■logical operators
■miscellaneous symbols
pl/sql operators
■ arithmetic operators :
operator meaning
+ addition
* multiplication
** exponentiation
- subtraction
/ division
■ relational operators :
operator meaning
<>, ^=, != not equal to
> greater than
< less than
= equal to
>= greater than or equal to <=
less than or equal to
■ special operators
–is null
–like
–between
–in
■ logical operators
–not
–and
–or
■ miscellaneous symbols :
–( ) lists separators :
■e.g. name in (‘sameer’, ‘rajiv’, ‘amit’)
–; end of statement :
■e.g. procedure_name (arg1, arg2);
–. item separator
■e.g. select <col_list> from schema.table_name …. ;
–:= assignment
■e.g. rec_no := rec_no+1;
–|| concatenation
■e.g. full_name := ‘sameer’ || ‘agashe’;
–-- comment delimiter
■e.g. -- this is a comment
■syntax :
<var_name> <data_type> [not null] [:=<value>];
constants
■values that do not change during program execution
■declaration is same as variable declaration except
keyword constant
■syntax :
<const_name > constant <data type> :=<value>;
■constants must also be initialized
■examples :
pin_code constant number (6) := 411002;
pi constant number (4,3) := 3.142;
category constant varchar (25) default ‘executive’;
■ pl /sql block
–the declarative & the exception handling section are
optional
–the executable section is mandatory
–a block can have any number of nested blocks defined
within its executable section
–a nested block is called as sub block
the declarative section
■ used for declaring
–variables, constants
–cursors
–definition of errors : exceptions
■ declaring objects allocates storage space to it
null statement
handlers
■ solution :
–use of select … into … statement
–use of if ... then … else statement
decalre
v_empno number (4) := 7369;
v_comm emp.comm%type;
begin
select comm into v_comm from emp
where empno = v_empno;
if v_comm is null then
v_comm := 500;
update emp set comm = v_comm
where empno = v_empno;
else
v_comm := v_comm *1.25;
update emp set comm = v_comm
where empno = v_empno;
end if;
commit;
end;
conditional constructs :
■checks for the validity of a condition and accordingly
performs a corresponding action
■syntax :
if <condition> then
<statements>
[ elsif <condition> then
<statements> ]
...
[ else
<statements> ]
end if;
■ syntax 1 :
if <condition> then
<statements>
end if;
■ e.g.
if v_sal > 5000 then
v_comm := v_sal *.15;
end if;
■ syntax 2 :
if <condition> then
<statements>
[ else
<statements> ]
end if;
■ e.g.
if v_sal > 5000 then
v_comm := v_sal *.15;
else
v_comm := v_sal *.20;
end if;
■ syntax 3 :
if <condition> then
<statements>
[ else
if <condition> then
<statements>
end if; ]
end if;
conditional constructs :
■ example. 1 : for empno 7869
a) if his salary is less than 3000 then raise the
commission by 15% of salary,
b) if his salary is between 3000 and 5000 then raise the
commission by 200 + 12% of salary,
c) otherwise raise the commission by 500 + 10% of salary.
declare
v_empno number(4) := 7869;
v_comm emp.comm%type;
v_sal emp.sal%type;
begin
select sal, comm into v_sal, v_comm from emp
where empno = v_empno;
if v_sal < 3000 then
v_comm := v_comm + v_sal * .15;
elsif v_sal <= 5000 then
v_comm := v_comm + 200 + v_sal * .12;
else
v_comm := v_comm + 500 + v_sal * .1;
end if;
update emp set comm = v_comm
where empno = v_empno;
end;
iterative constructs :
■iterative constructs execute a sequence of
statements repeatedly
■iterative constructs in pl/sql are
–loop
–while - loop
–for – loop
e.g. :
loop
v_counter := v_counter + 1;
...
if v_counter > 50 then
exit;
end if;
end loop;
loop construct
exiting loop using exit when :
–condition can be specified with exit statement itself
loop
<statements>
exit when <condition>;
end loop;
■ e.g. :
loop
insert into temp ……..
v_counter := v_counter + 1;
exit when v_counter > 50;
end loop;
while - loop
■a while - loop is condition driven
■loop is executed as long as condition evaluates to
true
■syntax :
while <condition> loop
<statements>
end loop;
for - loop
■executes a set of statements specified no. of times.
■syntax :
for <counter> in [reverse] lowerbound .. upperbound
loop
<statements>
end loop;
■ example 1 :
for v_counter in 1..50 loop
insert into ….
:
end loop;
■ example 2 :
for v_counter in reverse 1..50 loop
insert into…..
:
end loop;
nested loops
you can have nesting to any number of loops
■labeling loop to identify them
label
■undeclared identifier
■label name placed before loop or
■ goto :
–transfers control to a label unconditionally
–not to be used normally
declare
v_counter binary_integer := 1;
begin
loop
insert into temp
values (v_counter, ‘loop index’);
v_counter := v_counter + 1;
if v_counter > 50 then
goto end_loop;
end if;
end loop;
<<end_loop>>
null;
end;
:
if
:
end if;
:
loop
:
end loop;
■ e.g. 1 if
:
end if;
:
if
:
end if;
■a goto statement can’t branch from an exception
■use
–improves readability
–if there are no executable statements, used to create
dummy sub programs for testing