Profile

Saturday, 25 June 2011

SQL: loading data from one table to another using INSERT INTO

Here is a quick script to load data from one table to another one. You can just copy the data or add optional condition with the WHERE statement. The syntax works like this

INSERT INTO dbo.targettable ( [column1],[columnN])
SELECT [column1],[columnN]
FROM dbo.sourcetable GO

INSERT INTO AdventureWorksDW2008.dbo.FactInternetSales
([RevisionNumber],[OrderQuantity],[UnitPrice]
,[ExtendedAmount],[UnitPriceDiscountPct],[DiscountAmount]
,[ProductStandardCost],[TotalProductCost])
SELECT
[RevisionNumber],[OrderQuantity],[UnitPrice]
,[ExtendedAmount],[UnitPriceDiscountPct],[DiscountAmount]
,[ProductStandardCost],[TotalProductCost]
FROM AdventureWorksDW.dbo.[FactInternetSales]
WHERE OrderQuantity > 200
GO