How to insert data from one table to another

  • Hi ,

    I have a table A and B and Table A already has some data i just want to include my data (which is in table B)into A.

    Can anyone tell me how i should add the data into table A.

    Appreciate the help

  • If you actually want to insert the data from table A into table B you can do something like this (assuming that the structure of both tables is the same)

    INSERT dbo.TableB ( Column1name, Column2name, Column3name, Column4name, Column5name )

    SELECT Column1name, Column2name, Column3name, Column4name, Column5name

    FROM dbo.tableA

    If the structures are not the same then you will need to match the columns up in order in the select statement and insert portion.

    If you just want to see the data together in the same query, again assuming same structure:

    SELECT Column1name, Column2name, Column3name, Column4name, Column5name

    FROM dbo.tableA

    UNION ALL

    SELECT Column1name, Column2name, Column3name, Column4name, Column5name

    FROM dbo.tableB

    CEWII

  • Structures are the same,

    If i use first method do i lose the data in table A,since i want to add the rows from table B to table A.

  • No, unless you execute a delete statement you end up with the data that was just selected in both places. You do need to keep in mind primary keys, if the key of A conflicts with the key of B the insert will fail.

    CEWII

  • Thank you

  • You are welcome.

    CEWII

Viewing 6 posts - 1 through 5 (of 5 total)

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