Reputation: 63
I have 2 tables, TABLE1 with columns col1, col2, col3, col4 and col5 in a database on server1. I have TABLE2 with the same columns and almost same data as TABLE1 excluding columns col2 and col5 in a database on server2. TABLE2 has the same data in col1, col3 and col4 as TABLE1. Now I know I can just create a new table and use the import/export feature of sql-server. But this would not be feasible if the tables had tons of data and more columns.
So my question is there any way to insert data in col2 and col5 to TABLE2 through any other means? If this is possible I would like to make sure the data that is being inserted is col2 and col5 in TABLE2 matches to that of TABLE1 meaning if I have col1 with data lets say 5 and col2 has data xyz, would like this data to be inserted at this row. (FYI col1 is the primary key on both tables).
Both servers are sql server 2008r2.
Upvotes: 0
Views: 182
Reputation: 66
Provided the two SQL servers are on the same network, make a linked server on server2.
How to create the linked server for SQL Server 2008 where we have the database from 2000 and 2005
You may need to do an IDENTITY INSERT should you wish to keep the Id columns the same.
How to turn IDENTITY_INSERT on and off using SQL Server 2008?
You would be able to write an INSERT statement along the lines of:
INSERT INTO [server2].[database1].[table1] ( col1, col3, col5 )
SELECT col1, col3, col5 FROM [server1].[database1].[table1]
If you don't want to introduce duplicate keys, use a WHERE NOT EXISTS col1 IN ( ... )
.
If you don't want to introduce duplicate rows, use a EXCEPT
statement.
Hope this helps you.
Upvotes: 1