0% found this document useful (0 votes)
31 views2 pages

Dbms 6th

dbms 5th

Uploaded by

Irene Thomas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views2 pages

Dbms 6th

dbms 5th

Uploaded by

Irene Thomas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

-- Create the N_Roll_Call table

CREATE TABLE N_Roll_Call (

student_id NUMBER PRIMARY KEY,

student_name VARCHAR2(100),

class_id NUMBER

);

-- Insert sample data into N_Roll_Call

INSERT INTO N_Roll_Call (student_id, student_name, class_id) VALUES (1, 'Alice', 101);

INSERT INTO N_Roll_Call (student_id, student_name, class_id) VALUES (2, 'Bob', 102);

INSERT INTO N_Roll_Call (student_id, student_name, class_id) VALUES (3, 'Charlie', 101);

-- Create the O_Roll_Call table

CREATE TABLE O_Roll_Call (

student_id NUMBER,

student_name VARCHAR2(100),

class_id NUMBER

);

-- PL/SQL Block to merge data

DECLARE

CURSOR roll_call_cursor IS

SELECT student_id, student_name, class_id

FROM N_Roll_Call;

BEGIN

FOR roll_call_row IN roll_call_cursor LOOP

DECLARE

v_count INTEGER;

BEGIN

SELECT COUNT(*)
INTO v_count

FROM O_Roll_Call

WHERE student_id = roll_call_row.student_id

AND class_id = roll_call_row.class_id;

IF v_count = 0 THEN

INSERT INTO O_Roll_Call (student_id, student_name, class_id)

VALUES (roll_call_row.student_id, roll_call_row.student_name, roll_call_row.class_id);

ELSE

DBMS_OUTPUT.PUT_LINE('Record for student_id ' || roll_call_row.student_id || ' already


exists. Skipping...');

END IF;

END;

END LOOP;

COMMIT;

DBMS_OUTPUT.PUT_LINE('Data merge completed successfully.');

EXCEPTION

WHEN OTHERS THEN

ROLLBACK;

DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);

END;

You might also like