swap tables

  • Hi,

    I have two tables

    Table1 Table2

    1 5

    2 6

    3 7

    4 8

    How to swap table2 values in Table1 & Table1 to Table2...?

  • karunakar2351 (9/12/2014)


    Hi,

    I have two tables

    Table1 Table2

    1 5

    2 6

    3 7

    4 8

    How to swap table2 values in Table1 & Table1 to Table2...?

    It depends on what exactly you want to do, you can rename the tables or you can swap the values.

    For renaming use sp_rename, Table1 to Table1_temp, Table_2 to Table1 and Table1_temp to Table2

    For the swap, select * into #temp1 from Table1, Truncate Table1, insert into Table1 select * from Table2, Truncate Table2, insert into Table2 select * from #temp1, drop #temp1

    😎

  • This sounds an aweful lot like an interview question.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (9/12/2014)


    This sounds an aweful lot like an interview question.

    Yep, sure smells like one

    😎

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

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