Dbms 6 Cursor
Dbms 6 Cursor
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> create table Acc_55(Acc_no number(10) not null primary key , Status char(2));
Table created.
SQL> insert into Acc_55 values(1001 , A);
SQL> insert into Acc_55 values(1001 , 'A');
1 row created.
SQL> insert into Acc_55 values(1002 , 'NA');
1 row created.
SQL> insert into Acc_55 values(1003 , 'NA');
1 row created.
SQL> insert into Acc_55 values(1004 , 'A');
1 row created.
SQL> insert into Acc_55 values(1005 , 'NA');
1 row created.
SQL> declare
2 flag number;
3 account number(10);
4 begin
5 account:=&account;
6
7 update Acc_55 set Status = 'A' where account=Acc_no and Status = 'NA';
8
9 flag := (sql%rowcount);
10 dbms_output.put_line('No. of records updated :'|| flag);
11 end;
12 /
Enter value for account: 1002
old 5: account:=&account;
new 5: account:=1002;
PL/SQL procedure successfully completed.
SQL> declare
2 flag number;
3 account number(10);
4 begin
5 account:=&account;
6
7 update Acc_55 set Status = 'A' where account=Acc_no and Status = 'NA';
8
9 flag := (sql%rowcount);
10 dbms_output.put_line('No. of records updated :'|| flag);
11 end;
12 /
Enter value for account: 1005
old 5: account:=&account;
new 5: account:=1005;
PL/SQL procedure successfully completed.
SQL> select * from Acc_55;
ACC_NO ST
---------- --
1001 A
1002 A
1003 NA
1004 A
1005 A
SQL> set serveroutput on;
SQL> declare
2 flag number;
3 account number(10);
4 begin
5 account:=&account;
6
7 update Acc_55 set Status = 'A' where account=Acc_no and Status = 'NA';
8
9 flag := (sql%rowcount);
10 dbms_output.put_line('No. of records updated :'|| flag);
11 end;
12 /
Enter value for account: 1003
old 5: account:=&account;
new 5: account:=1003;
No. of records updated :1
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------
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_55(Emp_no number(10) not null , Salary number(10,2));
Table created.
SQL> insert into EMP_55 values(1001,50000);
1 row created.
SQL> insert into EMP_55 values(1002,150000);
1 row created.
SQL> insert into EMP_55 values(1003,10000);
1 row created.
SQL> insert into EMP_55 values(1004,120000);
1 row created.
SQL> insert into EMP_55 values(1005,20000);
1 row created.
SQL> select * from EMP_55;
EMP_NO SALARY
---------- ----------
1001 50000
1002 150000
1003 10000
1004 120000
1005 20000
SQL> create table Increment_sal_55(E_no number(10) not null , Sal number(10,
2));
Table created.
SQL> declare
2 cursor C_sal is select Emp_no,Salary from EMP_55 where Salary<(select AVG(Salary)
from EMP_55);
3 empid EMP_55.Emp_no%type;
4 empsal EMP_55.Salary%type;
5 begin
6 open c_sal;
7 WHILE c_sal%isopen
8 LOOP
9 fetch c_sal into empid,empsal;
10 exit when c_sal%notfound;
11 if c_sal%found then
12 update EMP_55 set Salary = (Salary + (Salary*0.1)) where Emp_no=empid;
13 insert into Increment_sal_55 values(empid,empsal);
14 end if;
15 end LOOP;
16 close c_sal;
17 end;
18 /
PL/SQL procedure successfully completed.
SQL> select * from Increment_sal_55;
E_NO SAL
---------- ----------
1001 50000
1003 10000
1005 20000
SQL> commit;
Commit complete.
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------
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 stud21(roll number(4), att number(4), status varchar(1));
create table d_stud(roll number(4), att number(4));
SQL> create table Stud_55(Roll_no number(10),Att number(5,2),Status char(1))
;
Table created.
SQL> insert into Stud_55 values(1,90.32,NULL);
1 row created.
SQL> insert into Stud_55 values(2,72.20,NULL);
1 row created.
SQL> insert into Stud_55 values(3,63.00,NULL);
1 row created.
SQL> insert into Stud_55 values(4,82.90,NULL);
1 row created.
SQL> insert into Stud_55 values(5,87.90,NULL);
1 row created.
SQL> create table D_stud_55(Roll_no number(10),Att number(5,2));
Table created.
SQL> declare
2 Cursor c_att is select Roll_no,Att,Status from Stud_55 where Att<75;
3 mroll Stud_55.Roll_no%type;
4 matt Stud_55.Att%type;
5 mstatus Stud_55.Status%type;
begin
open c_att;
if c_att%isopen then
9 LOOP
10 fetch c_att into mroll,matt,mstatus;
11 exit when c_att%notfound;
12 if c_att%found then
13 update Stud_55 set Status='D' where mroll=Roll_no;
14 insert into D_stud_55 values(mroll,matt);
15 end if;
16 end LOOP;
17 end if;
18 close c_att;
19 end;
20 /
PL/SQL procedure successfully completed.
SQL> select * from D_stud_55 ;
ROLL_NO ATT
---------- ----------
2 72.2
3 63
SQL> commit;
Commit complete.