Vous êtes sur la page 1sur 12

/*declare

v_myage number:=&v_myage;
begin
if v_myage<11 then
dbms_output.put_line('i am a child');
elsif v_myage<20 then
dbms_output.put_line('i am a young');
elsif v_myage<30 then
dbms_output.put_line('i am in my twenties');
elsif v_myage<40 then
dbms_output.put_line('i am in my thirties');
else
dbms_output.put_line('i am always young');
end if;
end;*/
-------------------------------
declare
v_count number:=1;
begin
while v_count<=10 loop
dbms_output.put_line(v_count);
v_count:=v_count+1;
end loop;
end;
/

------------------------------
begin
for i in reverse 1..10 loop
dbms_output.put_line(i);
end loop;
end;
/

----------------------------------

declare
v_name m.name%type;
v_regno m.regno%type:=1;
v_counter number(2):=1;
begin
select max(name) into v_name
from m
where regno=&v_regno;
loop
insert into m1(regno,name)
values((v_regno+v_counter),v_name);
v_counter:=v_counter+1;
exit when v_counter>3;
end loop;
end;
/

collections[records]

-----------------------------------
declare
type t_rec is record
(v_sal number(9),
v_hire_date emp.hiredate%type,
v_name emp.ename%type);
v_myrec t_rec;
begin
select ename,sal,hiredate into
v_myrec.v_name,v_myrec.v_sal,v_myrec.v_hire_date
from emp where empno=7900;
insert into m1 values(v_myrec.v,v_sal,v_myrec.v_name);
dbms_output.put_line(v_myrec.v_name|| ' '||
v_myrec.v_hire_date||' '||v_myrec.v_sal);
end;
/

---------------------------------
declare
emp_rec emp%rowtype;
v_empno emp.empno%type:=&v_empno;
begin
select * into emp_rec
from emp
where empno=v_empno;
dbms_output.put_line(emp_rec.ename||emp_rec.sal||emp_rec.job);
end;
/

--------------------------------------
declare
type name_tab_type is table of
varchar2(20)
index by binary_integer;
name_tab name_tab_type;
begin
name_tab(-1):='cgi';
name_tab(1):='kolar';
name_tab(5):='1-07-19';
name_tab(10):='plsql';
name_tab(2):=21;
dbms_output.put_line(name_tab(-1)|| name_tab(10));
end;
/

---------------------------
declare
type name_tab_type is table of
varchar2(20)
index by binary_integer;
name_tab name_tab_type;
begin
name_tab(-1):='cgi';
name_tab(1):='kolar';
name_tab(5):='1-07-19';
name_tab(10):='plsql';
name_tab(2):=21;
dbms_output.put_line('the preious value'|| name_tab.prior(5));
dbms_output.put_line('the next value'|| name_tab.next(-1));
dbms_output.put_line('the first value'|| name_tab.first);
dbms_output.put_line('the last value'|| name_tab.last);
dbms_output.put_line('the count'||name_tab.count);
if name_tab.exists(-1) then
dbms_output.put_line('exist');
end if;
name_tab.delete(10);
dbms_output.put_line('the count'||name_tab.count);
end;
/

------------------------------------------------
declare
type emp_table_type is table of
sad%rowtype index by pls_integer;
my_emp_table emp_table_type;
max_count number(2):=13;
begin
for i in 10..max_count
loop
select * into my_emp_table(i) from sad
where regno=i;
end loop;
for i in my_emp_table.first..my_emp_table.last
loop
dbms_output.put_line(my_emp_table(i).name);
end loop;
end;
/

-----------------------------------------------
declare
cursor c1 is select ename,job from emp
where deptno=10;
v_ename emp.ename%type;
v_job v_ename%type;
begin
if not c1%isopen then
open c1;
end if;
loop
fetch c1 into v_ename,v_job;
exit when c1%notfound or c1%rowcount>=5;
dbms_output.put_line(v_ename|| ' '||v_job);
end loop;
close c1;
end;
/
--------------------------------------------
declare
cursor c1 is select * from emp;
emp_rec emp%rowtype;
begin
if not c1%isopen then
open c1;
end if;
loop
fetch c1 into emp_rec;
exit when c1%notfound ;
dbms_output.put_line(emp_rec.ename||' '||emp_rec.hiredate);
end loop;
close c1;
end;
/
------------------------------
FOR LOOP---
declare
cursor c_cursor is
select empno,sal from emp
where deptno=30;
begin
for emp_rec in c_cursor
loop
dbms_output.put_line(emp_rec.empno||' '||emp_rec.sal);
end loop;
end;
/
----------------------------------------
begin
for emp_record in(select empno,ename from emp)
loop
dbms_output.put_line(emp_record.empno||' '||emp_record.ename);
end loop;
end;
/
--------------------------------------------
CURSOR WITH PARAMETERS

declare
cursor cur_rec(v_deptno number,v_job varchar2)is select ename,sal from emp
where deptno=v_deptno
and job=v_job;
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
open cur_rec(&v_deptno,'&v_job');
loop
fetch cur_rec into v_ename,v_sal;
exit when cur_rec%notfound;
dbms_output.put_line(v_ename||v_sal);
end loop;
close cur_rec;
end;
/
-------------------------
declare
v_deptno emp.deptno%type:=&v_deptno;
v_ename emp.ename%type;
begin
select ename into v_ename
from emp
where deptno=v_deptno;
exception
when others then
dbms_output.put_line('an error occured');
dbms_output.put_line(sqlcode);
end;
/
--------------------------------------------------
declare
e1 exception;
v_ename emp.ename%type;
begin
delete from dept where deptno=&v_deptno;
-------------------------------------
USER DEFINED EXCEPTION
declare
row_count number;
dept_count number;
no_data_returned exception;
begin
select count(*)
into row_count
from emp
where deptno=&v_deptno
and job='&p_job';
if row_count=0 then
raise no_data_returned;
end if;
when no_data_returned
then
dbms_output.put_line('no records found');
end;
/
-----------------------------------------------
declare
e1 exception;
c_empid emp.empno%type;
cursor c_kt is select empno into c_empid from emp where sal=1;
begin
open c_kt;
fetch c_kt into c_empid;
if c_kt%notfound then
dbms_output.put_line('no data found');
end if;
close c_kt;
end;
/
----------------------------------------
declare
e1 exception;
pragma exception_init(e1,-01722);
begin
insert into dept(deptno,dname)values(null,'sales');
exception
when e1 then
dbms_output.put_line('insert operation failed');
dbms_output.put_line(sqlcode);
end;
/
==================================================
MULTIPLE EXCEPTION IS FOUND
declare
e1 exception;
e2 exception;
e3 exception;
pragma exception_init(e1,-02292);
pragma exception_init(e2,-01722);
begin
delete from dept where deptno='&v_deptno';
if sql%notfound then
raise e2;
end if;
exception
when e1 then
dbms_output.put_line(' u cannot delete parent table');
when e2 then
dbms_output.put_line('no data found');
when e3 then
dbms_output.put_line('no character found');
end;
/
==================================================
begin
delete from dept where deptno=&v_deptno;
if sql%notfound then
raise_application_error(-20111,'no data found');
end if;
end;
/
=============================================
declare
v_deptno emp.deptno%type:=&v_deptno;
v_ename emp.ename%type;
begin
select ename into v_ename
from emp
where deptno=v_deptno;
exception
when no_data_found then
raise_application_error(-20001,'no data found');
end;
/
====================================================
create or replace procedure pro1
(a in number,b number,c out number)
is
begin
c:=a+b;
end;
===================================================
ANANYMOUS BLOCK
create or replace procedure pro1
(v_empno number,v_ename out varchar2,
v_sal out number)
is
begin
select ename ,sal into
v_ename,v_sal
from emp
where empno=v_empno;
end;
/
=============================================
declare
cursor c1 is select * from dept;
cursor c2(pdno number) is select * from emp where deptno=pdno;
begin
for vdep in c1
loop
dbms_output.put_line('------------');
dbms_output.put_line(vdep.deptno||' '||vdep.dname);
dbms_output.put_line('----------');
for erec in c2(vdep.deptno)
loop
dbms_output.put_line(erec.empno||' '||erec.ename||' '||erec.deptno);
end loop;
end loop;
end;
/
=========================================
create or replace procedure raise_sal
(p_id in emp.empno%type,
p_percent in number)
is
begin
update e1
set sal=sal*(1+p_percent/100)
where empno=p_id;
end raise_sal;
/
===================================
declare
v_e1_name e1.ename%type;
v_e1_sal e1.sal%type;
begin
pro1(7782,v_e1_name,v_e1_sal);
dbms_output.put_line(v_e1_name||' earns'||to_char(v_e1_sal,'$999,999.00'));
end;
/
=======================================
create or replace procedure format_phone
(p_phone_no in out varchar2) is
begin
p_phone_no:='('||substr(p_phone_no,1,4)||
')'|| substr(p_phone_no,4,3)||
'-'||substr(p_phone_no,7);
end format_phone;
/
===============================
variable b_phone_no varchar2(15);
execute:b_phone_no:='6362841750'
print b_phone_no
execute format_phone(:b_phone_no)
print b_phone_no
============================================
create or replace procedure add_dpt
(p_name in dpt.dname%type,
p_loc in dpt.loc%type) is
begin
insert into dpt(deptno,dname,loc)
values(s2.nextval,p_name,p_loc);
end add_dpt;
/
======================================
execute add_dpt('training','kerala')
execute add_dpt(p_loc='dance',p_name='saima');
================================================
create or replace procedure add_dpt
(p_name in dpt.dname%type:='unknown' ,
p_loc in dpt.loc%type default 1700) is
begin
insert into dpt(deptno,dname,loc)
values(s2.nextval,p_name,p_loc);
end add_dpt;
/
execute add_dpt
execute add_dpt(p_loc=>'coorg')

==============PROCEDURE WITHIN A PROCEDURE=============================


create or replace procedure process_e1
is
cursor cur_e1_cursor is
select empno
from e1;
begin
for e1_rec in cur_e1_cursor
loop
raise_sal(e1_rec.empno,20);
end loop;
commit;
end process_e1;
/
execute raise_sal(800,20)
execute process_e1()
================FUNCTIONS==================
create or replace function fun1(a in number,b in number)
return number is
c number;
begin
c:=a+b;
return (c);
end;
/
variable z number;
exec:z:=fun1(143,143)
select sal ,comm,fun1(sal,comm) from emp;

==========================================
create or replace function get_sal
(p_id emp.empno%type)return number is
v_sal emp.sal%type:=0;
v_comm emp.comm%type;
begin
select sal,comm
into v_sal,v_comm
from emp
where empno=p_id;
v_sal:=v_sal+v_comm;
return v_sal;
end get_sal;
/
create or replace function get_sal
(p_id emp.empno%type)return number is
v_sal emp.sal%type:=0;
v_comm emp.comm%type;
begin
select sal,comm
into v_sal,v_comm
from emp
where empno=p_id;
v_sal:=v_sal+v_comm;
return v_sal;
end get_sal;
/
create or replace function get_sal
(p_id emp.empno%type)return number is
v_sal emp.sal%type:=0;
v_comm emp.comm%type;
begin
select sal,comm
into v_sal,v_comm
from emp
where empno=p_id;
v_sal:=v_sal+v_comm;
return v_sal;
end get_sal;
/create or replace function get_sal
(p_id emp.empno%type)return number is
v_sal emp.sal%type:=0;
v_comm emp.comm%type;
begin
select sal,comm
into v_sal,v_comm
from emp
where empno=p_id;
v_sal:=v_sal+v_comm;
return v_sal;
end get_sal;
/create or replace function get_sal
(p_id emp.empno%type)return number is
v_sal emp.sal%type:=0;
v_comm emp.comm%type;
begin
select sal,comm
into v_sal,v_comm
from emp
where empno=p_id;
v_sal:=v_sal+v_comm;
return v_sal;
end get_sal;
/create or replace function get_sal
(p_id emp.empno%type)return number is
v_sal emp.sal%type:=0;
v_comm emp.comm%type;
begin
select sal,comm
into v_sal,v_comm
from emp
where empno=p_id;
v_sal:=v_sal+v_comm;
return v_sal;
end get_sal;
/create or replace function get_sal
(p_id emp.empno%type)return number is
v_sal emp.sal%type:=0;
v_comm emp.comm%type;
begin
select sal,comm
into v_sal,v_comm
from emp
where empno=p_id;
v_sal:=v_sal+v_comm;
return v_sal;
end get_sal;
/create or replace function get_sal
(p_id emp.empno%type)return number is
v_sal emp.sal%type:=0;
v_comm emp.comm%type;
begin
select sal,comm
into v_sal,v_comm
from emp
where empno=p_id;
v_sal:=v_sal+v_comm;
return v_sal;
end get_sal;
/create or replace function get_sal
(p_id emp.empno%type)return number is
v_sal emp.sal%type:=0;
v_comm emp.comm%type;
begin
select sal,comm
into v_sal,v_comm
from emp
where empno=p_id;
v_sal:=v_sal+v_comm;
return v_sal;
end get_sal;
/
select get_sal(empno),sal,comm from emp;
==========================================
create or replace function dml_call_sql(p_sal number)
return number is
begin
insert into e1(empno,ename,email,hire_date,job,sal)
values(1,'frost','sadiya@gmail.com',
sysdate,'SA_MAN',p_sal;
return (p_sal+100);
end;
/
create or replace function dml_call_sql(p_sal number)
return number is
begin
insert into e1(empno,ename,email,hire_date,job,sal)
values(1,'frost','sadiya@gmail.com',
sysdate,'SA_MAN',p_sal;
return (p_sal+100);
end;
/
create or replace function dml_call_sql(p_sal number)
return number is
begin
insert into e1(empno,ename,email,hire_date,job,sal)
values(1,'frost','sadiya@gmail.com',
sysdate,'SA_MAN',p_sal;
return (p_sal+100);
end;
/

create or replace function dml_call_sql(p_sal number)


return number is
begin
insert into e1(empno,ename,email,hire_date,job,sal)
values(1,'frost','sadiya@gmail.com',
sysdate,'SA_MAN',p_sal;
return (p_sal+100);
end;
/

create or replace function dml_call_sql(p_sal number)


return number is
begin
insert into e1(empno,ename,email,hire_date,job,sal)
values(1,'frost','sadiya@gmail.com',
sysdate,'SA_MAN',p_sal;
return (p_sal+100);
end;
/

create or replace function dml_call_sql(p_sal number)


return number is
begin
insert into e1(empno,ename,email,hire_date,job,sal)
values(1,'frost','sadiya@gmail.com',
sysdate,'SA_MAN',p_sal;
return (p_sal+100);
end;
/
create or replace function dml_call_sql(p_sal number)
return number is
begin
insert into e1(empno,ename,email,hire_date,job,sal)
values(1,'frost','sadiya@gmail.com',
sysdate,'SA_MAN',p_sal;
return (p_sal+100);
end;
/
===============================================
create or replace function dml_call_sql(p_sal number)
return number is
begin
insert into e1(empno,ename,job,sal)
values(1,'frost','SA_MAN',p_sal);
return (p_sal+100);
end;
/
=========PACKAGE====================
create or replace package body pack1
is
function f1(x in number,y in number)return number
is
z number;
begin
z:=x+y;
return(z);
end;
procedure p1(a in number,b in number,c out number)
is
begin
c:=f1(a,b);
end;
end;
/
variable z number
desc pack1
exec pack1.p1(10,20:z);

==============================================
create or replace package global_consts is
c_mile_2_kilo constant number:=1.6093;
c_mile_2_mile constant number:=0.6214;
c_yard_2_meter constant number:=0.9144;
c_meter_2_yard constant number:=0.9144;
end global_consts;

begin
dbms_output.put_line('20 miles='||
20* global_consts.c_mile_2_kilo||'km');
end;

create function mtr2yrd(p_m number)return number is


begin
return(p_m*global_consts.c_meter_2_yard);
end mtr2yrd;
/
execute dbms_output.put_line(mtr2yrd(1))

Vous aimerez peut-être aussi