views:

910

answers:

2

Hello everyone,

I am using SQL Server 2008 Enterprise. I need to import all data from Server/Instance "Server Foo", Database "Foo" and table "Foo", into destination Server/Instance "Server Goo", Database "Goo" and table "Goo". Table Foo and Table Goo are of the same schema. If the same row exists for table Goo, I want to keep the origin data in Goo and ingore the import row in Foo (table Foo and table Goo both has a uniqueidentifier type column called CustomerID which acts as primary key and clustered index), just like ignore duplicate key does.

I am looking for simple and reliable ways to write T-SQL to solve data export/import issue. Any reference samples?

EDIT 1:

I have tried the below solution using MERGE, but met with the following error from SQL Server Management Studio. Any ideas what is wrong?

Some more information:

LabTest1\SQLServer2008 => Server\Instance name; OrderDB => DB name; dbo => schema name; Orders => Table name.

merge into [dbo].[Orders] as Target
using "LabTest1\SQLServer2008.OrderDB.dbo.Orders" as source
on target.Hash = source.Hash
when not matched then
INSERT     ([Hash]
           ,[Order]
           ,[Name]
           ,[CreationTime]
           ,[Description])
     VALUES
     (
     source.[Hash], source.[Order], source.[Name], source.[CreationTime], source.[Description]
     )
when MATCHED then
;

Error message:

Msg 102, Level 15, State 1, Line 16 Incorrect syntax near ';'.

thanks in advance, George

+3  A: 
marc_s
Thanks Marc, for the linked server, how could I fill the "Linked Server" value in the New Linked Server wizard? I tried to input machine name but failed.
George2
Did you pick the "SQL Server" option on the "New Linked Server / General" page? Just enter the name of the SQL Server instance you want to link to. On the other pages of the wizard, you might also need to specify credentials to connect to that server instance
marc_s
Thanks Marc, if my source server name is foo, and instance name is fooinstance, I should enter foo\fooinstance? What is your recommended setting for credential?
George2
Yes, enter "foo\fooinstance" - that should work. As for credentials - that's totally up to you. Either use an existing account you already have (e.g. one that the app connects to the database with), or create a specific one - whatever works for you
marc_s
Thanks Marc, connection success. But failed to execute your merge statements, after "WHEN MATCHED THEN", it says syntax near ';' is wrong. I have posted my current TSQL and related error into EDIT1 section of my original post, any ideas what is wrong?
George2
Then just delete out the WHEN MATCHED THEN line alltogether - you're not doing anything if the source and target entries match, right? You could issue an UPDATE statement here
marc_s
Thanks Marc, your solution works!
George2
+1  A: 

If you are using the Enterprise edition of SQL Server then why make your project difficult for yourself by using raw T-SQL code?

You can achieve your objectives in a more simple and robust manner by using SQL Server Integration Services (SSIS), a technology specifically designed to perform ETL tasks and is of course included with your edition of SQL Server.

Pre-built components already exist to process data in the manner which you desire and you have the added bonus of being able to easily incorporate auditing and logging capabilities into your solution.

What you are looking to achieve could be accomplished via pure T-SQL coding, however you would be making life more difficult for yourself in the process and the resulting solution would, in my opinion, most likely be cumbersome and less elegant.

John Sansom
Hi John, the SSIS URL cannot be opened. :-)
George2
@George: Probably your browser, works for me.
John Sansom