简体   繁体   English

SQL Server 2008:使用主键更新表

[英]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) TableName: ParentTableParentID是自动递增号和主键)

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) TableName: ParentTableParentID是自动递增号和主键)

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. 首先,请注意ParentID是自动增量号,也是主键。 There are also relationship between the ParentTable with others. ParentTable与其他人之间也存在关系。

On the DEV server, I changed name from "A" to "Z" with parentID# 1 , and I also deleted the record for parentID# 2 . 在DEV服务器上,我将parentID# 1名称从“ A”更改为“ Z”,并且还删除了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. 由于已在DEV服务器上删除了parentID# 2记录,因此假设我将从与生产服务器上与ParentID# 2关联的其他表中删除所有记录。

What is a good way to update the ParentTable on production that looks exactly same as the ParentTable on the DEV server? 什么是在生产环境上更新与DEV服务器上的ParentTable完全相同的生产中更新ParentTable的好方法?

Thanks in advance 提前致谢

Assuming that the two databases are on the same server (or linked server). 假设两个数据库位于同一服务器(或链接服务器)上。 You could do an MERGE query. 您可以执行MERGE查询。 If not you could update the values with an UPDATE query. 如果不是,则可以使用UPDATE查询更新值。

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. 目前尚不清楚这是您的情况,还是只想添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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