Monday, August 6, 2012

Ways to Insert data from one table to another - SQL Server

Method 1 :  INSERT INTO SELECT


Use this method if the destination table is already created in the destination database.  
USE  MyDb
GO


INSERT INTO DestinationTable(FirstName, LastName)

SELECT FirstName, LastName

FROM SourceDatabase.SourceTable
WHERE condition= 1


Method 2 :  SELECT INTO


Use this method if the destination table is not already created in the destination database. The SELECT statement would create the new table with same data types in the destination database along with copying the data.

SELECT FirstName, LastName


INTO DestinationTable

FROM SourceDatabase.SourceTable

WHERE condition= 1

In this case new table 'DestinationTable' will be created with same data type as the selected columns.


No comments:

Post a Comment