简体   繁体   English

SQL Server数据迁移失败

[英]SQL Server data migration fail

So we moved our SQL server to a new platform. 因此,我们将SQL Server移至了新平台。 During this move the primary and foreign keys were not transferred. 在此移动过程中,未传输主键和外键。

Is there a way I can script the keys from the old tables and add them to the new tables? 有没有一种方法可以对旧表中的键进行脚本编写并将其添加到新表中?

It doesn't look like they are going to redo it and just have us all the keys manually. 看起来他们不会重做它,只是手动给我们提供了所有键。 Any speedy/safe way to do this is appreciated. 任何快速/安全的方法都可以做到这一点。

SQL Server 2008 SQL Server 2008

Yup, you're going to love this. 是的,您会喜欢上它的。 I have to drop and re-add FK's once in a while (like when I want to change an index that SQL hijacked for FK enforcement), so I wrote a view that will give me the drop and add statements: 我必须不时删除并重新添加FK(例如,当我想更改SQL为FK实施而劫持的索引时),所以我写了一个视图,该视图将为我提供drop和add语句:

select *, addqry = 'ALTER TABLE '+FKtable+' WITH ' + case when is_not_trusted = 1 then 'NO' else '' end + 'CHECK'
                    + ' ADD  CONSTRAINT ['+FK+'] FOREIGN KEY ('+FKcol+') '
                    + ' REFERENCES '+PKtable+' ('+PKcol+')'+' ON UPDATE '+onUpdate+' ON DELETE '+onDelete
                    + case when is_not_for_replication = 1 then ' NOT FOR REPLICATION' else '' end + ';'
                    + case when is_disabled = 1 then '  ALTER TABLE '+FKtable+' NOCHECK CONSTRAINT ['+FK+'];' else '' end
    ,dropqry = 'ALTER TABLE '+FKtable+' DROP ['+FK+'];'
from (
    select   PKtable        = object_schema_name(f.referenced_object_id)+'.'+object_name(f.referenced_object_id)
        ,PKtbl      = object_name(f.referenced_object_id)
        ,PKcol      = pc.name
        ,FKtable        = object_schema_name(f.parent_object_id)+'.'+object_name(f.parent_object_id)
        ,FKtbl      = object_name(f.parent_object_id)
        ,colseq     = fk.constraint_column_id
        ,FKcol      = fc.name
        ,FK     = object_name(f.object_id)
        ,onUpdate   = replace(f.update_referential_action_desc collate SQL_Latin1_General_CP1_CI_AS, '_', ' ')
        ,onDelete   = replace(f.delete_referential_action_desc collate SQL_Latin1_General_CP1_CI_AS, '_', ' ')
        ,f.is_disabled
        ,f.is_not_trusted
        ,f.is_not_for_replication
    from sys.foreign_key_columns as fk
        join sys.foreign_keys f on fk.constraint_object_id = f.object_id
        join sys.columns as fc on f.parent_object_id = fc.object_id and fk.parent_column_id = fc.column_id
        join sys.columns as pc on f.referenced_object_id = pc.object_id and fk.referenced_column_id = pc.column_id
) t

Since you're dealing with primary key and foreign keys, I would use a (free) tool like SQL Server Database Tools (SSDT). 由于您正在处理主键和外键,因此我将使用(免费)工具,例如SQL Server数据库工具(SSDT)。 This is a Microsoft tool that integrates with Visual Studio (if you have it - you don't need it). 这是一个与Visual Studio集成的Microsoft工具(如果有,则不需要)。 It will synch a target schema with a source schema. 它将目标模式与源模式同步。 It does essentially what a tool like Red Gat Schema Compare does, except it's free. 它基本上是像Red Gat Schema Compare这样的工具所能做的,除了它是免费的。 And it works very well. 而且效果很好。 Using a tool like this will ensure your entire schema got moved over, not just PKs and FKs. 使用这样的工具将确保移走整个架构,而不仅仅是PK和FK。

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

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