简体   繁体   English

SQL Server 2005,如何将数据库关系图复制到另一台服务器

[英]SQL Server 2005, how to copy a Database Diagram to another server

Is there a way to copy a SQL Server Database Diagram to another server? 有没有办法将SQL Server数据库图复制到另一台服务器?

I found this and modified it sightly to copy only one diagram: 我发现了这一点,并对其进行了修改,仅复制了一张图:

INSERT INTO dbB.dbo.sysdiagrams 
SELECT [name],[principal_id],[version],[definition]
FROM dbA.dbo.sysdiagrams
Where name = 'MyDiagramName'

But I need to copy it to another Server (Development to Production). 但是我需要将其复制到另一台服务器(开发到生产)。

I don't want to create a linked server to do this. 我不想创建链接服务器来执行此操作。 ( Updated explanation ) The reason behind that is that I want to include the diagram in a upgrade script. 更新的说明 )其背后的原因是我想将图表包含在升级脚本中。 I made changes to the database to support a new version (new tables, etc) and I want the diagram be be part of the upgrade script. 我对数据库进行了更改以支持新版本(新表等),并且希望该图成为升级脚本的一部分。 so it's best if i could put that in a SQL script. 因此,最好将其放入SQL脚本中。 If a got a separated file to manually import afterward, it could do the job, but it not the best. 如果有一个单独的文件随后可以手动导入,则可以完成此任务,但这不是最佳选择。

So i need to 'save' the diagram definition to a file somehow to restore it on the other server. 因此,我需要将图定义“保存”到文件中以某种方式在另一台服务器上还原。

Just found this solution . 刚找到这个解决方案

In this article, There's the code to create the Stored Procedure that generate a SQL Server Script to recreate the diagrams. 在本文中,有用于创建存储过程的代码,该存储过程生成SQL Server脚本以重新创建图。 So you just save the output of the Stored Procedure in a .SQL file and run it on the other server. 因此,您只需将存储过程的输出保存在.SQL文件中,然后在另一台服务器上运行它即可。

The problem is to convert Varbinary To a String (Varchar) in Hex in order to be able use it in a insert/update query. 问题是将Varbinary转换为十六进制字符串(Varchar),以便能够在插入/更新查询中使用它。 But it's well explained in the link... 但这在链接中有很好的解释...

First : Create one Link Server From Source Server inside Destination Server . 首先:从Destination Server内部的Source Server创建一个链接Destination Server

For create Link Server use this Link 要创建Link Server使用此链接

Second : Use This 第二:使用此

USE DestinationDatabase

DELETE  sysDiagrams
WHERE   name IN ( SELECT    name
              FROM      <LinkServerName>.SourceDatabase.dbo.sysDiagrams )

SET IDENTITY_INSERT sysDiagrams ON

INSERT  sysDiagrams
    ( name ,
      principal_id ,
      diagram_id ,
      version ,
      definition
    )
    SELECT  name ,
            principal_id ,
            diagram_id ,
            version ,
            definition
    FROM    <LinkServerName>.SourceDatabase.dbo.sysDiagrams

SET IDENTITY_INSERT sysDiagrams OFF

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

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