views:

457

answers:

3

using tsql, sqlserver 2005.

I would like insert records from table table2 into an existing table table1 as easily as I could enter it into a new table table1 using:

select facilabbr, unitname, sortnum into table1 from table2

Any ideas?

+3  A: 

Assuming you just want to append and that the columns match up:

INSERT INTO Table1
    SELECT facilabbr, unitname, sortnum FROM table2

If you want to replace and the columns still match:

Truncate Table1
INSERT INTO Table1
    SELECT facilabbr, unitname, sortnum FROM table2

If you want to replace and the columns do not match:

DROP Table1
SELECT facilabbr, unitname, sortnum INTO Table1 FROM table2
Joel Coehoorn
Yes this syntax works, but if you are not doing this ad hoc and are going to re-use the code, it is always a better idea to include the column list in the INSERT portion. I've been working on a series of "bad habits" articles, and this is certainly one of them. http://is.gd/4ucu2
Aaron Bertrand
+2  A: 
INSERT INTO table1
SELECT facilabbr, unitname, sortnum FROM table2
AJ
A: 

INSERT INTO TABLE1 T1 (T1.FIELD1, T1.FIELD2) SELECT (T2.FIELD1, T2.FIELD2) FROM TABLE2 T2 should work.

Wil P