You are on page 1of 7

EX NO:7

PROCEDURES , FUNCTIONS AND

CURSORS

03-08-2011

AIM:
To create the tables and answer the given queries using Procedures,functions and cursors

SQL:
Tables are created and values are inserted.

Student table:
SQL> select * from s_details; ROLL_NO NAME ADDR STAFF_IN_ID

---------- ---------- ---------- -------------------------------------1 2 3 hari banu rasi poy cbe cbe 10 11 12

Admission details table:


SQL> select * from a_details; ROLL_NO NAME DEPT BATCH CATEGORY FEES ACCOMOD

---------- ---------- ---------- ---------- ---------- ---------- ------------------------------------------------A_DATE --------1 27-JUL-09 2 27-JUL-09 banu it 2009 general 15000 hostel hari it 2009 general 15000 hostel

3 29-APR-08

rasi

cs

2008

general

15000 day_s

Staff details table:


SQL> select * from staff_details; ROLL_NO STAFF_IN_ID STAFF_NAME DEPT CONTACT_NO CATEGORY DESIG ---------- ----------- ---------- ---------- ---------- ---------- ------------------------------------------------JOIN_DATE --------- ---------1 12-JUL-09 2 11-JAN-00 3 10-JUL-90 10 50000 11 15000 12 25000 meera ece 963258741 nonteach attender sangeetha cse 852963147 teach nonteach priya it 147852963 teach prof SALARY

Hostel details table:


SQL> select * from hos_details;

ROOM_NO

ROLL_NO

P_NAME

ROOM_TYPE CONTACT_NO

----------------- ---------------- ------------ -------------------- ----------------------101 102 103 1 2 3 selvi umapathy poongodi ac nonac ac 369852147 963258741 965478123

1. Create a stored procedure to display the staff name when student name and roll number given as input: SQL> create or replace procedure p2(id number,bonus number) is 2 3 4 5 6 7 n1 number; begin select (salary+(salary*bonus/100)) into n1 from staff_details where staff_in_id=id; dbms_output.put_line('Salary with bonus'||''||n1); end; /

Procedure created. SQL> set serveroutput on; SQL> call p2(10,25); Salary with bonus62500 Call completed. 2. create a stored procedure to calculate the bonus amount when staff id and bonus percentage is given as input: SQL> create or replace procedure p2(id number,bonus number) is 2 3 4 5 6 7 n1 number; begin select (salary+(salary*bonus/100)) into n1 from staff_details where staff_in_id=id; dbms_output.put_line('Salary with bonus'||''||n1); end; /

Procedure created. SQL> set serveroutput on;

SQL> call p2(10,25); Salary with bonus62500 Call completed. 3. Create a function to find the junior most teaching staff: SQL> create or replace function f1 2 3 4 5 return varchar2 is n2 varchar(20); begin select staff_name into n2 from staff_details where join_date>=all(select join_date from sta

ff_details); 6 7 8 / Function created. SQL> begin 2 3 4 / The Junior most staff is :priya PL/SQL procedure successfully completed. 4. Create function to display the number of students joined at the particular date when the date is given as input: SQL> create or replace function f2(ad_date in date) 2 3 return number is n1 number; dbms_output.put_line('The Junior most staff is :'||f1); end; return n2; end;

4 5 6 7 8 /

begin select count(roll_no) into n1 from a_details where a_date=ad_date; return n1; end;

Function created. SQL> begin 2 dbms_output.put_line('Number of students admitted in the given date :'||f2('27-jul-2009')); 3 end; 4 / Number of students admitted in the given date :2 5. Create a cursor to display the student details when batch and department is given as input: SQL> declare cursor c1 is 2 3 4 5 6 7 8 9 10 11 12 select name,roll_no from a_details where batch=&batch and dept='&dept'; n1 varchar(10); n2 number; begin open c1; if c1 %isopen then loop fetch c1 into n1,n2; exit when c1 %notfound; if c1 %found then dbms_output.put_line(n1||''||n2);

13 14 15 16 17 18 19 20 21 /

end if; end loop; commit; else dbms_output.put_line('unable'); close c1; end if; end;

Enter value for batch: 2008 Enter value for dept: cs old 2: new 2: rasi3 PL/SQL procedure successfully completed. 6. Create a cursor to find the number of hostel students in each batch: declare cursor c2 is select count(roll_no),batch from a_details where accomod='hostl' group by batch; n1 number; n2 number; begin open c2; if c2 %isopen then loop fetch c2 into n1,n2; select name,roll_no from a_details where batch=&batch and dept='&dept'; select name,roll_no from a_details where batch=2008 and dept='cs';

exit when c2 %notfound; if c2 %found then dbms_output.put_line(n1||''||''||n2); end if; end loop; commit; else dbms_output.put_line('unable'); close c2; end if; end; / 22009 12008

Result:
Thus the functions, procedures and cursors are created for the given queries and executed successfully.

You might also like