Update, select and Insert in one query

  • Hi ,
    I have a following update query and I want to select the updated data from "master table" and insert into a different table called "MainMaster"
    Could anyone please help on this?

    Update master
            set checkin ='N'
            where nos IN (SELECT B.nos FROM master B 
                            INNER JOIN employee C ON C.id = B.id
                            where B.size = 0
                            AND B.checkin ='Y')

    T

  • Use an OUTPUT clause.

    John

  • Perhaps:
    UPDATE [master]
    SET checkin ='N'
    OUTPUT inserted.* --You may need to adjust the * to the correct Column names if your tables do not exactly match
    INTO MainMaster
    WHERE nos IN (SELECT B.nos
                 FROM [master] B
                  INNER JOIN employee C ON C.id = B.id
                 WHERE B.size = 0
                    AND B.checkin ='Y');

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Thom A - Tuesday, April 18, 2017 5:42 AM

    Perhaps:
    UPDATE [master]
    SET checkin ='N'
    OUTPUT inserted.* --You may need to adjust the * to the correct Column names if your tables do not exactly match
    INTO MainMaster
    WHERE nos IN (SELECT B.nos
                 FROM [master] B
                  INNER JOIN employee C ON C.id = B.id
                 WHERE B.size = 0
                    AND B.checkin ='Y');

    Thank you , it worked out :)!

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

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