Inserting Results of a Query into a Table

by Dave Andrews

Sometimes its necessary to populate an existing table with the results of a query. This can be done very easily with an INSERT statement combined with a SELECT.

A typical insert query looks like this.

1
2
3
4
5
6
7
8
9
10
INSERT INTO Person 
     (Name, 
      Address, 
      Phone
     )
VALUES 
     ('Test Guy', 
      '123 Test St', 
      '555-5555'
     )

This works fine for just one row. But what if we had another table which contained the information we needed, and had 100000 rows that we wanted to copy over? Let’s just combine our INSERT with a SELECT!

1
2
3
4
5
6
7
8
9
10
11
INSERT INTO Person 
     (Name, 
      Address, 
      Phone
     )
SELECT
     Name,
     Address,
     Phone
FROM
     TableThatHasValues

It’s that easy! The SELECT query will execute and the results of that select query will be piped into the INSERT command, inserting every single row.