Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert table values from one database to another database? [closed]

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.

like image 749
naveenkumar Avatar asked Aug 17 '10 12:08

naveenkumar


People also ask

How do I copy data from one table of one database to another table of another database in SQL?

Right-click on the database name, then select "Tasks" > "Export data..." from the object explorer. The SQL Server Import/Export wizard opens; click on "Next". Provide authentication and select the source from which you want to copy the data; click "Next". Specify where to copy the data to; click on "Next".


2 Answers

How about this:

USE TargetDatabase GO  INSERT INTO dbo.TargetTable(field1, field2, field3)    SELECT field1, field2, field3      FROM SourceDatabase.dbo.SourceTable      WHERE (some condition) 
like image 85
marc_s Avatar answered Oct 11 '22 03:10

marc_s


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

1 Creating Linked Servers {if needs} (SQL server 2008 R2 - 2012) http://technet.microsoft.com/en-us/library/ff772782.aspx#SSMSProcedure

2 configure the linked server to use Credentials a) http://technet.microsoft.com/es-es/library/ms189811(v=sql.105).aspx

EXEC sp_addlinkedsrvlogin 'NAMEOFLINKEDSERVER', 'false', null, 'REMOTEUSERNAME', 'REMOTEUSERPASSWORD'

-- CHECK SERVERS

SELECT * FROM sys.servers 

-- TEST LINKED SERVERS

EXEC sp_testlinkedserver N'NAMEOFLINKEDSERVER' 

INSERT INTO NEW LOCAL TABLE

SELECT * INTO NEWTABLE FROM [LINKEDSERVER\INSTANCE].remoteDATABASE.remoteSCHEMA.remoteTABLE 

OR

INSERT AS NEW VALUES IN REMOTE TABLE

INSERT INTO    [LINKEDSERVER\INSTANCE].remoteDATABASE.remoteSCHEMA.remoteTABLE SELECT  * FROM    localTABLE 

INSERT AS NEW LOCAL TABLE VALUES

INSERT INTO    localTABLE SELECT  * FROM    [LINKEDSERVER\INSTANCE].remoteDATABASE.remoteSCHEMA.remoteTABLE 
like image 26
OzzKr Avatar answered Oct 11 '22 03:10

OzzKr