1

I have two tables Table1, Table2. From Table1 want to transfer data to Table2.

Suppose I have the following query:

Select * 
from Table1 
where UserID between 5 and 10

I want to store above result to Table2. Both tables save same type of columns. How to transfer data from table to table?

If have any questions, please ask.

1 Answer 1

1

following INSERT INTO statement:

INSERT INTO TABLE2 SELECT * FROM TABLE1

Now suppose you do not want to copy all the rows, but only those rows that meet a specific criteria. Say you only want to copy those rows where COL1 is equal to "A." To do this you would just modify the above code to look like this:

INSERT INTO TABLE2 SELECT * FROM TABLE1 WHERE COL1 = 'A'

See how simple it is to copy data from one table to another using the INSERT INTO method? You can even copy a selected set of columns, if you desire, by identifying the specific columns you wish to copy and populate, like so:

INSERT INTO TABLE2 (COL1, COL2, COL3) 
SELECT COL1, COL4, COL7 FROM TABLE1

The above command copies only data from columns COL1, COL4, and COL7 in TABLE1 to COL1, COL2, and COL3 in TABLE2.

1
  • Additionnally if you want to avoid writing queries you can still use the export Wizard (right click on the db in SSMS then Taks menu). You will have to specify the table to which you want to transfer the data.
    – nopol
    Jan 30, 2012 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.