Anonymous Block
1.
declare
v_maxdep varchar2(10);
begin
select max(deptno) into v_maxdep from dept;
dbms_output.put_line('The max dept number is: '||v_maxdep);
end;
/
2.
declare
v_max dept.deptno%type;
begin
select Max(deptno)+1 into v_max from dept;
insert into dept values(v_max,'FINANCE',null);
end;
/
3.
ACCEPT v_loc varchar2(10) prompt 'Enter Location: '
declare
v_location varchar2(10):= ('&v_loc');
begin
update dept set loc=v_location where deptno=5;
dbms_output.put_line('Location has been updated with '||v_location||' succesfully');
commit;
end;
/
4.
begin
delete from dept where deptno=(select max(deptno) from DEPT);
dbms_output.put_line('The Finance dept has been deleted successfully.');
end;
/
5.
accept v_name varchar(20) prompt 'Enter name: '
DECLARE
v_ename emp.ename%type:=('&v_name');
v_basic dept.basic%type;
PF emp.sal%type;
HRA emp.sal%type;
DA emp.sal%type;
NetSal emp.sal%type;
begin
select basic into v_basic from dept where DEPTNO in (select deptno from emp where ename =upper(v_ename));
HRA:= v_basic * 0.31;
DA :=v_basic * 0.15;
if v_basic < 3000 then
pf:= v_basic * 0.05;
elsif v_basic between 3000 and 5000 then
pf:=v_basic * 0.07;
elsif v_basic between 5000 and 8000 then
pf := v_basic * 0.08;
end if;
netsal := v_basic+da+hra+pf;
dbms_output.put_line('The pf and net salary for '||v_ename||' is '||pf||' and '||netsal);
exception
when no_data_found then
dbms_output.put_line('There is no employee found with this name.!!');
end;
/
6.
declare
v_grade salgrade.grade%type;
cursor gradecur is select * from salgrade;
cursor empsal is select ename,sal from emp;
cur_grd gradecur%rowtype;
v_sal emp.sal%type;
v_name emp.ename%type;
begin
open empsal;
loop
fetch empsal into v_name,v_sal;
exit when empsal%notfound;
open gradecur;
loop fetch gradecur into cur_grd;
exit when gradecur%notfound;
if v_sal between cur_grd.lowsal and cur_grd.highsal then
v_grade:=cur_grd.grade;
if v_grade='A' then
dbms_output.put_line('The employee is Project manager');
elsif v_grade = 'B' then
dbms_output.put_line('The employee is Team lead');
else
dbms_output.put_line('The employee is engineer');
end if;
end if;
end loop;
close gradecur;
end loop;
close empsal;
end;
/
7.
accept v_empno emp.empno%type prompt 'Enter emplyee no: '
declare
v_empno emp.empno%type:= ('&v_empno');
v_sal emp.sal%type;
v_comm emp.comm%type;
v_bonus emp.sal%type;
begin
select comm into v_comm from emp where empno=v_empno;
if v_comm is null then
dbms_output.put_line('Employee doesn''t earn any commision.');
else
v_bonus:= v_comm *0.15;
v_sal := v_sal + v_bonus;
dbms_output.put_line(v_bonus||' bonus has been added to the salary and the current salary is: '||v_sal);
end if;
end;
/
8.
declare
v_empcnt number(3);
v_depname DEPT.DNAME%type;
v_avgsal emp.sal%type;
cursor c1 is select d.dname,count(e.empno) EmpCount,avg(e.sal) AvgSal from emp e join dept d on e.deptno=d.deptno group by d.dname;
begin
open c1;
loop
fetch c1 into v_depname,v_empcnt,v_avgsal;
exit when c1%notfound;
if v_empcnt is null then
dbms_output.put_line('No employees are working in this department.');
else
dbms_output.put_line(v_depname||' has '||v_empcnt||' employee(s) and the average salary is '||v_avgsal);
end if;
end loop;
end;
/
9.
accept v_empno emp.empno%tye prompt 'Enter employee No: '
declare
v_empno emp.empno%type :=('&v_empno');
v_empsal emp.sal%type;
v_avgsal emp.sal%type;
begin
select sal into v_empsal from emp where empno=v_empno;
select avg(sal) into v_avgsal from emp where deptno in (select deptno from emp where empno=v_empno);
if v_empsal > v_avgsal then
dbms_output.put_line('employee salary is more than the average salary');
else
dbms_output.put_line('employee salary is less than the average salary');
end if;
end;
/
Procedures and functions:
1.
create or replace procedure delempbyjob(p_deptno number) is
cnt number;
begin
delete from emp where deptno =p_deptno;
cnt:=sql%rowcount;
dbms_output.put_line(cnt||' employees have been deleted.');
end;
/
Execution:
exec delempbyjob(4);
2.
create or replace procedure delempbyjob(p_deptno number,empcnt out number) is
begin
delete from emp where deptno =p_deptno;
empcnt:=sql%rowcount;
end;
/
Execution:
declare
empcnt number;
begin
delempbyjob(4,empcnt);
dbms_output.put_line(empcnt||' employees have been deleted.');
end;
/
3.
create or replace function delempbydep(p_deptno number) return number is
cnt number;
PRAGMA AUTONOMOUS_TRANSACTION;
begin
delete from emp where deptno =p_deptno;
commit;
cnt:=sql%rowcount;
dbms_output.put_line(cnt||' employees have been deleted.');
return cnt;
end;
/
declare
cnt number;
begin
cnt:=delempbydep(4);
dbms_output.put_line(cnt);
end;
/
4.A.
create or replace procedure Withdraw(p_accno number,p_amt number) is
v_bal customers.balance%type;
begin
select balance into v_bal from customers where accno=p_accno;
if v_bal < p_amt then
raise_application_error(-20002,'There is not sufficient funds in your account.');
else
v_bal :=v_bal - p_amt;
update customers set balance = v_bal where accno=p_accno;
commit;
dbms_output.put_line(p_amt||' has been debited from your account.');
end if;
exception
when no_data_found then
raise_application_error(-20001,'Invalid account number.');
end;
end;
/
4.B.
create or replace procedure Deposit(p_accno number,p_amt number) is
v_bal customers.balance%type;
begin
select balance into v_bal from customers where accno=p_accno;
v_bal :=v_bal + p_amt;
update customers set balance = v_bal where accno=p_accno;
commit;
dbms_output.put_line(p_amt||' has been credited to your account.');
exception
when no_data_found then
raise_application_error(-20001,'Invalid account number.');
end;
/
4.C.
create or replace procedure Transfer(p_acc1 number,p_acc2 number,p_amt number) is
v_bal1 number;
v_bal2 number;
begin
select balance into v_bal1 from customers where accno=p_acc1;
select balance into v_bal2 from customers where accno=p_acc2;
if v_bal1 < p_amt then
raise_application_error(-20001,'There is not sufficient funds in your account.');
else
v_bal1 := v_bal1- p_amt;
v_bal2 := v_bal2 + p_amt;
update customers set balance = v_bal1 where accno = p_acc1;
update customers set balance = v_bal2 where accno = p_acc2;
commit;
dbms_output.put_line(p_amt||' has been transfered from '||p_acc1||' to '||p_acc2);
end if;
exception
when no_data_found then
raise_application_error(-20003,'Invalid account entered.');
end;
/
Cursors and Data Types as in 3GL:
1.
accept v_empno emp.empno%type prompt 'Enter employee no: '
declare
v_empno emp.empno%type:= ('&v_empno');
cursor c1 is select ename,hiredate,sal,mgr,deptno from emp where empno=v_empno;
emp_rec c1%rowtype;
begin
open c1;
fetch c1 into emp_rec;
insert into ret_emp values(v_empno,emp_rec.ename,emp_rec.hiredate,sysdate,emp_rec.sal,emp_rec.mgr,emp_rec.deptno);
close c1;
exception
when no_data_found then
raise_application_error(-20001,'Employee does not exist with this number.');
end;
/
2.
declare
cursor empsal is select sal from emp;
cursor empgrd is select * from empgrade;
v_sal emp.sal%type;
v_grade number(1);
v_grade1 number(1):=0;
v_grade2 number(1):=0;
v_grade3 number(1):=0;
cur_grd empgrd%rowtype;
begin
open empsal;
loop fetch empsal into v_sal;
exit when empsal%notfound;
open empgrd;
loop fetch empgrd into cur_grd;
exit when empgrd%notfound;
if v_sal between cur_grd.lowsal and cur_grd.highsal then
v_grade := cur_grd.grade;
if v_grade = 1 then
v_grade1 := v_grade1 + 1;
elsif v_grade = 2 then
v_grade2 := v_grade2 +1;
else
v_grade3 := v_grade3+1;
end if;
end if;
end loop;
close empgrd;
end loop;
close empsal;
dbms_output.put_line('No of employees with the grade 1 are '||v_grade1);
dbms_output.put_line('No of employees with the grade 2 are '||v_grade2);
dbms_output.put_line('No of employees with the grade 3 are '||v_grade3);
end;
/
Cursors:
1.
create or replace procedure PayIncrement is
cnt number(1);
begin
update emp set sal = sal + sal * 0.15 where deptno = 1;
cnt := sql%rowcount;
dbms_output.put_line(cnt||' employees have been awarded with 15% increment in thier salary.');
end;
/
2.
declare
cursor empcur is select ename,deptno,sal from emp;
cursor grdcur is select * from empgrade;
grd_rec grdcur%rowtype;
emp_rec empcur%rowtype;
v_grade number(1);
begin
open empcur;
loop fetch empcur into emp_rec;
exit when empcur%notfound;
open grdcur;
loop fetch grdcur into grd_rec;
exit when grdcur%notfound;
if emp_rec.sal between grd_rec.lowsal and grd_rec.highsal then
v_grade := grd_rec.grade;
if v_grade = 5 then
dbms_output.put_line(emp_rec.ename||' grade is '||grd_rec.grade||' minsal is '||grd_rec.lowsal||' maxsal is '||grd_rec.highsal);
end if;
end if;
end loop;
close grdcur;
end loop;
close empcur;
end;
/
3.
declare
suri varchar2(1000);
begin
suri:='create table old_dept as select * from dept';
execute immediate suri;
dbms_output.put_line(sql%rowcount||' rows were copied to the new table.');
end;
/
4.
declare
v_name emp.ename%type;
cursor c1 is select ename from emp where deptno = 3;
begin
open c1;
loop
fetch c1 into v_name;
exit when c1%notfound;
dbms_output.put_line(v_name||' is working for dept no 3.');
end loop;
end;
/
5.
declare
cursor c1 is select * from dept;
dept_cur c1%rowtype;
begin
for dept_cur in c1 loop
dbms_output.put_line(dept_cur.dname||' '||dept_cur.loc);
end loop;
end;
/
6.
declare
cursor c1 is select ename,sal,deptno from emp where rownum <=6 order by sal desc;
emp_rec c1%rowtype;
begin
for emp_rec in c1 loop
dbms_output.put_line(emp_rec.ename||' '||emp_rec.sal||' '||emp_rec.deptno);
end loop;
end;
/
7.
declare
cursor c1 is select deptno,dname from dept;
v_deptno dept.deptno%type;
v_dname dept.dname%type;
begin
open c1;
loop
fetch c1 into v_deptno,v_dname ;
exit when c1%notfound;
dbms_output.put_line('The list of employees in department '||v_deptno||' is as follows:');
declare
emp_rec emp%rowtype;
cursor c2(v_deptno number) is select * from emp where deptno = v_deptno;
begin
open c2(v_deptno);
loop
fetch c2 into emp_rec;
exit when c2%notfound;
dbms_output.Put_line(emp_rec.ename||' '||emp_rec.job||' '||emp_rec.hiredate||' '||emp_rec.sal);
end loop;
close c2;
end;
end loop;
close c1;
end;
/
8.
create or replace procedure Raise_Sal(p_depno number,increment_pct number) is
cursor c1 is select * from emp where deptno=p_depno for update of sal;
emp_rec c1%rowtype;
begin
open c1;
loop
fetch c1 into emp_rec;
exit when c1%notfound;
update emp set sal = sal + sal*increment_pct where current of c1;
end loop;
close c1;
end;
/
Exception Handling:
1.
declare
v_sal emp.sal%type := &v_sal;
v_cnt number(1):=0;
no_emp exception;
more_emp exception;
begin
select count(*) into v_cnt from emp where sal = v_sal;
if v_cnt = 0 then
raise no_emp;
elsif v_cnt = 1 then
insert into sal_msg values(null,v_sal,'there is an employee does exist');
else raise more_emp;
end if;
exception
when no_emp then
insert into sal_msg values(null,v_sal,'There are no employees found with this salary.');
when more_emp then
insert into sal_msg values(null,v_sal,'More than one employee found.');
when others then
insert into sal_msg values(null,v_sal,'Unknown error occured.');
commit;
end;
/
2.
declare
cnt number:=0;
employee_exist exception;
v_depno emp.deptno%type:= &deptno;
pragma exception_init (employee_exist,-20001);
begin
select count(*) into cnt from emp where deptno = v_depno;
if cnt = 0 then
delete from dept where deptno=v_depno;
dbms_output.put_line(v_depno||' department has been removed from the departments table.');
else
raise employee_exist;
end if;
exception
when employee_exist then
dbms_output.put_line('Empliyee(s) exist in this department that you are trying to delete.');
end;
/
3.
accept v_sal emp.sal%type prompt 'Enter Salary: '
accept v_depno emp.deptno%type prompt 'Enter deptno: '
declare
v_sal emp.sal%type :=('&v_sal');
v_deptno emp.deptno%type:=('&v_depno');
v_minsal emp.sal%type := 800;
v_maxsal emp.sal%type := 5000;
unknown_dept exception;
salrange_exceed exception;
pragma exception_init(unknown_dept,-22091);
cnt number(1);
begin
select count(*) into cnt from emp e join dept d on e.deptno=d.deptno where d.deptno = v_deptno;
if cnt = 0 then
raise unknown_dept;
else
if v_sal between v_minsal and v_maxsal then
update emp set sal = v_sal where deptno = v_deptno;
commit;
else raise salrange_exceed;
end if;
end if;
exception
when salrange_exceed then
dbms_output.put_line('The specified salary is not in the given range.');
when unknown_dept then
dbms_output.put_line('The given dept doesn''t exist in the dept table.');
when others then
dbms_output.put_line('Unknown error occured.');
end;
/
Packages:
1.
create or replace
package Dep_Store as
procedure CreateNewItem(p_item_no varchar2,p_desc varchar2);
procedure PromoteItem(p_item number,p_cost number);
procedure RemoveItem(p_item number);
procedure ChangeItemCost(p_item number,p_cost number);
end;
create or replace
package body Dep_Store as
procedure CreateNewItem(p_item_no varchar2,p_desc varchar2) is
begin
insert into Items(Item_id,Item_number,Description,Status,Cost) values(ITEMS_ITEM_ID_S.nextval,p_item_no,p_desc,'t',0);
commit;
end;
procedure PromoteItem(p_item number,p_cost number) is
v_cost items.cost%type;
v_status items.status%type;
begin
select cost,status into v_cost,v_status from items where item_id = p_item;
if v_status = 't' then
if p_cost <= 0 or p_cost is null then
raise_application_error(-20003,'Cost cannot be Zero or null');
else
update items set cost = p_cost,status = 'l' where item_id=p_item;
end if;
end if;
commit;
end;
procedure RemoveItem(p_item number) is
v_status items.status%type;
v_cost items.cost%type;
begin
select status,cost into v_status,v_cost from items where item_id = p_item;
if v_status = 't' and v_cost = 0 then
delete from items where item_id=p_item;
else
raise_application_error(-20003,'Item cannot be deleted because either it''s status is live or cost is non-zero.');
end if;
end;
procedure ChangeItemCost(p_item number,p_cost number) is
v_status items.status%type;
begin
select status into v_status from items where item_id = p_item;
if v_status = 't' then
raise_application_error(-20001,'Cost cannot be assigned to a test item.');
else
update items set cost = p_cost where item_id= p_item;
end if;
commit;
end;
end;
Triggers:
1.
create or replace trigger after_delete_item
after delete on items
for each row
begin
insert into items_archive values(:old.item_id,:old.item_number,:old.description,:old.status,:old.cost);
end;
/
create or replace trigger after_update_cost
after update on items
for each row
begin
insert into auditing_cost values(:old.item_id,:old.cost,:new.cost);
end;
/
2.
create or replace trigger after_delete_manager
after delete on emp
for each row
begin
update emp set mgr=null where mgr not in (select empno from emp);
end;
/
3.
create or replace trigger biu_newemployee
before insert or update on emp
for each row
declare
v_minsal job_salary.minsal%type;
v_maxsal job_salary.maxsal%type;
v_Newsal emp.sal%type;
v_oldSal emp.sal%type;
v_hike number(5,2) := 10;
cursor c1 is select minsal,maxsal from job_salary where job= :new.job;
begin
open c1;
loop
fetch c1 into v_minsal,v_maxsal;
exit when c1%notfound;
if inserting then
if :new.sal < v_minsal or :new.sal > v_maxsal then
raise_application_error(-20001,'The given salary is not in the salary range of this job.');
end if;
end if;
v_oldsal := :old.sal;
v_newsal := :new.sal;
v_hike := (v_newsal-v_oldsal)/v_oldsal * 100;
if updating then
if v_hike >10 then
raise_application_error(-20005,'Salary hike of more than 10 % is not allowed!');
elsif :new.sal < v_minsal or :new.sal > v_maxsal then
raise_application_error(-20002,'The change in the salary is not lies between the specified range for this job.');
end if;
end if;
end loop;
close c1;
end;
/
4.
create or replace
trigger bdu_job_salary
before delete or update on job_salary
for each row
declare
v_cnt number;
begin
select count(*) into v_cnt from emp where job = :old.job;
if v_cnt >= 1 then
raise_application_error(-20013,v_cnt||' '||'Employees exist in this job category hence cannot be deleted.');
end if;
end;
Happy Coding...!!!!!
1.
declare
v_maxdep varchar2(10);
begin
select max(deptno) into v_maxdep from dept;
dbms_output.put_line('The max dept number is: '||v_maxdep);
end;
/
2.
declare
v_max dept.deptno%type;
begin
select Max(deptno)+1 into v_max from dept;
insert into dept values(v_max,'FINANCE',null);
end;
/
3.
ACCEPT v_loc varchar2(10) prompt 'Enter Location: '
declare
v_location varchar2(10):= ('&v_loc');
begin
update dept set loc=v_location where deptno=5;
dbms_output.put_line('Location has been updated with '||v_location||' succesfully');
commit;
end;
/
4.
begin
delete from dept where deptno=(select max(deptno) from DEPT);
dbms_output.put_line('The Finance dept has been deleted successfully.');
end;
/
5.
accept v_name varchar(20) prompt 'Enter name: '
DECLARE
v_ename emp.ename%type:=('&v_name');
v_basic dept.basic%type;
PF emp.sal%type;
HRA emp.sal%type;
DA emp.sal%type;
NetSal emp.sal%type;
begin
select basic into v_basic from dept where DEPTNO in (select deptno from emp where ename =upper(v_ename));
HRA:= v_basic * 0.31;
DA :=v_basic * 0.15;
if v_basic < 3000 then
pf:= v_basic * 0.05;
elsif v_basic between 3000 and 5000 then
pf:=v_basic * 0.07;
elsif v_basic between 5000 and 8000 then
pf := v_basic * 0.08;
end if;
netsal := v_basic+da+hra+pf;
dbms_output.put_line('The pf and net salary for '||v_ename||' is '||pf||' and '||netsal);
exception
when no_data_found then
dbms_output.put_line('There is no employee found with this name.!!');
end;
/
6.
declare
v_grade salgrade.grade%type;
cursor gradecur is select * from salgrade;
cursor empsal is select ename,sal from emp;
cur_grd gradecur%rowtype;
v_sal emp.sal%type;
v_name emp.ename%type;
begin
open empsal;
loop
fetch empsal into v_name,v_sal;
exit when empsal%notfound;
open gradecur;
loop fetch gradecur into cur_grd;
exit when gradecur%notfound;
if v_sal between cur_grd.lowsal and cur_grd.highsal then
v_grade:=cur_grd.grade;
if v_grade='A' then
dbms_output.put_line('The employee is Project manager');
elsif v_grade = 'B' then
dbms_output.put_line('The employee is Team lead');
else
dbms_output.put_line('The employee is engineer');
end if;
end if;
end loop;
close gradecur;
end loop;
close empsal;
end;
/
7.
accept v_empno emp.empno%type prompt 'Enter emplyee no: '
declare
v_empno emp.empno%type:= ('&v_empno');
v_sal emp.sal%type;
v_comm emp.comm%type;
v_bonus emp.sal%type;
begin
select comm into v_comm from emp where empno=v_empno;
if v_comm is null then
dbms_output.put_line('Employee doesn''t earn any commision.');
else
v_bonus:= v_comm *0.15;
v_sal := v_sal + v_bonus;
dbms_output.put_line(v_bonus||' bonus has been added to the salary and the current salary is: '||v_sal);
end if;
end;
/
8.
declare
v_empcnt number(3);
v_depname DEPT.DNAME%type;
v_avgsal emp.sal%type;
cursor c1 is select d.dname,count(e.empno) EmpCount,avg(e.sal) AvgSal from emp e join dept d on e.deptno=d.deptno group by d.dname;
begin
open c1;
loop
fetch c1 into v_depname,v_empcnt,v_avgsal;
exit when c1%notfound;
if v_empcnt is null then
dbms_output.put_line('No employees are working in this department.');
else
dbms_output.put_line(v_depname||' has '||v_empcnt||' employee(s) and the average salary is '||v_avgsal);
end if;
end loop;
end;
/
9.
accept v_empno emp.empno%tye prompt 'Enter employee No: '
declare
v_empno emp.empno%type :=('&v_empno');
v_empsal emp.sal%type;
v_avgsal emp.sal%type;
begin
select sal into v_empsal from emp where empno=v_empno;
select avg(sal) into v_avgsal from emp where deptno in (select deptno from emp where empno=v_empno);
if v_empsal > v_avgsal then
dbms_output.put_line('employee salary is more than the average salary');
else
dbms_output.put_line('employee salary is less than the average salary');
end if;
end;
/
Procedures and functions:
1.
create or replace procedure delempbyjob(p_deptno number) is
cnt number;
begin
delete from emp where deptno =p_deptno;
cnt:=sql%rowcount;
dbms_output.put_line(cnt||' employees have been deleted.');
end;
/
Execution:
exec delempbyjob(4);
2.
create or replace procedure delempbyjob(p_deptno number,empcnt out number) is
begin
delete from emp where deptno =p_deptno;
empcnt:=sql%rowcount;
end;
/
Execution:
declare
empcnt number;
begin
delempbyjob(4,empcnt);
dbms_output.put_line(empcnt||' employees have been deleted.');
end;
/
3.
create or replace function delempbydep(p_deptno number) return number is
cnt number;
PRAGMA AUTONOMOUS_TRANSACTION;
begin
delete from emp where deptno =p_deptno;
commit;
cnt:=sql%rowcount;
dbms_output.put_line(cnt||' employees have been deleted.');
return cnt;
end;
/
declare
cnt number;
begin
cnt:=delempbydep(4);
dbms_output.put_line(cnt);
end;
/
4.A.
create or replace procedure Withdraw(p_accno number,p_amt number) is
v_bal customers.balance%type;
begin
select balance into v_bal from customers where accno=p_accno;
if v_bal < p_amt then
raise_application_error(-20002,'There is not sufficient funds in your account.');
else
v_bal :=v_bal - p_amt;
update customers set balance = v_bal where accno=p_accno;
commit;
dbms_output.put_line(p_amt||' has been debited from your account.');
end if;
exception
when no_data_found then
raise_application_error(-20001,'Invalid account number.');
end;
end;
/
4.B.
create or replace procedure Deposit(p_accno number,p_amt number) is
v_bal customers.balance%type;
begin
select balance into v_bal from customers where accno=p_accno;
v_bal :=v_bal + p_amt;
update customers set balance = v_bal where accno=p_accno;
commit;
dbms_output.put_line(p_amt||' has been credited to your account.');
exception
when no_data_found then
raise_application_error(-20001,'Invalid account number.');
end;
/
4.C.
create or replace procedure Transfer(p_acc1 number,p_acc2 number,p_amt number) is
v_bal1 number;
v_bal2 number;
begin
select balance into v_bal1 from customers where accno=p_acc1;
select balance into v_bal2 from customers where accno=p_acc2;
if v_bal1 < p_amt then
raise_application_error(-20001,'There is not sufficient funds in your account.');
else
v_bal1 := v_bal1- p_amt;
v_bal2 := v_bal2 + p_amt;
update customers set balance = v_bal1 where accno = p_acc1;
update customers set balance = v_bal2 where accno = p_acc2;
commit;
dbms_output.put_line(p_amt||' has been transfered from '||p_acc1||' to '||p_acc2);
end if;
exception
when no_data_found then
raise_application_error(-20003,'Invalid account entered.');
end;
/
Cursors and Data Types as in 3GL:
1.
accept v_empno emp.empno%type prompt 'Enter employee no: '
declare
v_empno emp.empno%type:= ('&v_empno');
cursor c1 is select ename,hiredate,sal,mgr,deptno from emp where empno=v_empno;
emp_rec c1%rowtype;
begin
open c1;
fetch c1 into emp_rec;
insert into ret_emp values(v_empno,emp_rec.ename,emp_rec.hiredate,sysdate,emp_rec.sal,emp_rec.mgr,emp_rec.deptno);
close c1;
exception
when no_data_found then
raise_application_error(-20001,'Employee does not exist with this number.');
end;
/
2.
declare
cursor empsal is select sal from emp;
cursor empgrd is select * from empgrade;
v_sal emp.sal%type;
v_grade number(1);
v_grade1 number(1):=0;
v_grade2 number(1):=0;
v_grade3 number(1):=0;
cur_grd empgrd%rowtype;
begin
open empsal;
loop fetch empsal into v_sal;
exit when empsal%notfound;
open empgrd;
loop fetch empgrd into cur_grd;
exit when empgrd%notfound;
if v_sal between cur_grd.lowsal and cur_grd.highsal then
v_grade := cur_grd.grade;
if v_grade = 1 then
v_grade1 := v_grade1 + 1;
elsif v_grade = 2 then
v_grade2 := v_grade2 +1;
else
v_grade3 := v_grade3+1;
end if;
end if;
end loop;
close empgrd;
end loop;
close empsal;
dbms_output.put_line('No of employees with the grade 1 are '||v_grade1);
dbms_output.put_line('No of employees with the grade 2 are '||v_grade2);
dbms_output.put_line('No of employees with the grade 3 are '||v_grade3);
end;
/
Cursors:
1.
create or replace procedure PayIncrement is
cnt number(1);
begin
update emp set sal = sal + sal * 0.15 where deptno = 1;
cnt := sql%rowcount;
dbms_output.put_line(cnt||' employees have been awarded with 15% increment in thier salary.');
end;
/
2.
declare
cursor empcur is select ename,deptno,sal from emp;
cursor grdcur is select * from empgrade;
grd_rec grdcur%rowtype;
emp_rec empcur%rowtype;
v_grade number(1);
begin
open empcur;
loop fetch empcur into emp_rec;
exit when empcur%notfound;
open grdcur;
loop fetch grdcur into grd_rec;
exit when grdcur%notfound;
if emp_rec.sal between grd_rec.lowsal and grd_rec.highsal then
v_grade := grd_rec.grade;
if v_grade = 5 then
dbms_output.put_line(emp_rec.ename||' grade is '||grd_rec.grade||' minsal is '||grd_rec.lowsal||' maxsal is '||grd_rec.highsal);
end if;
end if;
end loop;
close grdcur;
end loop;
close empcur;
end;
/
3.
declare
suri varchar2(1000);
begin
suri:='create table old_dept as select * from dept';
execute immediate suri;
dbms_output.put_line(sql%rowcount||' rows were copied to the new table.');
end;
/
4.
declare
v_name emp.ename%type;
cursor c1 is select ename from emp where deptno = 3;
begin
open c1;
loop
fetch c1 into v_name;
exit when c1%notfound;
dbms_output.put_line(v_name||' is working for dept no 3.');
end loop;
end;
/
5.
declare
cursor c1 is select * from dept;
dept_cur c1%rowtype;
begin
for dept_cur in c1 loop
dbms_output.put_line(dept_cur.dname||' '||dept_cur.loc);
end loop;
end;
/
6.
declare
cursor c1 is select ename,sal,deptno from emp where rownum <=6 order by sal desc;
emp_rec c1%rowtype;
begin
for emp_rec in c1 loop
dbms_output.put_line(emp_rec.ename||' '||emp_rec.sal||' '||emp_rec.deptno);
end loop;
end;
/
7.
declare
cursor c1 is select deptno,dname from dept;
v_deptno dept.deptno%type;
v_dname dept.dname%type;
begin
open c1;
loop
fetch c1 into v_deptno,v_dname ;
exit when c1%notfound;
dbms_output.put_line('The list of employees in department '||v_deptno||' is as follows:');
declare
emp_rec emp%rowtype;
cursor c2(v_deptno number) is select * from emp where deptno = v_deptno;
begin
open c2(v_deptno);
loop
fetch c2 into emp_rec;
exit when c2%notfound;
dbms_output.Put_line(emp_rec.ename||' '||emp_rec.job||' '||emp_rec.hiredate||' '||emp_rec.sal);
end loop;
close c2;
end;
end loop;
close c1;
end;
/
8.
create or replace procedure Raise_Sal(p_depno number,increment_pct number) is
cursor c1 is select * from emp where deptno=p_depno for update of sal;
emp_rec c1%rowtype;
begin
open c1;
loop
fetch c1 into emp_rec;
exit when c1%notfound;
update emp set sal = sal + sal*increment_pct where current of c1;
end loop;
close c1;
end;
/
Exception Handling:
1.
declare
v_sal emp.sal%type := &v_sal;
v_cnt number(1):=0;
no_emp exception;
more_emp exception;
begin
select count(*) into v_cnt from emp where sal = v_sal;
if v_cnt = 0 then
raise no_emp;
elsif v_cnt = 1 then
insert into sal_msg values(null,v_sal,'there is an employee does exist');
else raise more_emp;
end if;
exception
when no_emp then
insert into sal_msg values(null,v_sal,'There are no employees found with this salary.');
when more_emp then
insert into sal_msg values(null,v_sal,'More than one employee found.');
when others then
insert into sal_msg values(null,v_sal,'Unknown error occured.');
commit;
end;
/
2.
declare
cnt number:=0;
employee_exist exception;
v_depno emp.deptno%type:= &deptno;
pragma exception_init (employee_exist,-20001);
begin
select count(*) into cnt from emp where deptno = v_depno;
if cnt = 0 then
delete from dept where deptno=v_depno;
dbms_output.put_line(v_depno||' department has been removed from the departments table.');
else
raise employee_exist;
end if;
exception
when employee_exist then
dbms_output.put_line('Empliyee(s) exist in this department that you are trying to delete.');
end;
/
3.
accept v_sal emp.sal%type prompt 'Enter Salary: '
accept v_depno emp.deptno%type prompt 'Enter deptno: '
declare
v_sal emp.sal%type :=('&v_sal');
v_deptno emp.deptno%type:=('&v_depno');
v_minsal emp.sal%type := 800;
v_maxsal emp.sal%type := 5000;
unknown_dept exception;
salrange_exceed exception;
pragma exception_init(unknown_dept,-22091);
cnt number(1);
begin
select count(*) into cnt from emp e join dept d on e.deptno=d.deptno where d.deptno = v_deptno;
if cnt = 0 then
raise unknown_dept;
else
if v_sal between v_minsal and v_maxsal then
update emp set sal = v_sal where deptno = v_deptno;
commit;
else raise salrange_exceed;
end if;
end if;
exception
when salrange_exceed then
dbms_output.put_line('The specified salary is not in the given range.');
when unknown_dept then
dbms_output.put_line('The given dept doesn''t exist in the dept table.');
when others then
dbms_output.put_line('Unknown error occured.');
end;
/
Packages:
1.
create or replace
package Dep_Store as
procedure CreateNewItem(p_item_no varchar2,p_desc varchar2);
procedure PromoteItem(p_item number,p_cost number);
procedure RemoveItem(p_item number);
procedure ChangeItemCost(p_item number,p_cost number);
end;
create or replace
package body Dep_Store as
procedure CreateNewItem(p_item_no varchar2,p_desc varchar2) is
begin
insert into Items(Item_id,Item_number,Description,Status,Cost) values(ITEMS_ITEM_ID_S.nextval,p_item_no,p_desc,'t',0);
commit;
end;
procedure PromoteItem(p_item number,p_cost number) is
v_cost items.cost%type;
v_status items.status%type;
begin
select cost,status into v_cost,v_status from items where item_id = p_item;
if v_status = 't' then
if p_cost <= 0 or p_cost is null then
raise_application_error(-20003,'Cost cannot be Zero or null');
else
update items set cost = p_cost,status = 'l' where item_id=p_item;
end if;
end if;
commit;
end;
procedure RemoveItem(p_item number) is
v_status items.status%type;
v_cost items.cost%type;
begin
select status,cost into v_status,v_cost from items where item_id = p_item;
if v_status = 't' and v_cost = 0 then
delete from items where item_id=p_item;
else
raise_application_error(-20003,'Item cannot be deleted because either it''s status is live or cost is non-zero.');
end if;
end;
procedure ChangeItemCost(p_item number,p_cost number) is
v_status items.status%type;
begin
select status into v_status from items where item_id = p_item;
if v_status = 't' then
raise_application_error(-20001,'Cost cannot be assigned to a test item.');
else
update items set cost = p_cost where item_id= p_item;
end if;
commit;
end;
end;
Triggers:
1.
create or replace trigger after_delete_item
after delete on items
for each row
begin
insert into items_archive values(:old.item_id,:old.item_number,:old.description,:old.status,:old.cost);
end;
/
create or replace trigger after_update_cost
after update on items
for each row
begin
insert into auditing_cost values(:old.item_id,:old.cost,:new.cost);
end;
/
2.
create or replace trigger after_delete_manager
after delete on emp
for each row
begin
update emp set mgr=null where mgr not in (select empno from emp);
end;
/
3.
create or replace trigger biu_newemployee
before insert or update on emp
for each row
declare
v_minsal job_salary.minsal%type;
v_maxsal job_salary.maxsal%type;
v_Newsal emp.sal%type;
v_oldSal emp.sal%type;
v_hike number(5,2) := 10;
cursor c1 is select minsal,maxsal from job_salary where job= :new.job;
begin
open c1;
loop
fetch c1 into v_minsal,v_maxsal;
exit when c1%notfound;
if inserting then
if :new.sal < v_minsal or :new.sal > v_maxsal then
raise_application_error(-20001,'The given salary is not in the salary range of this job.');
end if;
end if;
v_oldsal := :old.sal;
v_newsal := :new.sal;
v_hike := (v_newsal-v_oldsal)/v_oldsal * 100;
if updating then
if v_hike >10 then
raise_application_error(-20005,'Salary hike of more than 10 % is not allowed!');
elsif :new.sal < v_minsal or :new.sal > v_maxsal then
raise_application_error(-20002,'The change in the salary is not lies between the specified range for this job.');
end if;
end if;
end loop;
close c1;
end;
/
4.
create or replace
trigger bdu_job_salary
before delete or update on job_salary
for each row
declare
v_cnt number;
begin
select count(*) into v_cnt from emp where job = :old.job;
if v_cnt >= 1 then
raise_application_error(-20013,v_cnt||' '||'Employees exist in this job category hence cannot be deleted.');
end if;
end;
Happy Coding...!!!!!