Prgm5 & PRGM 6
Prgm5 & PRGM 6
Declare the variables,Open the cursor & extract the values from the cursor.
Close the cursor. Employee(E_id,E_name, Age, Salary)
Solution:
STEP1: Create table Employee
CREATE TABLE Employee (
E_id INT,
E_name VARCHAR(255),
Age INT,
Salary DECIMAL(10, 2)
);
OUTPUT:
Table created
OUTPUT:
Statement Processed.
STEP 3: Create a procedure
CREATE OR REPLACE PROCEDURE fetch_employee_data
IS
emp_id Employee.E_id%TYPE;
emp_name Employee.E_name%TYPE;
emp_age Employee.Age%TYPE;
emp_salary Employee.Salary%TYPE;
CURSOR emp_cursor IS
SELECT E_id, E_name, Age, Salary
FROM Employee;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO emp_id, emp_name, emp_age, emp_salary;
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id || ', Name: ' || emp_name
|| ', Age: ' || emp_age || ', Salary: ' || emp_salary);
END LOOP;
CLOSE emp_cursor;
END;
/
OUTPUT:
Procedure created.
STEP 4: Run the procedure
BEGIN
fetch_employee_data;
END;
OUTPUT:
Employee ID: 1, Name: Samarth, Age: 30, Salary: 50000
Employee ID: 2, Name: Ramesh Kumar, Age: 25, Salary: 45000
Employee ID: 3, Name: Seema Banu, Age: 35, Salary: 62000
Employee ID: 4, Name: Dennis Anil, Age: 28, Salary: 52000
Employee ID: 5, Name: Rehman Khan, Age: 32, Salary: 58000
Statement processed.
Program 6
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.
Solution:
To accomplish this task in MySQL, we can use a stored procedure with a
parameterized cursor to merge data from one table (N_RollCall) into another table
(O_RollCall) while skipping existing data. We’ll iterate through the records of
N_RollCall and insert them into O_RollCall only if they do not already exist.
Step 1: First, let’s create the N_RollCall and O_RollCall tables with similar structure:
OUTPUT:
Table created
OUTPUT:
Table created
OUTPUT:
Statement Processed.
Begin
INSERT INTO N_RollCall VALUES(1, 'Shivanna', '08-15-1995');
INSERT INTO N_RollCall VALUES(2, 'Bhadramma','03-22-1998');
INSERT INTO N_RollCall VALUES(3, 'Cheluva', '12-10-1990');
INSERT INTO N_RollCall VALUES(4, 'Devendra', '05-18-2000');
INSERT INTO N_RollCall VALUES(5, 'Eshwar', '09-03-1997');
end;
OUTPUT:
Statement Processed.
OUTPUT:
BEGIN
-- Open the cursor
OPEN n_cursor;
IF v_count = 0 THEN
-- Insert the record into O_RollCall
INSERT INTO O_RollCall (student_id, student_name, birth_date)
VALUES (n_id, n_name, n_birth_date);
END IF;
END LOOP;
OUTPUT:
Procedure created.
Begin
merge_rollcall_data;
End;
OUTPUT:
Statement Processed.