Cursor to fetch record from two table and insert to another

  • Hi

    I have to fetch record from two table and insert to the third one with the help of cursor. Please help. below is the table

    Table 1:

    CREATE TABLE AA (

    id INT PRIMARY KEY,

    first_name VARCHAR(50),

    last_name VARCHAR(50),

    Subject VARCHAR(30) )

    INSERT INTO AA(id, first_name, last_name, Subject)VALUES (10001, 'Atul', 'Upadhyay','Computer Science')

    INSERT INTO AA (id, first_name, last_name, Subject)VALUES (10002, 'Nandita', 'Upadhyay','Computer Science')

    INSERT INTO AA (id, first_name, last_name, Subject)VALUES (10003, 'Mitesh', 'Gupta','Science')

    INSERT INTO AA (id, first_name, last_name, Subject)VALUES (10004, 'Aditya', 'Jha','Computer Science')

    INSERT INTO AA (id, first_name, last_name, Subject)VALUES (10005, 'Sushil', 'Kumar','English')

    INSERT INTO AA (id, first_name, last_name, Subject)VALUES (10006, 'Pratap', 'Singh','Hindi')

    ---------------------------------------

    Table 2 :

    CREATE TABLE BB (

    id INT PRIMARY KEY,

    first_name VARCHAR(50),

    last_name VARCHAR(50),

    Subject VARCHAR(30) )

    INSERT INTO BB(id, first_name, last_name, Subject)VALUES (20001, 'Ankit', 'Anand','Computer Science')

    INSERT INTO BB(id, first_name, last_name, Subject)VALUES (20002, 'Atul', 'Anand','Computer Science')

    INSERT INTO BB(id, first_name, last_name, Subject)VALUES (20003, 'Ashish', 'Gupta','Science')

    INSERT INTO BB(id, first_name, last_name, Subject)VALUES (20004, 'Amod', 'Jha','Computer Science')

    Table 3: to be inserted

    Create Table CC(

    id int,

    first_name VARCHAR(50),

    last_name VARCHAR(50))

    I need the ID,First_name,Last_Name where subject name is Computer Science (With the help of cursor only)

    Thanks

  • Homework, isn't it? 🙂 otherwise there is no reason to use an explicit cursor.

    Do you remember joins from a couple of classes ago? Try populating the cursor joining your two base tables.

    _____________________________________
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at Amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
  • 😀 I can do this by join ..but unable to do this with cursor ...

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply