views:

268

answers:

4

I want to update a static table on my local development database with current values from our server (accessed on a different network/domain via VPN). Using the Data Import/Export wizard would be my method of choice, however I typically run into one of two issues:

  1. I get primary key violation errors and the whole thing quits. This is because it's trying to insert rows that I already have.
  2. If I set the "delete from target" option in the wizard, I get foreign key violation errors because there are rows in other tables that are referencing the values.

What I want is the correct set of options that means the Import/Export wizard will update rows that exist and insert rows that do not (based on primary key or by asking me which columns to use as the key).

How can I make this work? This is on SQL Server 2005 and 2008 (I'm sure it used to work okay on the SQL Server 2000 DTS wizard, too).

A: 

One option is to download the data to a new table, then use commands similar to the following to update the target:

update target set
    col1 = d.col1,
    col2 = d.col2
from downloaded d
inner join target t on d.pk = t.pk

insert into target (col1, col2, ...)
select (d.col1, d.col2, ...) from downloaded d
where d.pk not in (select pk from target)
Neil Barnwell
A: 

If you disable the FK constrains during the 2nd option - and resume them after finsih - it will work.

But if you are using identity to create pk that are involves in the FK - it will cause a problem, so it works only if the pk values remains the same.

Dani
Yes, I suppose there is that. It could leave me with inconsistent data if some values have been removed on the server though, which is more work.
Neil Barnwell
I was editing for that reason. if you change values, you might need to have a smarter way to do the job, or if it's ok - just run over the entire dev database - that's what I do. I upgrade the production database, and then before starting a new developing cycle, I copy the production database completely on the development database.
Dani
+1  A: 

I'm not sure you can do this in management studio. I have had some good experiences with RedGate SQL Data Compare in synchronising databases, but you do have to pay for it.

Robin M
This would be my favoured approach too as it automatically does what Dani is suggesting you do.
Damian Powell
I don't have SQL Compare, and I'd be concerned how "one-way" it is. Mind you, I suppose if you connect to the source db with a readonly account you can't do any damage.
Neil Barnwell
The user interface is designed to make it clear which way you're synchronising but as you said you could use a readonly account one end. BTW SQL Compare is for schema and SQL Data Compare is for data.
Robin M
+1  A: 

The SQL Server Database Publishing Wizard can export a set of sql insert scripts for the table that you are interested in. Just tell it to export just data and not schema. It'll also create the necessary drop statements.

Mike Killey
Drop statements or Delete statements?
Neil Barnwell
Just tried that application and it crashes on my machine. Server 2008 x64 running Sql server 2008. Reading the documentation it looks like its for SQL Server 2000 and 2005.
Simon Hughes
Haven't used it in a while, but ISTR that it generates drop statements, which is probably not what you're looking for. In which case you'd be better off with one of the other tools available as suggested.
Mike Killey