How to insert table values from one database to another database?

naveenkumar picture naveenkumar · Aug 17, 2010 · Viewed 298.8k times · Source

I want a query to insert records from one table to another table in a different database if the destination table already exists, it should append the records at the end of the table.

Answer

marc_s picture marc_s · Aug 17, 2010

How about this:

USE TargetDatabase
GO

INSERT INTO dbo.TargetTable(field1, field2, field3)
   SELECT field1, field2, field3
     FROM SourceDatabase.dbo.SourceTable
     WHERE (some condition)