08 May,2015 by Tom Collins
Question: How can I INSERT into an existing table using a SELECT statement from an existing table?
I don’t want to create a new table , just simply INSERT into an existing table
Answer: To INSERT data from an existing table into an existing table use the INSERT INTO .. SELECT method. This method will take a recordset from an existing table and inserts into another existing table.
INSERT INTO myTable(col1,col2)
SELECT col1,col2 FROM myTableexisting
A common misunderstanding is to use the following pattern, which creates a new table.
SELECT * INTo myNewTable FROM myOLDTable
USE SQL_TOOLS go CREATE TABLE myTest (myID INT, myvalue VARCHAR(100) ) CREATE TABLE myTest2 (myID INT, myvalue VARCHAR(100) ) GO --insert source data into source table INSERT INTO myTest SELECT 1,'myvalue1' UNION SELECT 2, 'myalue2' GO --copy source data into a another table INSERT INTO myTest2 (myID,myValue) SELECT myID,myValue FROM myTest WHERE myID = 1 SELECT * FROM myTest2 DROP TABLE myTest DROP TABLE myTest2
SQL Server - Bulk Insert CSV into a SQL Server table - SQL
This is only a preview. Your comment has not yet been posted.
As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.
Having trouble reading this image? View an alternate.
Posted by: |