Sql-server – how to transfer data from table to table

sql serversql-server-2008

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.

Best Answer

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.