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

DBMS_assign6_SYCOC171

The document provides various PL/SQL examples demonstrating the use of implicit and explicit cursors for updating account statuses, employee salary increments, and student attendance records. It includes code snippets for activating inactive accounts, increasing salaries for employees below average, marking detained students, and merging data between tables. Each example illustrates the use of cursors and database operations, along with the expected outputs after execution.

Uploaded by

lahareanushka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

DBMS_assign6_SYCOC171

The document provides various PL/SQL examples demonstrating the use of implicit and explicit cursors for updating account statuses, employee salary increments, and student attendance records. It includes code snippets for activating inactive accounts, increasing salaries for employees below average, marking detained students, and merging data between tables. Each example illustrates the use of cursors and database operations, along with the expected outputs after execution.

Uploaded by

lahareanushka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

//SYCOC171 DISHA PARALE

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.

SQL> insert into customer values(103,'Ram',300,'Active');

1 row created.

SQL> insert into customer values(104,'Savi',378,'Inactive');

1 row created.

SQL> insert into customer values(105,'Sham',400,'Inactive');

1 row created.

SQL> select * from customer ;

ACC_NO NAME LAST_TRANS STATUS_BEF


---------- ---------- ---------- ----------
101 Sam 250 Active
102 Ravi 390 Inactive
103 Ram 300 Active
104 Savi 378 Inactive
105 Sham 400 Inactive

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.

SQL> select * from customer;

ACC_NO NAME LAST_TRANS STATUS


---------- ---------- ---------- ----------
101 Sam 250 Active
102 Ravi 390 Active
103 Ram 300 Active
104 Savi 378 Active
105 Sham 400 Active

=====================================================================================
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.

SQL> insert into emp values(201,10000);

1 row created.

SQL> insert into emp values(202,30000);

1 row created.

SQL> insert into emp values(203,12000);

1 row created.

SQL> insert into emp values(204,45000);

1 row created.

SQL> insert into emp values(205,14000);

1 row created.

SQL> insert into emp values(206,19000);

1 row created.

SQL> select * from emp;

E_NO SALARY
---------- ----------
201 10000
202 30000
203 12000
204 45000
205 14000
206 19000

SQL> create table increment_salary(E_no int , Salary int );

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

PL/SQL procedure successfully completed.

SQL> select * from increment_salary;

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.

SQL> insert into stud (roll,att) values (&roll,&att);


Enter value for roll: 1
Enter value for att: 90
old 1: insert into stud (roll,att) values (&roll,&att)
new 1: insert into stud (roll,att) values (1,90)

1 row created.

SQL> insert into stud (roll,att) values (&roll,&att);


Enter value for roll: 2
Enter value for att: 60
old 1: insert into stud (roll,att) values (&roll,&att)
new 1: insert into stud (roll,att) values (2,60)

1 row created.

SQL> insert into stud (roll,att) values (&roll,&att);


Enter value for roll: 3
Enter value for att: 89
old 1: insert into stud (roll,att) values (&roll,&att)
new 1: insert into stud (roll,att) values (3,89)

1 row created.

SQL> insert into stud (roll,att) values (&roll,&att);


Enter value for roll: 4
Enter value for att: 74
old 1: insert into stud (roll,att) values (&roll,&att)
new 1: insert into stud (roll,att) values (4,74)

1 row created.

SQL> insert into stud (roll,att) values (&roll,&att);


Enter value for roll: 5
Enter value for att: 40
old 1: insert into stud (roll,att) values (&roll,&att)
new 1: insert into stud (roll,att) values (5,40)

1 row created.

SQL> select * from stud;

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 /

PL/SQL procedure successfully completed.

SQL> select * from d_stud;

ROLL ATT
---------- ----------
2 60
4 74
5 40

SQL> select * from stud;

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.

SQL> insert into N_ROLLCALL values(401 ,'Avi');

1 row created.

SQL> insert into N_ROLLCALL values(402 ,'Savi');

1 row created.

SQL> create table O_ROLLCALL(roll number(10), name varchar(10));

Table created.

SQL> insert into O_ROLLCALL values(401 ,'Avi');

1 row created.

SQL> insert into O_ROLLCALL values(402 ,'Savi');

1 row created.

SQL> insert into O_ROLLCALL values(403 ,'Adi');

1 row created.

SQL> insert into O_ROLLCALL values(404 ,'Dev');

1 row created.

SQL> select * from O_ROLLCALL;

ROLL NAME
---------- ----------
401 Avi
402 Savi
403 Adi
404 Dev

SQL> select * from N_ROLLCALL;

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

PL/SQL procedure successfully completed.

SQL> select * from N_ROLLCALL;

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

SQL> select * from emp;

E_NO SALARY D_NO


---------- ---------- ----------
101 30000 1
102 20000 1
103 40000 2
104 45000 2
105 80000 3
106 60000 3

SQL> create table dept_salary(d_no int , Avg_salary int);

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 .

PL/SQL procedure successfully completed.

SQL> select distinct * from dept_salary;

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));

-->

SQL> select *from stud;

ROLL ATT ST
---------- ---------- --
1 90
2 60
3 89
4 74
5 40

SQL> select *from d_stud;

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 /

PL/SQL procedure successfully completed.

SQL> select * from d_stud;

ROLL ATT
---------- ----------
2 60
4 74
5 40

You might also like