plsqlnotes (1)
plsqlnotes (1)
begin
insert into t1(a) values(9);
insert into t1(a) values(10);
insert into t1(a) values(11);
insert into t1(a) values(12);
commit;
end;
/
begin
null;
end;
/
begin
dbms_output.put_line('hi');
dbms_output.put_line(123);
end;
/
begin
dbms_output.put_line('hi' || ' ' || 123);
end;
/
declare
a number := 100;
c date := sysdate; -- dynamic initialization
begin
dbms_output.put_line(a);
a := 10;
dbms_output.put_line(c);
c := sysdate-a;
dbms_output.put_line('hi' || ' '||a || ' '|| c);
end;
/
declare
a employees%rowtype;
b number := 100;
begin
dbms_output.put_line(b);
select * into a from employees where employee_id=100;
return;
dbms_output.put_line(a.first_name ||' '||a.salary||' '||a.hire_date);
end;
/
declare
g employees.first_name%type;
begin
select first_name into g from employees where employee_id = 100;
dbms_output.put_line(g);
end;
/
1.into clause
2.variable declaration
3.only one row
declare
g employees%rowtype;
begin
select * into g from employees where employee_id = 100;
dbms_output.put_line(g.first_name||' '||g.salary||' '||g.hire_date);
end;
/
declare
g employees.first_name%type;
h employees.salary%type;
begin
select first_name ,salary into g,h from employees where employee_id = 100;
dbms_output.put_line(g || ' '||h);
end;
/
declare
type j is record(first_name employees.first_name%type ,salary employees.salary
%type);
o j;
begin
select first_name ,salary into o from employees where employee_id = 100;
dbms_output.put_line(o.first_name || ' '||o.salary);
end;
/
declare
begin
dbms_output.put_line(1);
goto c;
dbms_output.put_line(2);
<<b>>
dbms_output.put_line(3);
<<a>>
dbms_output.put_line(4);
<<c>>
--return; comment line
dbms_output.put_line(5);
end;
/
declare
b number := 5;
begin
dbms_output.put_line(b||' '||b); -- 5 5
declare
b varchar(5) := 100;
begin
b := 10+b;
dbms_output.put_line(b||' '||b);
end;
dbms_output.put_line(b||' '||b);
end;
/
/*
control statements
****************
simple if
if then else
if then elsif
nested if
loop
for loop
while loop
case
*/
declare
g number := 5;
begin
delete from t1;
if g < 10 then
insert into t1(a) values(g);
end if;
commit;
end;
/
declare
g number := 9;
begin
--delete from t1;
commit;
if g > 10 then
insert into t1(a) values(g);
else
update t1 set a=g;
end if;
commit;
end;
/
declare
b varchar(50) := 'oracle';
begin
if b = 'java' then
dbms_output.put_line('80% placement assurance');
end if;
end;
/
declare
c number;
g varchar(5);
begin
c := 10;
g := 'java';
if g = 'java' then
if c > 5 then
dbms_output.put_line('success');
else
dbms_output.put_line('failed');
end if;
end if;
--dbms_output.put_line('end of the statement');
end;
/
declare
i number := 0;
begin
loop
i := i+1;
if mod(i,2) = 1 then
dbms_output.put_line(i);
end if;
begin
end;
/
begin
end;
/
begin
end;
/
begin
end loop;
end;
/
declare
h number;
begin
h := 1;
declare
a number := 0;
b number;
begin
select case when 1=1 then 100 end into b from dual; -- 100
select decode(1,2,3,4) into b from dual;
dbms_output.put_line(b); -- 4
case
else
dbms_output.put_line(125);
end case;
end;
/
Nested Block
***********
declare
a date := sysdate;
begin
dbms_output.put_line(a);
declare
a date;
begin
a := sysdate + 5;
dbms_output.put_line(a);
end;
end;
/
*********************************************************************
CURSOR => sql private work area , only one row can be processed at a time ,
compile time data fetching process
declare
a employees.salary%type;
begin
select salary into a from employees where employee_id = 109;
if sql%found then
insert into t1(a) values(a);
dbms_output.put_line(sql%rowcount);
end if;
end;
/
declare
--a employees%rowtype;
cursor cc is select * from employees;
a cc%rowtype;
begin
open cc;
fetch cc into a;
dbms_output.put_line(a.employee_id || ' '|| a.first_name);
close cc;
end;
/
declare
--a employees%rowtype;
cursor cc is select * from employees;
a cc%rowtype;
begin
open cc;
loop
fetch cc into a;
exit when cc%notfound;
dbms_output.put_line(a.employee_id || ' '|| a.first_name);
end loop;
dbms_output.put_line(cc%rowcount);
if cc%isopen then
close cc;
end if;
fetch cc into a;
end;
/
declare
b varchar(50);
cursor j(i in number) is select first_name from employees where department_id = i;
begin
open j(90);
loop
fetch j into b;
exit when j%notfound;
dbms_output.put_line(b);
end loop;
close j;
end;
/
declare
b varchar(50);
cursor j(i in number) is select first_name from employees where department_id = i;
begin
for k in j(90) loop
dbms_output.put_line(k.first_name);
end loop;
end;
/
declare
cursor ee(c in number) is select first_name from employees where department_id=c;
cursor dd(a in varchar) is select * from departments where department_name = a;
b dd%rowtype;
g varchar(50);
begin
open dd('Sales');
fetch dd into b;
open ee(b.department_id);
loop
fetch ee into g;
exit when ee%notfound;
dbms_output.put_line(g);
end loop;
dbms_output.put_line(ee%rowcount);
close ee;
close dd;
end;
/
declare
a number := 10,20; -- this is not possible coz only one value can be assigned
begin
dbms_output.put_line(a);
end;
/
declare
type t is table of varchar(50) index by pls_integer; -- binary_integer
type g is table of number index by varchar(1);
b g;
a t; -- collection variable
begin
a(1) := 'oracle';
a(2) := 123;
a(3) := 'c++';
a(4) := 456;
dbms_output.put_line(a.count);
dbms_output.put_line(a.prior(4) ); --3
dbms_output.put_line(a.next(1)); -- 2
for j in 1..a.count loop
dbms_output.put_line( a(j) );
end loop;
b('a') := 10;
b('b') := 20;
dbms_output.put_line(b.count);
dbms_output.put_line(b.first); -- index
dbms_output.put_line(b.last);
dbms_output.put_line(b('a')); -- 10
end;
/
declare
type c is table of varchar(50);
a c := c(1001,101,11,31,301,33,55,501,450,'oracle','java','c++');
begin
dbms_output.put_line(a.count);
for l in 1..a.count loop
dbms_output.put_line(a(l));
end loop;
a.delete(1,6);
dbms_output.put_line(a.count);
end;
/
declare
type j is varray(100) of number;
i j;
begin
i := j(11,12,13,36,45,58);
dbms_output.put_line(i.limit);
dbms_output.put_line(i.count);
i.trim(2);
dbms_output.put_line(i.count);
i.extend(2); -- only allocated space will get extended
dbms_output.put_line(i.count);
for k in 1..i.count loop
dbms_output.put_line(i(k));
end loop;
end;
/
declare
type h is table of varchar(50);
a h;
begin
select first_name bulk collect into a from employees;
dbms_output.put_line(a.count);
end;
/
declare
begin
for i in (select employee_id from employees) loop
insert into t1(a) values(i.employee_id);
end loop;
end;
/
*/
declare
type h is table of varchar(50);
a h;
begin
select employee_id bulk collect into a from employees;
forall j in a.first..a.last
insert into t1(a) values(a(j));
end;
/
declare
type a is table of number;
b a;
cursor d is select employee_id from employees;
begin
open d;
fetch d bulk collect into b;
forall c in b.first..b.last save exceptions
insert into t1(a) values(b(c));
close d;
exception
when others then
dbms_output.put_line(sql%bulk_exceptions.count); -- 57
end;
/
declare
type a is table of number;
b a;
cursor d is select employee_id from employees;
begin
open d;
loop
fetch d bulk collect into b limit 10; -- ?
exit when d%notfound;
forall c in b.first..b.last save exceptions
insert into t1(a) values(b(c));
commit; -- ?
end loop;
close d;
exception
when others then
dbms_output.put_line(sql%bulk_exceptions.count); -- 57
end;
/
Dynamic Sql
************
begin
execute immediate 'drop table t1';
end;
/
declare
a employees%rowtype;
begin
execute immediate 'create table t1(a number)';
execute immediate 'select * from employees';
execute immediate 'select * from employees where employee_id= 200' into a;
dbms_output.put_line(a.first_name||' '||a.salary);
end;
/
declare
a employees%rowtype;
begin
execute immediate 'select * from employees where employee_id= :i' into a using 102;
dbms_output.put_line(a.first_name||' '||a.salary);
end;
/
declare
a employees.first_name%type;
b varchar(50) := 'salary';
begin
execute immediate 'select '|| b ||' from employees where employee_id= :i' into a
using 102;
dbms_output.put_line(a);
end;
/
j := 'first_name';
select j into i from employees where rownum = 1;
dbms_output.put_line(i);
declare
a varchar(50);
begin
for j in (select employee_id from employees) loop
execute immediate 'select first_name from employees where employee_id = :greens'
into a using j.employee_id;
dbms_output.put_line(a);
end loop;
end;
/
declare
c varchar(50) := 'last_name';
q departments%rowtype;
a varchar(50);
o varchar(1000) := 'select * from departments where department_id = 90';
begin
execute immediate 'select c from employees where employee_id = :g' into a using
110; -- error why ?
execute immediate 'select '||c||' from employees where employee_id = :g' into a
using 100;
dbms_output.put_line(a);
execute immediate o into q;
end;
/
exec second_sp(140);
exec second_sp(141);
declare
w varchar(50);
x date;
begin
second_sp(b => 150 , c => w , d => x);
end;
/
clear scr;
create or replace procedure inout_sp( a in out varchar) as
begin
dbms_output.put_line(a);
select first_name into a from employees where employee_id=a;
dbms_output.put_line(a);
end;
/
declare
p varchar(50);
begin
p := 175;
inout_sp(p);
end;
/
declare
x number;
c date;
begin
c := tst2(24000,x);
dbms_output.put_line(x);
dbms_output.put_line(c);
end;
/
declare
b date;
begin
b := tst2(101,b);
dbms_output.put_line(b);
end;
/
create or replace type h2o is table of number;
/
3.whereas in function we can not perform dml and it must return a value. to make
dml perform we can use pragma autonomous_transaction. we can not do ddl in
function.
**********************************************************************
trigger => to set a rule on a table . it automatically fires when an event occurs
logon
logoff
events
end;
/
before after
stment rowlevel
insert insert
update update
delete delete
compound trigger
before statement is
begin
insert into t3(c) values(50);
end before statement;
after statement is
begin
insert into t5(e) values(51);
end after statement;
after each row is
begin
insert into t6(f) values(88);
end after each row;
end;
/
*******************************************************************
create or replace function outout(a out number , b out date) return varchar
begin
select salary , hire_date into a,b from employees where rownum=1;
return a||' '||b;
end;
/
declare
a number;
b date;
c varchar(50);
begin
c := outout(a,b);
dbms_output.put_line(c);
end;
/
in
out
in out
declare
n varchar(50);
begin
sp(b=>104,a=>n);
end;
/
declare
a varchar(50) := 118;
begin
sp1(a);
end;
/
declare
x date;
begin
x := f1(145);
dbms_output.put_line(x);
end;
/
procedure p1 as
begin
dbms_output.put_line(1);
end p1;
end pkg;
/
execute pkg.p1;
execute pkg.p2(a=>789);
select pkg.f5 from dual;
package overloading
***************************************
declare
i utl_file.file_type;
begin
i := utl_file.fopen('PRQ','prq.txt','w',32767);
declare
j utl_file.file_type;
k varchar(2000);
begin
j := utl_file.fopen('ABC','list1.txt','r',32767);
for i in 1..3 loop
utl_file.get_line(j,k,32767);
dbms_output.put_line(k);
end loop;
utl_file.fclose(j);
end;
/
XML
****
select dbms_xmlgen.getxml('select first_name , salary , hire_date from employees
where salary > 15000')
from dual;
exception handling
****************
two types of timing error => compile time error / run time error
predefined exceptions
no_data_found
invalid_cursor
too_many_rows
dup_val_on_index
zero_divide
exception
when no_data_found then
dbms_output.put_line(sqlerrm);
declare
e exception;
pragma exception_init(e,-1476);
f exception;
pragma exception_init(f,-1402);
g exception;
pragma exception_init(g,-1);
begin
dbms_output.put_line(5/0);
exception
when e then
dbms_output.put_line('0 divide');
when f then
dbms_output.put_line('no data found');
when g then
dbms_output.put_line('too many rows');
end;
/
user defined
***********
declare
a exception;
begin
dbms_output.put_line(5/2);
dbms_output.put_line(10/2);
raise a;
dbms_output.put_line(20/2);
exception
when a then
dbms_output.put_line('test 1');
end;
/
begin
dbms_output.put_line('test 1');
insert into t1(a) values(10);
raise_application_error(-20152,'hi this is for testing'); -- -20000 to -20999
delete from t1;
end;
/
begin
dbms_output.put_line('test 1');
dbms_output.put_line('test 1');
dbms_output.put_line('test 1');
dbms_output.put_line(8/0);
dbms_output.put_line('test 1');
dbms_output.put_line('test 1');
dbms_output.put_line('test 1');
exception
when others then
dbms_output.put_line( dbms_utility.format_error_backtrace() ); -- error line
dbms_output.put_line( dbms_utility.format_error_stack() ); -- sqlerrm
end;
/
*********************************************************************
Ref cursor - will accept multiple datatype , data - holds a select statement
real time - to display set of data with multiple set of datatypes in the ui (eg -
lov - drop down )
declare
s sys_refcursor;
begin
open s for select * from employees;
end;
/
declare
s sys_refcursor;
type t is table of employees%rowtype;
a t;
begin
open s for select * from employees;
fetch s bulk collect into a;
for j in 1..a.count loop
dbms_output.put_line(a(j).first_name||' ' ||a(j).salary);
end loop;
end;
/
sqlplus method
variable ac refcursor
exec refsp(ab=>:ac);
print ac;
two types
strongly
weakly
variable a refcursor;
variable b refcursor;
print a;
print b;
Toad => schema browser => choose procedure (left hand side) => right click on your
procedure => click execute => give inputs if appicable else give ok
**********************************************************************
Local Procedure
Declare
procedure g1;
procedure g1 as
begin
insert into t1(a) values(1);
commit;
end;
procedure g2;
procedure g2 as
begin
delete from t1 where a=5;
commit;
end;
begin
g1;
g2;
end;
/
procedure p3test;
procedure p3test as
begin
dbms_output.put_line(1+2+3);
end;
begin
p2test;
p3test;
select * into a from departments where rownum=1;
end;
/
*********************************************************************
Forward Declaration
Declare
procedure xy;
procedure yz; -- forward declaration
procedure xy as
begin
yz;
dbms_output.put_line(10);
end;
procedure yz as
begin
dbms_output.put_line(1);
end;
begin
xy;
end;
/
**********************************************************************