ASSIGNMENT 7 DBMS CODE
ASSIGNMENT 7 DBMS CODE
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.
OUTPUT :
CREATE TABLE account (acc_no int primary key , status varchar(20) , last_transaction_date DATE);
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;
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.
increment_salary(E_no, Salary)
OUTPUT :
Salary DECIMAL(10,2)
);
);
DECLARE
CURSOR emp_cursor IS
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
UPDATE EMP
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.
att NUMBER(4),
status VARCHAR2(1)
);
att NUMBER(4)
);
DECLARE
CURSOR stud_cursor IS
v_roll stud21.roll%TYPE;
v_att stud21.att%TYPE;
BEGIN
OPEN stud_cursor;
LOOP
UPDATE stud21
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 :
name VARCHAR2(50),
attendance NUMBER(3)
);
name VARCHAR2(50),
attendance NUMBER(3)
);
DECLARE
FROM N_RollCall
v_roll N_RollCall.roll%TYPE;
v_name N_RollCall.name%TYPE;
v_attendance N_RollCall.attendance%TYPE;
BEGIN
OPEN roll_cursor(rec.roll);
CLOSE roll_cursor;
IF NOT EXISTS (SELECT 1 FROM O_RollCall WHERE roll = v_roll) THEN
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 :
d_no NUMBER(4),
Salary NUMBER(10,2)
);
Avg_salary NUMBER(10,2)
);
DECLARE
FROM EMP
GROUP BY d_no;
v_dno EMP.d_no%TYPE;
v_Avg_salary dept_salary.Avg_salary%TYPE;
BEGIN
OPEN dept_cursor(rec.d_no);
CLOSE dept_cursor;
END LOOP;
COMMIT;
END;