Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 758
Lill Lansey Avatar asked Oct 21 '09 14:10

Lill Lansey


People also ask

How do I insert a SELECT query result into a table?

From the Query Designer menu, point to Change Type, and then click Insert Results. In the Choose Target Table for Insert Results Dialog Box, select the table to copy rows to (the destination table).

What is SELECT into existing table?

The INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Note: The existing records in the target table are unaffected.

Does SQL SELECT into CREATE TABLE?

SELECT... INTO creates a new table in the default filegroup and inserts the resulting rows from the query into it. To view the complete SELECT syntax, see SELECT (Transact-SQL).

Can I create a table from a SELECT statement?

You can create one table from another by adding a SELECT statement at the end of the CREATE TABLE statement: CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl; MySQL creates new columns for all elements in the SELECT .


2 Answers

INSERT INTO table1
SELECT facilabbr, unitname, sortnum FROM table2
like image 58
Abram Simon Avatar answered Oct 08 '22 12:10

Abram Simon


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
like image 23
Joel Coehoorn Avatar answered Oct 08 '22 13:10

Joel Coehoorn