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

ASSIGNMENT 7 DBMS CODE

The document contains multiple PL/SQL blocks demonstrating the use of implicit and explicit cursors for various database operations. It includes examples for updating account statuses, increasing employee salaries, marking students as detained based on attendance, merging data between tables, and calculating department-wise average salaries. Each section provides the necessary SQL commands, table structures, and procedural logic for the operations described.

Uploaded by

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

ASSIGNMENT 7 DBMS CODE

The document contains multiple PL/SQL blocks demonstrating the use of implicit and explicit cursors for various database operations. It includes examples for updating account statuses, increasing employee salaries, marking students as detained based on attendance, merging data between tables, and calculating department-wise average salaries. Each section provides the necessary SQL commands, table structures, and procedural logic for the operations described.

Uploaded by

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

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)

OUTPUT :

CREATE TABLE account (acc_no int primary key , status varchar(20) , last_transaction_date DATE);

INSERT into account values (1,'INACTIVE',SYSDATE-500);


INSERT into account values (2,'ACTIVE',SYSDATE-10);
INSERT into account values (3,'INACTIVE',SYSDATE-400);
INSERT into account values (4,'INACTIVE',SYSDATE-55);
INSERT into account values (5,'INACTIVE',SYSDATE-567);
INSERT into account values (6,'ACTIVE',SYSDATE-50);
INSERT into account values (7,'ACTIVE',SYSDATE-5);

select * FROM account where status='INACTIVE' AND last_transaction_date <= SYSDATE-365;

DECLARE
row_count number;

BEGIN

FOR REC IN (select * FROM account where status='INACTIVE' AND last_transaction_date <=
SYSDATE-365)
LOOP
DBMS_OUTPUT.PUT_LINE('account NO : ' || REC.acc_no || ', status : ' || REC.status || ', date :' ||
REC.last_transaction_date);
end LOOP;

update account
set status = 'ACTIVE' where status='INACTIVE' AND last_transaction_date <= SYSDATE-365;

IF SQL%FOUND THEN
DBMS_OUTPUT.PUT_LINE('The update was successful.');
row_count := SQL%ROWCOUNT;
DBMS_OUTPUT.PUT_LINE('The row count is ' || row_count);
ELSE
DBMS_OUTPUT.PUT_LINE('No rows were updated.');
END IF;

FOR REC IN (select * FROM account )


LOOP
DBMS_OUTPUT.PUT_LINE('account NO : ' || REC.acc_no || ', status : ' || REC.status || ', date :' ||
REC.last_transaction_date);
end LOOP;

END;
/

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)

OUTPUT :

CREATE TABLE EMP (

E_no INT PRIMARY KEY,

Salary DECIMAL(10,2)

);

INSERT INTO EMP (E_no, Salary) VALUES (101, 50000);

INSERT INTO EMP (E_no, Salary) VALUES (102, 40000);

INSERT INTO EMP (E_no, Salary) VALUES (103, 55000);

INSERT INTO EMP (E_no, Salary) VALUES (104, 35000);

INSERT INTO EMP (E_no, Salary) VALUES (105, 45000);

CREATE TABLE increment_salary (

E_no INT PRIMARY KEY,


Salary DECIMAL(10,2)

);

DECLARE

CURSOR emp_cursor IS

SELECT E_no, Salary

FROM EMP

WHERE Salary < (SELECT AVG(Salary) FROM EMP);

v_E_no EMP.E_no%TYPE;

v_Salary EMP.Salary%TYPE;

v_New_Salary EMP.Salary%TYPE;

BEGIN

OPEN emp_cursor;

LOOP

FETCH emp_cursor INTO v_E_no, v_Salary;

EXIT WHEN emp_cursor%NOTFOUND;

v_New_Salary := v_Salary * 1.10;

UPDATE EMP

SET Salary = v_New_Salary

WHERE E_no = v_E_no;

INSERT INTO increment_salary (E_no, Salary)

VALUES (v_E_no, v_New_Salary);

END LOOP;

CLOSE emp_cursor;

COMMIT;

END;

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


OUTPUT:

CREATE TABLE stud21 (

roll NUMBER(4) PRIMARY KEY,

att NUMBER(4),

status VARCHAR2(1)

);

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

INSERT INTO stud21 VALUES (1002, 70, 'P');

INSERT INTO stud21 VALUES (1003, 60, 'P');

INSERT INTO stud21 VALUES (1004, 90, 'P');

INSERT INTO stud21 VALUES (1005, 50, 'P');

CREATE TABLE d_stud (

roll NUMBER(4) PRIMARY KEY,

att NUMBER(4)

);

DECLARE

CURSOR stud_cursor IS

SELECT roll, att FROM stud21 WHERE att < 75;

v_roll stud21.roll%TYPE;
v_att stud21.att%TYPE;

BEGIN

OPEN stud_cursor;

LOOP

FETCH stud_cursor INTO v_roll, v_att;

EXIT WHEN stud_cursor%NOTFOUND;

UPDATE stud21

SET status = 'D'

WHERE roll = v_roll;

INSERT INTO d_stud (roll, att)

VALUES (v_roll, v_att);

END LOOP;

CLOSE stud_cursor;

COMMIT;

END;

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_RoliCall. If the data in the first
table already exist in the second table then that data should be skipped.

OUTPUT :

CREATE TABLE N_RollCall (

roll NUMBER(4) PRIMARY KEY,

name VARCHAR2(50),
attendance NUMBER(3)

);

INSERT INTO N_RollCall VALUES (1001, 'Aniket', 85);

INSERT INTO N_RollCall VALUES (1002, 'kartik', 70);

INSERT INTO N_RollCall VALUES (1003, 'sai', 90);

CREATE TABLE O_RollCall (

roll NUMBER(4) PRIMARY KEY,

name VARCHAR2(50),

attendance NUMBER(3)

);

INSERT INTO O_RollCall VALUES (1002, 'kartik', 70);

INSERT INTO O_RollCall VALUES (1004, 'Babar', 95);

DECLARE

CURSOR roll_cursor(p_roll NUMBER) IS

SELECT roll, name, attendance

FROM N_RollCall

WHERE roll = p_roll;

v_roll N_RollCall.roll%TYPE;

v_name N_RollCall.name%TYPE;

v_attendance N_RollCall.attendance%TYPE;

BEGIN

FOR rec IN (SELECT roll FROM N_RollCall) LOOP

OPEN roll_cursor(rec.roll);

FETCH roll_cursor INTO v_roll, v_name, v_attendance;

CLOSE roll_cursor;
IF NOT EXISTS (SELECT 1 FROM O_RollCall WHERE roll = v_roll) THEN

INSERT INTO O_RollCall (roll, name, attendance)

VALUES (v_roll, v_name, v_attendance);

END IF;

END LOOP;

COMMIT;

END;

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)

OUTPUT :

CREATE TABLE EMP (

e_no NUMBER(4) PRIMARY KEY,

d_no NUMBER(4),

Salary NUMBER(10,2)

);

INSERT INTO EMP VALUES (1001, 10, 50000);

INSERT INTO EMP VALUES (1002, 10, 60000);

INSERT INTO EMP VALUES (1003, 20, 70000);

INSERT INTO EMP VALUES (1004, 20, 80000);

INSERT INTO EMP VALUES (1005, 30, 90000);

CREATE TABLE dept_salary (

d_no NUMBER(4) PRIMARY KEY,

Avg_salary NUMBER(10,2)
);

DECLARE

CURSOR dept_cursor(p_dno NUMBER) IS

SELECT d_no, AVG(Salary) AS Avg_salary

FROM EMP

WHERE d_no = p_dno

GROUP BY d_no;

v_dno EMP.d_no%TYPE;

v_Avg_salary dept_salary.Avg_salary%TYPE;

BEGIN

FOR rec IN (SELECT DISTINCT d_no FROM EMP) LOOP

OPEN dept_cursor(rec.d_no);

FETCH dept_cursor INTO v_dno, v_Avg_salary;

CLOSE dept_cursor;

INSERT INTO dept_salary (d_no, Avg_salary)

VALUES (v_dno, v_Avg_salary);

END LOOP;

COMMIT;

END;

You might also like