Lill Lansey
Lill Lansey

Reputation: 4915

How to do the equivalent of a 'Tsql select into', into an existing table

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?

Upvotes: 15

Views: 11877

Answers (3)

Wil P
Wil P

Reputation: 3371

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

should work.

Upvotes: 4

Abram Simon
Abram Simon

Reputation: 3269

INSERT INTO table1
SELECT facilabbr, unitname, sortnum FROM table2

Upvotes: 27

Joel Coehoorn
Joel Coehoorn

Reputation: 415931

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

Upvotes: 12

Related Questions