0% found this document useful (0 votes)
10 views10 pages

AKAB

Uploaded by

rintureji20041
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

AKAB

Uploaded by

rintureji20041
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

sum of three number

DECLARE
A number;
B number;
C number;
S number;
BEGIN
A:=&A;
B:=&B;
C:=&C;
S:=(A+B+C);
dbms_output.put_line('sum'||"S");
END;
/
Enter value for a: 2
old 7: A:=&A;
new 7: A:=2;
Enter value for b: 3
old 8: B:=&B;
new 8: B:=3;
Enter value for c: 5
old 9: C:=&C;
new 9: C:=5;
sum10

average of three number

DECLARE
A number;
B number;
C number;
S number;
AVG number;
BEGIN
A:=&A;
B:=&B;
C:=&C;
S:=(A+B+C);
AVG:=(S/3);
dbms_output.put_line('sum '||"S");
dbms_output.put_line('average '||"AVG");
END;
/
Enter value for a: 5
old 8: A:=&A;
new 8: A:=5;
Enter value for b: 5
old 9: B:=&B;
new 9: B:=5;
Enter value for c: 5
old 10: C:=&C;
new 10: C:=5;
sum 15
average 5

grade of student
DECLARE
P number;
BEGIN
P:=&P;
if(P>=80)then
dbms_output.put_line('grade:distinction');
elsif(p>60 and p<=79)then
dbms_output.put_line('grade:first class');
elsif(p>50 and p<=59)then
dbms_output.put_line('grade:second class');
else
dbms_output.put_line('fail');
end if;
END;
/
Enter value for p: 99
old 4: P:=&P;
new 4: P:=99;
grade:distinction

factorial of number

DECLARE
N number;
F number;
I number;
BEGIN
F:=1;
N:=&N;
for I IN 1..N
LOOP
F:=F*I;
END LOOP;
dbms_output.put_line('factorial:'||"F");
END;
/
Enter value for n: 5
old 7: N:=&N;
new 7: N:=5;
factorial:120

sum of n number

DECLARE
N number;
S number;
I number;
BEGIN
S:=0;
N:=&N;
for I IN 1..N
LOOP
S:=S+I;
END LOOP;
dbms_output.put_line('sum:'||"S");
END;
/
Enter value for n: 5
old 7: N:=&N;
new 7: N:=5;
sum:15

reverse a number

DECLARE
N NUMBER;
T NUMBER;
R NUMBER;
S NUMBER;
BEGIN
S:=0;
N:=&N;
T:=N;
while(T>0)
LOOP
R:=T MOD 10;
S:=(S*10)+R;
T:=TRUNC(T/10);
END LOOP;
dbms_output.put_line('reverse of '||"N"||' is '||"S");
END;
/
Enter value for n: 125
old 8: N:=&N;
new 8: N:=125;
reverse of 125 is 521

prime of number

DECLARE
N number;
I number;
C number;
P number;
BEGIN
C:=0;
N:=&N;
for I IN 2..N/2
LOOP
P:=MOD(N,I);
if (P=0) THEN
C:=C+1;
END if;
END LOOP;
if (C=0)THEN
dbms_output.put_line('the given number '||"N"||' is prime');
else
dbms_output.put_line('the given number '||"N"||' is not a prime');
END if;
END;
/
Enter value for n: 5
old 8: N:=&N;
new 8: N:=5;
the given number 5 is prime
PROCEDURE

CREATE TABLE EMPE(empid number primary key,empname varchar (23),salary number);


INSERT INTO EMPE VALUES(23,'MAYA',4500);
INSERT INTO EMPE VALUES(24,'GEETHU',2500);
INSERT INTO EMPE VALUES(34,'HARI',34500);
INSERT INTO EMPE VALUES(63,'HRIDHI',4800);

create or replace procedure inser (id number,amt number) is


sal number;
sal_error exception;
begin
select salary into sal from EMPE where empid=id;
if sal is null then
raise sal_error;
else
update EMPE set salary=salary+amt where empid=id;
end if;
EXCEPTION
when sal_error then
dbms_output.put_line(id||'has salary as null');
when no_data_found then
dbms_output.put_line(id||' no id exist');
end inser;
/
procedure created.
exec inser (23,900);

select * from EMPE;

EMPID EMPNAME SALARY


------ ----------------------- ----------
23 MAYA 5400
24 GEETHU 2500
34 HARI 34500
63 HRIDHI 4800

FUNCTION

create or replace function fun1(eid varchar)


return number
IS
incr number;
sal number;
sal_error exception;
begin
select salary into sal from EMPE where empid=eid;
if sal is null then
raise sal_error;
else
if sal>10000 then
incr:=0.3*sal;
elsif sal>5000 and sal<=10000 then
incr:=0.2*sal;
else
incr:=0.1*sal;
end if;
end if;
return incr;
EXCEPTION
when sal_error then
dbms_output.put_line(eid||'has salary as null');
when no_data_found then
dbms_output.put_line(eid||':no such id exist');
end fun1;
/
Function created.

declare
eid varchar(5);
amt number;
begin
eid:=&eid;
amt:=fun1(eid);
dbms_output.put_line('increment amount:'||amt);
end;
/
Enter value for eid: 34
old 5: eid:=&eid;
new 5: eid:=34;
increment amount:10350

PL/SQL procedure successfully completed.

PACKAGE

create or replace package pack1 as


procedure inser(eid varchar,ename varchar,salary number);
procedure del(eid varchar);
end pack1;
/
Package created.

create or replace package body pack1 as


procedure inser(eid varchar,ename varchar,salary number)is
begin
insert into EMPE values(eid,ename,salary);
end inser;
procedure del(eid varchar)is
begin
delete from EMPE where empid=eid;
end del;
end pack1;
/
Package body created.

execute pack1.inser('75','kithu',5500);
select * from EMPE;
EMPID EMPNAME SALARY
-------- ----------------------- ----------
23 MAYA 4500
24 GEETHU 2500
34 HARI 34500
63 HRIDHI 4800
75 kithu 5500
execute pack1.del('63');
select * from EMPE;

EMPID EMPNAME SALARY


------ ----------------------- ----------
23 MAYA 4500
24 GEETHU 2500
34 HARI 34500
75 kithu 5500

CURSOR

create table stdnt(sno int primary key,sname varchar(10),address varchar(10),m1


int,m2 int,m3 int);
insert into stdnt values(1,'kithu','love',99,56,87);
insert into stdnt values(2,'karthik','sky',5,8,47);
insert into stdnt values(3,'anakha','cute',89,98,100);
insert into stdnt values(4,'arjun','handsome',19,96,77);
create table stdnt1(sno int primary key,sname varchar(10),total int,result
varchar(10));

declare
cursor c1 is select * from stdnt;
total number(3);
result varchar(10);
begin
for i in c1 loop
total:=i.m1+i.m2+i.m3;
if(i.m1>40 and i.m2>40 and i.m3>40)then
result:='pass';
else
result:='fail';
end if;
insert into stdnt1 values(i.sno,i.sname,total,result);
end loop;
end c1;
/
select * from stdnt1;

SNO SNAME TOTAL RESULT


----- ---------- ---------- ----------
1 kithu 242 pass
2 karthik 60 fail
3 anakha 287 pass
4 arjun 192 fail

create table stdnt11(sno int primary key,sname varchar(10),total int,result


varchar(10));
create table stdnt111(sno int primary key,sname varchar(10),total int,result
varchar(10));
declare
cursor c2 is select * from stdnt1;
total number(3);
result varchar(10);
begin
for i in c2 loop
if(i.result='pass')then
insert into stdnt11 values(i.sno,i.sname,i.total,i.result);
else
insert into stdnt111 values(i.sno,i.sname,i.total,i.result);
end if;
end loop;
end c2;
/

select * from stdnt11;

SNO SNAME TOTAL RESULT


------ ---------- ---------- ---------
1 kithu 242 pass
3 anakha 287 pass

select * from stdnt111;

SNO SNAME TOTAL RESULT


------ ---------- ---------- ---------
2 karthik 60 fail
4 arjun 192 fail

create table stdnt1111(sno int primary key,sname varchar(10),total int,result


varchar(10));
declare
cursor c3 is select * from stdnt11;
cursor c4 is select * from stdnt111;
total number(3);
result varchar(10);
begin
for i in c3 loop
if(i.result='pass')then
insert into stdnt1111 values(i.sno,i.sname,i.total,i.result);
end if;
end loop;
for i in c4 loop
if(i.result='fail')then
insert into stdnt1111 values(i.sno,i.sname,i.total,i.result);
end if;
end loop;
end;
/
select * from stdnt1111;

SNO SNAME TOTAL RESULT


------ ---------- ---------- ----------
1 kithu 242 pass
3 anakha 287 pass
2 karthik 60 fail
4 arjun 192 fail

declare
cursor c5 is select * from stdnt;
a stdnt.sno%type;
b stdnt.sname%type;
c stdnt.address%type;
d stdnt.m1%type;
e stdnt.m2%type;
f stdnt.m3%type;
begin
open c5;
loop
fetch c5 into a,b,c,d,e,f;
dbms_output.put_line(a||''||b||''||c||''||d||''||e||''||f||'');
exit when c5 %notfound;
end loop;
close c5;
end;
/
select * from stdnt;

SNO SNAME ADDRESS M1 M2 M3


----- ---------- ---------- ---------- ---------- ----------
1 kithu love 99 56 87
2 karthik sky 5 8 47
3 anakha cute 89 98 100
4 arjun handsome 19 96 77

Exception
declare
numerator number;
denominator number;
result number;
begin
numerator:=&numerator;
denominator:=&denominator;
result:=numerator/denominator;
dbms_output.put_line(result||'result');
exception
when zero_divide then

dbms_output.put_line('zero divide error');


end;
/
Enter value for numerator: 5
old 6: numerator:=&numerator;
new 6: numerator:=5
Enter value for denominator: 0
old 7: denominator:=&denominator;
new 7: denominator:=0;
zero divide error

PL/SQL procedure successfully completed.

create or replace procedure inser (id number,amt number) is


sal number;
sal_error exception;
begin
select salary into sal from EMPE where empid=id;
if sal is null then
raise sal_error;
else
update EMPE set salary=salary+amt where empid=id;
end if;
EXCEPTION
when sal_error then
dbms_output.put_line(id||'has salary as null');
when no_data_found then
dbms_output.put_line(id||' no id exist');
end inser;
/
exec inser(88,000);
88 no id exist

TRIGGER

create table pd_master(pno int primary key,description varchar(40),selling_price


int,cost_price int,ppr_per number(3,2));
insert into pd_master values(101,'galaxy',100,50,5.76);
insert into pd_master values(102,'icecream',50,20,8.50);
insert into pd_master values(103,'cake',70,40,6.86);

create or replace trigger t1 before update of ppr_per on pd_master


for each row
begin
if(:new.ppr_per>:old.ppr_per)then
dbms_output.put_line('profit percentage is updated');
else
:new.ppr_per:=:old.ppr_per;
dbms_output.put_line('cannot be updated');
end if;
end t1;
/
Trigger created.

update pd_master set ppr_per=7.91 where pno=101;


profit percentage is updated
update pd_master set ppr_per=3.03 where pno=102;
cannot be updated

PNO DESCRIPTION SELLING_PRICE COST_PRICE PPR_PER


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

101 galaxy 100 50 7.91

102 icecream 50 20 8.5

103 cake 70 40 6.86

create or replace trigger t2 before insert on pd_master


for each row
declare
maxppr number(3,2);
begin
select max(ppr_per)into maxppr from pd_master;
if(:new.ppr_per>maxppr)then
dbms_output.put_line('profit value can be inserted');
else
raise_application_error(-20000,'value cannot be inserted');
end if;
end t2;
/
Trigger created.

insert into pd_master values(104,'pie',25,10,9.00);


profit value can be inserted
insert into pd_master values(105,'lays',10,3,1.00);
value cannot be inserted

PNO DESCRIPTION SELLING_PRICE COST_PRICE PPR_PER


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

101 galaxy 100 50 7.91

102 icecream 50 20 8.5

103 cake 70 40 6.86

PNO DESCRIPTION SELLING_PRICE COST_PRICE PPR_PER


-------- ------------- ------------- ---------- --------
104 pie 25 10 9

You might also like