0% found this document useful (0 votes)
9 views

plsql assignments1-4

The document contains various PL/SQL code snippets demonstrating different functionalities such as updating employee salaries based on experience, checking employee existence, iterating through employee records, and managing transactions with a package. It also includes procedures for inserting employee data, determining credit eligibility, and handling overloaded functions. Overall, the document showcases a range of database operations and PL/SQL programming techniques.

Uploaded by

Sumanth
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)
9 views

plsql assignments1-4

The document contains various PL/SQL code snippets demonstrating different functionalities such as updating employee salaries based on experience, checking employee existence, iterating through employee records, and managing transactions with a package. It also includes procedures for inserting employee data, determining credit eligibility, and handling overloaded functions. Overall, the document showcases a range of database operations and PL/SQL programming techniques.

Uploaded by

Sumanth
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/ 7

------------1----------------

declare
v_exp number;
v_sal number;
new_sal number;
cursor cur_v is select empno,ename,job,hiredate,sal from emp;
begin
for i in cur_v
loop
v_exp := months_between(sysdate,i.hiredate)/12;
--dbms_output.put_line(v_exp);
if v_exp < 2
then new_sal := i.sal + 0.15*i.sal;
elsif v_exp >2 and v_exp<4
then new_sal := i.sal + 0.25*i.sal;
elsif v_exp > 4
then new_sal := i.sal + 0.35*i.sal;
end if;
dbms_output.put_line(i.ename||' '||i.job||' '||i.sal||' '||new_sal);
update emp set sal = new_sal where empno = i.empno;
end loop;
end;

-----------3-------------

1 declare
2 grade char;
3 nam emp.ename%type;
4 salary emp.sal%type;
5 begin
6 select ename,sal into nam,salary from emp where empno = 7839;
7 case
8 when salary>25000 then grade := 'a';
9 when salary>15000 then grade := 'b';
10 else grade := 'c';
11 dbms_output.put_line(nam||' '||salary||' '||grade);
12 end case;
13* end;
SQL> /
KING 5000 c

PL/SQL procedure successfully completed.

--------------2---------------

1 declare
2 emid emp.empno%type :=&id;
3 eid number;
4 begin
5 select empno into eid from emp where empno=emid;
6 if eid = emid then dbms_output.put_line('emp id found');
7 else dbms_output.put_line('emp id not found');
8 end if;
9* end;

--------------4----------------
declare
cursor c_emp is select * from emp where sal>2000;
v_emp c_emp%rowtype;
begin
open c_emp;
loop
fetch c_emp into v_emp;
exit when c_emp %notfound;
dbms_output.put_line(v_emp.ename||' '||v_emp.sal);
end loop;
close c_emp;
end;

-----------------5---------------

declare
cur_var sys_refcursor;
dno departments.department_id%type;
dname departments.department_name%type;

begin
dbms_output.put_line('dept no less than 40');
open cur_var for select department_id,department_name from departments where
department_id<40;
loop
fetch cur_var into dno,dname;
exit when cur_var%notfound;
dbms_output.put_line(dno||' '||dname);
end loop;
close cur_var;

dbms_output.put_line('

');

dbms_output.put_line('working as Human Resource');

open cur_var for select department_id,department_name from departments where


department_name = 'Human Resources';
loop
fetch cur_var into dno,dname;
exit when cur_var%notfound;

dbms_output.put_line(dno||' '||dname);
end loop;
close cur_var;
end;

-----------------6-----------------

declare
v_ename employees.first_name%type;
v_ejob employees.job_id%type;
cursor emp_cursor is select first_name,job_id from employees;
begin
open emp_cursor;
loop
fetch emp_cursor into v_ename,v_ejob;
exit when emp_cursor%rowcount>5;
dbms_output.put_line(v_ename||' '||v_ejob);
end loop;
close emp_cursor;
end;

----------------7-----------------

create or replace procedure top_earner(n number)


is
cursor cur_top_earner is select ename,sal from(select distinct sal,ename from emp
order by sal desc ) where rownum<n+1;
begin
for i in cur_top_earner
loop
dbms_output.put_line(i.ename||i.sal);
end loop;
end;

begin
top_earner(5);
end;

KING 5000
FORD 3000
SCOTT 3000
JONES 2975
BLAKE 2850

---------------8---------------

create or replace procedure empl(nam employee.name%type,i_d employee.id%type,loc


employee.location%type)
is
begin
insert into employee values(nam,i_d,loc);
end empl;

---------------9---------------

create or replace procedure cred_elig_status


is
status char ;
cursor cur_cred is select my_name,max_cred_money,used_cred_money from credits;
begin
for i in cur_cred
loop
case
when i.max_cred_money > i.used_cred_money then status := 'y';
else status := 'n';
end case;
exit when cur_cred%notfound;
update credits set eligibility = status where my_name = i.my_name;
end loop;
end;

----------------or-----------

create or replace procedure cred_elig_status


is
status char ;
cursor cur_cred is select my_name,max_cred_money,used_cred_money from credits;
begin
for i in cur_cred
loop
if i.max_cred_money > i.used_cred_money then status := 'y';
else status := 'n';
end if;
exit when cur_cred%notfound;
update credits set eligibility = status where my_name = i.my_name;
end loop;
end;

begin
cred_elig_status;
end;

select * from credits;

MY_NAME ID_NUM MAX_CRED_MONEY USED_CRED_MONEY ELIGIBILITY


uma 2 100000 80000 y
sumanth 1 100000 90000 y
soumi 3 100000 120000 n

10

----------------11-----------------

create or replace function sal_hike(emp_id emp.employee_id%type)


return number
is
salary emp.salary%type;
begin
update emp set salary = (salary+(0.15*salary));
return salary;
end;

------------------12-----------------
create or replace function check_dept(dept_num number)
return boolean
is
eid dept.deptno%type;
begin
select count(*) into eid from dept where deptno=dept_num;
if eid =0 then return false;
else return true;
end if;
end;
create or replace procedure my_emp(emp_no emp.empno%type,emp_name emp.ename%type,
work_type emp.job%type,m_num emp.mgr%type,joining_date emp.hiredate%type,
salary emp.sal%type,commission emp.comm%type,dept_no emp.deptno%type)
is
value boolean;
begin
value := check_dept(dept_no);
if value = false then dbms_output.put_line('no such dept');
else
insert into emp
values(emp_no,emp_name,work_type,m_num,joining_date,salary,commission,dept_no);
dbms_output.put_line('insertion of date completed');
end if;
end;

begin
my_emp(5680,'sowmya','hr',7839,'10-07-2001',5000,1000,40);
end;

------------------13----------------

create or replace procedure proc_auth


is
cursor cur_auth is select fname,lname,total_books from author;
begin
for i in cur_auth
loop
dbms_output.put_line(i.total_books||' books by '||i.lname||', '||i.fname||'
ordered');
end loop;
end;

begin
proc_auth;
end;

----------------------14--------------------

-----------------table creation ----------------

create table transactions(acount_num number(10),


transaction_type varchar2(20),
balance number,
date_of_transaction date);

insert into transactions values(1234567890,'debit',1000,sysdate);

insert into transactions values(0123456789,'credit',1000,sysdate);


insert into transactions values(9012345678,'debit',400,sysdate);

-------package sequence---------

create or replace package trans_pkg


is
function check_balance(ac_num transactions.acount_num%type) return number;
procedure proc_trans(ac_num transactions.acount_num%type,
t_type transactions.transaction_type%type,
transac_amount transactions.transaction_amount%type);
end trans_pkg;

------------package body-------------

create or replace package body trans_pkg


is

------function implementation

function check_balance(ac_num transactions.acount_num%type)


return number
is
money number;
begin
select balance into money from transactions where acount_num=ac_num;
return money;
end check_balance;

--------procedure implementation

procedure proc_trans(ac_num transactions.acount_num%type,


t_type transactions.transaction_type%type,
transac_amount transactions.transaction_amount%type)
is
insufficient_blnc exception;
bal number;
begin
bal := check_balance(ac_num);

if t_type = 'credit' then


update transactions set balance = balance + transac_amount where acount_num=ac_num;
elsif t_type = 'debit' then
if bal >=500
then
update transactions set balance = balance - transac_amount where
acount_num=ac_num;

else raise insufficient_blnc;


end if;
else dbms_output.put_line('invalid transaction');
end if;
exception
when insufficient_blnc
then
dbms_output.put_line('insufficient blnc');
end proc_trans;
end trans_pkg;
begin
dbms_output.put_line(trans_pkg.check_balance(0123456789));
end;

begin
trans_pkg.proc_trans(1234567890,'debit',500);
end;

----------------------15----------------------

create or replace package over_load


as
function print_it(tdate date) return varchar2;
function print_it(nstring varchar2) return number;
end over_load;

create or replace package body over_load


as
--------function for date------

function print_it(tdate date) return varchar2


is
v_str varchar2(20);
begin
v_str := to_char(to_date(tdate,'dd-mm-yyyy'),'fmmonth,dd yyyy');
return v_str;
end print_it;

-------------function for number------

function print_it(nstring varchar2) return number


is
v_num number;
begin
v_num := to_number(nstring,'9999,999.00');
return v_num;
end print_it;
end over_load;

select over_load.print_it('864,725.99') from dual;

select over_load.print_it(to_date('10-07-2001')) from dual;

You might also like