DBMS_assign6_SYCOC171
DBMS_assign6_SYCOC171
Implicit Cursor
1. The bank manager has decided to activate all those accounts which were previously
marked as
inactive for performing no transaction in last 365 days. Write a PL/SQ block (using
implicit cursor)
to update the status of account, display an approximate message based on the no. of rows
affected
by the update.
(Use of %FOUND, %NOTFOUND, %ROWCOUNT)
-->
SQL> insert into customer values(101,'Sam',250,'Active');
1 row created.
SQL> insert into customer values(102,'Ravi',390,'Inactive');
1 row created.
1 row created.
1 row created.
1 row created.
SQL> DECLARE
2 total_rows number(6);
3 Begin
4 update customer set status='Active' where status='Inactive' and last_trans>365 ;
5 IF sql%found THEN
6 total_rows := sql%rowcount;
7 dbms_output.put_line( total_rows || ' Accounts activated successfully !!');
8 ELSIF sql%notfound THEN
9 dbms_output.put_line('Updation Not required !!');
10 END IF;
11 END;
12
13 /
3 Accounts activated successfully !!
PL/SQL procedure successfully completed.
=====================================================================================
EXPLICIT CURSOR:
2. Organization has decided to increase the salary of employees by 10% of existing
salary, who are
having salary less than average salary of organization, Whenever such salary updates
takes place, a
record for the same is maintained in the increment_salary table.
EMP (E_no , Salary)
increment_salary(E_no , Salary)
-->
SQL> create table emp( e_no int, salary int);
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
E_NO SALARY
---------- ----------
201 10000
202 30000
203 12000
204 45000
205 14000
206 19000
Table created.
SQL> declare
2 avg_salary int;
3
4 cursor sal_crsr is select e_no ,salary from emp where salary<avg_salary;
5 memp_no emp.e_no%type;
6 msalary emp.salary%type;
7 mincr_sal int;
8 Begin
9
10
11 select avg(salary) into avg_salary from emp;
12 dbms_output.put_line('Average salary = ' || avg_salary);
13 open sal_crsr;
14 if sal_crsr%isopen then
15 loop
16 fetch sal_crsr into memp_no,msalary;
17 exit when sal_crsr%notfound;
18 if sal_crsr%found then
19
20 mincr_sal:= (msalary)+(msalary*10/100);
21 insert into increment_salary values(memp_no,mincr_sal);
22 end if;
23 end loop;
24 end if;
25 close sal_crsr;
26
27 End;
28 /
Average salary = 21667
E_NO SALARY
---------- ----------
201 11000
203 13200
205 15400
206 20900
======================================================================================
3. Write PL/SQL block using explicit cursor for following requirements:
College has decided to mark all those students detained (D) who are having attendance
less than
75%. Whenever such update takes place, a record for the same is maintained in the D_Stud
table.
create table stud(roll number(4), att number(4), status varchar(1));
create table d_stud(roll number(4), att number(4));
parameterized Cursor
-->
SQL> create table stud(roll number(4), att number(4), status varchar(1));
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
ROLL ATT ST
---------- ---------- --
1 90
2 60
3 89
4 74
5 40
SQL> DECLARE
2 CURSOR CSR
3 IS
4 SELECT ROLL,ATT,STATUS FROM STUD WHERE ATT<75;
5 MROLL STUD.ROLL%TYPE;
6 MATT STUD.ATT%TYPE;
7 MSTATUS STUD.STATUS%TYPE;
8 BEGIN
9 OPEN CSR;
10 IF CSR%ISOPEN THEN
11 LOOP
12 FETCH CSR INTO MROLL,MATT,MSTATUS;
13 EXIT WHEN CSR%NOTFOUND;
14 IF CSR%FOUND THEN
15 UPDATE STUD SET STATUS='D' WHERE ROLL=MROLL;
16 INSERT INTO D_STUD VALUES(MROLL,MATT);
17 END IF;
18 END LOOP;
19 END IF;
20 CLOSE CSR;
21 END;
22 /
ROLL ATT
---------- ----------
2 60
4 74
5 40
ROLL ATT ST
---------- ---------- --
1 90
2 60 D
3 89
4 74 D
5 40 D
=======================================================================================
4. Write a PL/SQL block of code using parameterized Cursor, that will merge the data
available in
the newly created table N_RollCall with the data available in the table O_RollCall. If
the data in the
first table already exist in the second table then that data should be skipped.
-->
SQL> create table N_ROLLCALL(roll number(10), name varchar(10));
Table created.
1 row created.
1 row created.
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
ROLL NAME
---------- ----------
401 Avi
402 Savi
403 Adi
404 Dev
ROLL NAME
---------- ----------
401 Avi
402 Savi
SQL> declare
2 cursor cur_class is select * from O_ROLLCALL;
3 cursor cur_check(str_name varchar) is select roll from N_ROLLCALL where name
=str_name;
4 str_roll N_ROLLCALL.roll%type;
5 str_name N_ROLLCALL.name%type;
6 load number(10);
7 Begin
8 open cur_class;
9 loop
10 fetch cur_class into str_roll,str_name;
11 Exit when cur_class%notfound;
12 open cur_check(str_name);
13 fetch cur_check into load;
14 if cur_check%found then
15 dbms_output.put_line('stud' || ' ' || str_name || ' ' || 'Name is already there
.' );
16 else
17 dbms_output.put_line('stud' || ' ' || str_name || ' ' || 'Name does not exist .
Inserting in new_class table' );
18 insert into N_ROLLCALL values(str_roll,str_name);
19 end if;
20 close cur_check;
21 end loop;
22 close cur_class;
23 end;
24
25 /
stud Avi Name is already there .
stud Savi Name is already there .
stud Adi Name does not exist . Inserting in new_class table
stud Dev Name does not exist . Inserting in new_class table
ROLL NAME
---------- ----------
401 Avi
402 Savi
403 Adi
404 Dev
=====================================================================================
parameterized Cursor
5. Write the PL/SQL block for following requirements using parameterized Cursor:
Consider table EMP(e_no, d_no, Salary), department wise average salary should be inserted
into
new table dept_salary(d_no, Avg_salary)
EXPLICIT CURSOR: Cursor for loop
-->
Table created.
SQL> Declare
2 cursor crsr_salary is select * from emp;
3 cursor crsr_avg(vdno number) is select d_no, avg(salary) from emp group by(d_no)
having d_no=vdno ;
4 veno number(10);
5 vdno number(10);
6 vsal number(10);
7 mdno number(10);
8 mavsal number(10);
9 Begin
10 Open crsr_salary;
11 Loop
12 fetch crsr_salary into veno, vsal,vdno;
13 Exit When crsr_salary%NOTFOUND;
14 Open crsr_avg(vdno);
15 loop
16 Fetch crsr_avg into mdno,mavsal;
17 Exit When crsr_avg%NOTFOUND;
18 if crsr_avg%FOUND Then
19 dbms_output.put_line('Record Found . ');
20 End if;
21 End loop;
22 insert into dept_salary values(mdno,mavsal);
23 Close crsr_avg;
24 End loop;
25 Close crsr_salary;
26 End;
27 /
Record Found .
Record Found .
Record Found .
Record Found .
Record Found .
Record Found .
D_NO AVG_SALARY
---------- ----------
2 42500
3 70000
1 25000
=====================================================================================
6. Write PL/SQL block using explicit cursor: Cursor FOR Loop for following requirements:
College has decided to mark all those students detained (D) who are having attendance
less than
75%. Whenever such update takes place, a record for the same is maintained in the D_Stud
table.
create table stud21(roll number(4), att number(4), status varchar(1));
create table d_stud(roll number(4), att number(4));
-->
ROLL ATT ST
---------- ---------- --
1 90
2 60
3 89
4 74
5 40
no rows selected
SQL> Declare
2 Cursor cur_stud is select roll, att, status from
3 stud where att<75;
4 Begin
5 For attendance IN cur_stud
6 Loop
7 Update stud set status=’D’ where
8 Roll=attendance.roll;
9 Insert into d_stud values(attendance.roll, attendance.att);
10 End loop;
11 END;
12 /
ROLL ATT
---------- ----------
2 60
4 74
5 40