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

Assignment 6

The document discusses various examples of using cursors in PL/SQL blocks. It provides examples of implicit, explicit and parameterized cursors to perform operations like updating records, inserting into other tables and merging data between tables.

Uploaded by

wacih58121
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)
20 views

Assignment 6

The document discusses various examples of using cursors in PL/SQL blocks. It provides examples of implicit, explicit and parameterized cursors to perform operations like updating records, inserting into other tables and merging data between tables.

Uploaded by

wacih58121
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/ 10

Assignment 6

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)
CREATE TABLE acc1
(
acc_no INT PRIMARY KEY,
status VARCHAR(10)
);
INSERT INTO acc1 VALUES(1,'IA');
INSERT INTO acc VALUES(2,'IA');
INSERT INTO acc VALUES(3,'IA');

Declare
rows number(10);
Begin
update acc set status='A' where status='IA';
IF SQL%FOUND
then
dbms_output.put_line('changed');
else
dbms_output.put_line('unchanged');
end if;
rows:=SQL%rowcount;
dbms_output.put_line('No of rows affected='||rows);
END;
/
Output:
CREATE TABLE acc
(
acc_no INT PRIMARY KEY,
status VARCHAR(10)
);

Table created.

SQL> INSERT INTO acc VALUES(1,'IA');

1 row created.

SQL> INSERT INTO acc VALUES(2,'IA');

1 row created.

SQL> INSERT INTO acc VALUES(3,'IA');

1 row created.

SQL>
Declare
rows number(10);
Begin
update acc set status='A' where status='IA';
IF SQL%FOUND
then
dbms_output.put_line('changed');
else
dbms_output.put_line('unchanged');
end if;
rows:=SQL%rowcount;
dbms_output.put_line('No of rows affected='||rows);
END;
/
changed
No of rows affected=3

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)

CREATE TABLE emp298


(
name VARCHAR(20) PRIMARY KEY,
salary INT
);
CREATE TABLE incresal
(
name VARCHAR(20) PRIMARY KEY,
salary INT
);

INSERT INTO emp298 VALUES('beena',5690);


INSERT INTO emp298 VALUES('mena',9900);
INSERT INTO emp298 VALUES('ki',690);

Declare
cursor cr is select name,salary from emp298 where salary<(select avg(salary) from
emp298);
mname emp298.name%type;
msalary emp298.salary%type;
Begin
open cr;
if cr%isopen then
loop
fetch cr into mname,msalary;
exit when cr%notfound;
if cr%found then
update emp298 set salary=salary+(salary*0.1) where salary=msalary;
insert into incresal values(mname, msalary);
end if;
end loop;
end if;
close cr;
end;
/
CREATE TABLE emp298
(
name VARCHAR(20) PRIMARY KEY,
salary INT
);

Table created.
SQL> CREATE TABLE incresal
2 (
3 name VARCHAR(20) PRIMARY KEY,
4 salary INT
5 );

Table created.

SQL>
SQL> INSERT INTO emp298 VALUES('beena',5690);

1 row created.

SQL> INSERT INTO emp298 VALUES('mena',9900);

1 row created.

SQL> INSERT INTO emp298 VALUES('ki',690);

1 row created.

SQL>
SQL> Declare
2 cursor cr is select name,salary from emp298 where salary<(select avg(salary)
from emp298);
3 mname emp298.name%type;
4 msalary emp298.salary%type;
5 Begin
6 open cr;
7 if cr%isopen then
8 loop
9 fetch cr into mname,msalary;
10 exit when cr%notfound;
11 if cr%found then
12 update emp298 set salary=salary+(salary*0.1) where salary=msalary;
13 insert into incresal values(mname, msalary);
14 end if;
15 end loop;
16 end if;
17 close cr;
18 end;
19 /

PL/SQL procedure successfully completed.

SQL> SELECT *FROM incresal;

NAME SALARY
-------------------- ----------
ki 690
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));

CREATE TABLE stud21 (


roll NUMBER(4),
att NUMBER(4),
status VARCHAR(1)
);

CREATE TABLE D_stud (


roll NUMBER(4),
att NUMBER(4)
);

INSERT INTO stud21 VALUES (1, 80, 'P');


INSERT INTO stud21 VALUES (2, 70, 'P');
INSERT INTO stud21 VALUES (3, 60, 'P');
INSERT INTO stud21 VALUES (4, 50, 'P');
Declare
Cursor crsr_att is select roll, att,status from stud11 where att<75;
mroll stud11.roll%type;
matt stud11.att%type;
mstatus stud11.status%type;
Begin
open crsr_att;
if crsr_att%isopen then
loop
fetch crsr_att into mroll,matt,mstatus;
exit when crsr_att%notfound;
if crsr_att%found then
update stud11 set status='D' where roll=mroll;
insert into d_stud values(mroll,matt);
end if;
end loop;
end if;
Close crsr_att;
end;
Output:
SQL> CREATE TABLE stud21 (
2 roll NUMBER(4),
3 att NUMBER(4),
4 status VARCHAR(1)
5 );

Table created.

SQL>
SQL> CREATE TABLE D_stud (
2 roll NUMBER(4),
3 att NUMBER(4)
4 );

Table created.

SQL>
SQL> INSERT INTO stud21 VALUES (1, 80, 'P');

1 row created.

SQL> INSERT INTO stud21 VALUES (2, 70, 'P');

1 row created.

SQL> INSERT INTO stud21 VALUES (3, 60, 'P');


1 row created.

SQL> INSERT INTO stud21 VALUES (4, 50, 'P');

1 row created.
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
2 CURSOR crsr_att IS
3 SELECT roll, att, status
4 FROM stud21
5 WHERE att < 75;
6
7 mroll stud21.roll%TYPE;
8 matt stud21.att%TYPE;
9 mstatus stud21.status%TYPE;
10 BEGIN
11 OPEN crsr_att;
12 IF crsr_att%ISOPEN THEN
13 LOOP
14 FETCH crsr_att INTO mroll, matt, mstatus;
15 EXIT WHEN crsr_att%NOTFOUND;
16 IF crsr_att%FOUND THEN
17 UPDATE stud21 SET status = 'D' WHERE roll = mroll;
18 INSERT INTO D_stud VALUES (mroll, matt);
19 END IF;
20 END LOOP;
21 END IF;
22 CLOSE crsr_att;
23 END;
24 /

PL/SQL procedure successfully completed.

SQL> SELECT *FROM D_stud;

ROLL ATT
---------- ----------
2 70
3 60
4 50
parameterized Cursor 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.
CREATE TABLE N_RollCall (
roll_number NUMBER(4),
attendance_status VARCHAR2(1)
);

CREATE TABLE O_RollCall (


roll_number NUMBER(4),
attendance_status VARCHAR2(1)
);
INSERT INTO N_RollCall (roll_number, attendance_status) VALUES (101, 'P');
INSERT INTO N_RollCall (roll_number, attendance_status) VALUES (102, 'A');
INSERT INTO N_RollCall (roll_number, attendance_status) VALUES (103, 'P');
INSERT INTO N_RollCall (roll_number, attendance_status) VALUES (104, 'P');
INSERT INTO O_RollCall (roll_number, attendance_status) VALUES (101, 'P');
INSERT INTO O_RollCall (roll_number, attendance_status) VALUES (102, 'P');
INSERT INTO O_RollCall (roll_number, attendance_status) VALUES (103, 'A');

DECLARE
CURSOR c_merge_data (p_roll NUMBER) IS
SELECT *
FROM N_RollCall
WHERE roll_number = p_roll;

v_roll N_RollCall.roll_number%TYPE;
v_other_column N_RollCall.attendance_status%TYPE;
BEGIN
FOR data_rec IN c_merge_data(p_roll => 123) LOOP

BEGIN
SELECT roll_number INTO v_roll
FROM O_RollCall
WHERE roll_number = data_rec.roll_number;

EXCEPTION
WHEN NO_DATA_FOUND THEN
-- Insert data into O_RollCall if it doesn't already exist
INSERT INTO O_RollCall (roll_number, attendance_status)
VALUES (data_rec.roll_number, data_rec.attendance_status);
END;
END LOOP;

COMMIT;
END;
/
Output:
SQL> CREATE TABLE N_RollCall (
2 roll_number NUMBER(4),
3 attendance_status VARCHAR2(1)
4 );

Table created.

SQL>
SQL> CREATE TABLE O_RollCall (
2 roll_number NUMBER(4),
3 attendance_status VARCHAR2(1)
4 );

Table created.

SQL> INSERT INTO N_RollCall (roll_number, attendance_status) VALUES (101, 'P');

1 row created.

SQL> INSERT INTO N_RollCall (roll_number, attendance_status) VALUES (102, 'A');

1 row created.

SQL> INSERT INTO N_RollCall (roll_number, attendance_status) VALUES (103, 'P');

1 row created.
SQL> INSERT INTO O_RollCall (roll_number, attendance_status) VALUES (101, 'P');

1 row created.

SQL> INSERT INTO O_RollCall (roll_number, attendance_status) VALUES (102, 'P');

1 row created.

SQL> INSERT INTO O_RollCall (roll_number, attendance_status) VALUES (103, 'A');

1 row created.
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
2 CURSOR c_merge_data (p_roll NUMBER) IS
3 SELECT *
4 FROM N_RollCall
5 WHERE roll_number = p_roll;
6
7 v_roll N_RollCall.roll_number%TYPE;
8 v_other_column N_RollCall.attendance_status%TYPE;
9 BEGIN
10 FOR data_rec IN c_merge_data(p_roll => 123) LOOP
11 -- Check if data already exists in O_RollCall
12 BEGIN
13 SELECT roll_number INTO v_roll
14 FROM O_RollCall
15 WHERE roll_number = data_rec.roll_number;
16
17 -- Data already exists, skip
18 EXCEPTION
19 WHEN NO_DATA_FOUND THEN
20 -- Insert data into O_RollCall if it doesn't already exist
21 INSERT INTO O_RollCall (roll_number, attendance_status)
22 VALUES (data_rec.roll_number, data_rec.attendance_status);
23 END;
24 END LOOP;
25
26 COMMIT;
27 END;
28 /

PL/SQL procedure successfully completed.

SQL> SELECT *FROM O_RollCall;

ROLL_NUMBER A
----------- -
101 P
102 P
103 A
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)
CREATE TABLE dept_salary (
d_no NUMBER(4),
avg_salary NUMBER(10, 2)
);
CREATE TABLE EMP (
e_no NUMBER(4),
d_no NUMBER(4),
Salary NUMBER(10, 2)
);
INSERT INTO EMP (e_no, d_no, Salary) VALUES (1, 101, 50000);
INSERT INTO EMP (e_no, d_no, Salary) VALUES (2, 102, 60000);
INSERT INTO EMP (e_no, d_no, Salary) VALUES (3, 101, 55000);
INSERT INTO EMP (e_no, d_no, Salary) VALUES (4, 103, 70000);
DECLARE
CURSOR c_avg_salary (p_d_no NUMBER) IS
SELECT d_no, AVG(salary) AS avg_salary
FROM EMP
WHERE d_no = p_d_no
GROUP BY d_no;

v_d_no EMP.d_no%TYPE;
v_avg_salary EMP.salary%TYPE;
BEGIN
FOR salary_rec IN c_avg_salary(p_d_no => 123) LOOP
-- Insert department wise average salary into dept_salary table
INSERT INTO dept_salary (d_no, avg_salary)
VALUES (salary_rec.d_no, salary_rec.avg_salary);
END LOOP;

COMMIT;
END;
/
SQL> CREATE TABLE dept_salary (
2 d_no NUMBER(4),
3 avg_salary NUMBER(10, 2)
4 );

Table created.

SQL> INSERT INTO EMP (e_no, d_no, Salary) VALUES (1, 101, 50000);

1 row created.

SQL> INSERT INTO EMP (e_no, d_no, Salary) VALUES (2, 102, 60000);

1 row created.

SQL> INSERT INTO EMP (e_no, d_no, Salary) VALUES (3, 101, 55000);

1 row created.

SQL> INSERT INTO EMP (e_no, d_no, Salary) VALUES (4, 103, 70000);

1 row created.

SQL> DECLARE
2 CURSOR c_avg_salary (p_d_no NUMBER) IS
3 SELECT d_no, AVG(salary) AS avg_salary
4 FROM EMP
5 WHERE d_no = p_d_no
6 GROUP BY d_no;
7
8 v_d_no EMP.d_no%TYPE;
9 v_avg_salary EMP.salary%TYPE;
10 BEGIN
11 FOR salary_rec IN c_avg_salary(p_d_no => 123) LOOP
12 -- Insert department wise average salary into dept_salary table
13 INSERT INTO dept_salary (d_no, avg_salary)
14 VALUES (salary_rec.d_no, salary_rec.avg_salary);
15 END LOOP;
16
17 COMMIT;
18 END;
19 /

PL/SQL procedure successfully completed.

SQL> SELECT *FROM dept_salary;

D_NO AVG_SALARY
---------- ----------
101 50000
102 55000
103 60000
104 52000
EXPLICIT CURSOR: Cursor for loop 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));
CREATE TABLE stud21 (
roll NUMBER(4),
att NUMBER(4),
status VARCHAR(1)
);

CREATE TABLE d_stud (


roll NUMBER(4),
att NUMBER(4)
);
INSERT INTO stud21 (roll, att, status) VALUES (1, 80, 'P');
INSERT INTO stud21 (roll, att, status) VALUES (2, 70, 'P');
INSERT INTO stud21 (roll, att, status) VALUES (3, 60, 'P');
INSERT INTO stud21 (roll, att, status) VALUES (4, 50, 'P');

Declare
Cursor crsr_att is select roll, att,status from
stud21 where att<75;
Begin
for demo IN crsr_att
loop
update stud21 set status='D' where
roll=demo.roll;
insert into d_stud values(demo.roll,demo.att);
end loop;
End;
SQL> CREATE TABLE stud21 (
2 roll NUMBER(4),
3 att NUMBER(4),
4 status VARCHAR(1)
5 );

Table created.
SQL>
SQL> CREATE TABLE d_stud (
2 roll NUMBER(4),
3 att NUMBER(4)
4 );

Table created.

SQL> INSERT INTO stud21 (roll, att, status) VALUES (1, 80, 'P');

1 row created.

SQL> INSERT INTO stud21 (roll, att, status) VALUES (2, 70, 'P');

1 row created.

SQL> INSERT INTO stud21 (roll, att, status) VALUES (3, 60, 'P');

1 row created.

SQL> INSERT INTO stud21 (roll, att, status) VALUES (4, 50, 'P');

1 row created.
SQL> SET SERVEROUTPUT ON;
SQL> Declare
2 Cursor crsr_att is select roll, att,status from
3 stud21 where att<75;
4 Begin
5 for demo IN crsr_att
6 loop
7 update stud21 set status='D' where
8 roll=demo.roll;
9 insert into d_stud values(demo.roll,demo.att);
10 end loop;
11 END;
12 /

PL/SQL procedure successfully completed.

SQL> SELECT *FROM d_stud;

ROLL ATT
---------- ----------
2 70
3 60
4 50

You might also like