简体   繁体   中英

SQL Server 2008 : update the table with primary key

I modified the table from the development database and want to update the same table on production database with new data from the development server. Please let me explain this...

For example,

Development Table:

TableName: ParentTable ( ParentID is auto increment# and primary key)

ParentID        Name    Value
------------------------------
1               Z       A-1    (change value to Z)
2               B       B-1    (will delete the record completely)
3               C       C-1
4               D       D-1
5               E       E-1
6               F       F-1
7               G       G-1
8               H       H-1
9               I       I-1

Production table:

TableName: ParentTable ( ParentID is auto increment# and primary key)

ParentID        Name    Value
-----------------------------
1               A       A-1 
2               B       B-1  
3               C       C-1
4               D       D-1
5               E       E-1
6               F       F-1
7               G       G-1
8               H       H-1
9               I       I-1

First, note that ParentID is auto increment# and also a primary key. There are also relationship between the ParentTable with others.

On the DEV server, I changed name from "A" to "Z" with parentID# 1 , and I also deleted the record for parentID# 2 .

Since parentID# 2 record has been deleted on the DEV server, assume that I will delete any records from other tables that associated ParentID# 2 on production server.

What is a good way to update the ParentTable on production that looks exactly same as the ParentTable on the DEV server?

Thanks in advance

Assuming that the two databases are on the same server (or linked server). You could do an MERGE query. If not you could update the values with an UPDATE query.

MERGE INTO ProductionDB.Schema.ParentTable A
USING DevelopmentDB.Schema.ParentTable B
ON A.ParentID = B.ParentID
WHEN MATCHED THEN
UPDATE SET A.Name = B.Name, A.Value = B.Value;

Of course replace your server/database names. But this an rough example of what you're looking for.

I'd probably try something like:

declare @parentOffset int
begin transaction

select @parentOffset = max(ParentID) from Prod.dbo.ParentTable (holdlock)

set identity_insert Prod.dbo.ParentTable on

insert into Prod.dbo.ParentTable ( ParentID , Name , Value )
select Dev.dbo.ParentTable.ParentID + @parentOffset , Dev.dbo.ParentTable.Name , Dev.dbo.ParentTable.Value
from Dev.dbo.ParentTable.ParentTable

set identity_insert Prod.dbo.ParentTable off

insert into Prod.dbo.ChildTable ( ParentID , SomeOtherField )
select Dev.dbo.ChildTable.ParentID + @parentOffset , Dev.dbo.ChildTable.SomeOtherField

commit transaction

Obviously, test it out against an offline copy of things before you pull the trigger in production, and would put in some error handling, but this should get the job done. It won't test to see if there are already existing values in the table, but you could handle that with a slight modification & going through a temp table. It's unclear if that's your situation, or whether you just want to add.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM