Monday, July 20, 2015

SQL INSERT INTO SELECT Statement

The other day I ran upon some of my code that I thought might be helpful to some of you.  It is a quick and easy way to use an INSERT INTO statement combined with a SELECT statement in SQL to copy data from one table to another existing table.  Please note that any existing data that is already in the target table is unaffected by this process.

Here I will show you two different ways to accomplish this task.   

The first way will copy all of the columns from tblOriginal table to the tblTarget Table.

INSERT INTO tblTarget
SELECT * FROM tblOriginal;

The second way will copy specific columns from tblOriginal table to the tblTarget Table.

INSERT INTO tblTarget
(Column1, Column2, Column5)
SELECT Column1, Column2, Column5
FROM tblOrigninal;

I hope this helps someone out a bit and saves them a bit of time. I know it helped me out greatly.

If you like this post and want to see more, follow me on my website www.chadcompton.com
Or if you prefer...


No comments:

Post a Comment

Drop me a line.