15

Is there a way to copy a SQL Server Database Diagram to another 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. 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.

DavRob60
  • 3,517
  • 7
  • 34
  • 56

4 Answers4

5

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. So you just save the output of the Stored Procedure in a .SQL file and run it on the other server.

The problem is to convert Varbinary To a String (Varchar) in Hex in order to be able use it in a insert/update query. But it's well explained in the link...

DavRob60
  • 3,517
  • 7
  • 34
  • 56
2

First : Create one Link Server From Source Server inside Destination Server.

For create Link Server use this Link

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
Community
  • 1
  • 1
Ardalan Shahgholi
  • 11,967
  • 21
  • 108
  • 144
0

I have used the following with success.

  1. Create a temporary table in the source base to copy the binary diagram definition(s) to a table that you can generate a script from (because SysDiagrams isn't an option in Generate Scripts wizard) :
    CREATE TABLE CopySysDiagrams (
        name NvarChar(200), principal_id INT, diagram_id INT PRIMARY KEY,
        version INT, [definition] varBinary(MAX) )
    
    INSERT INTO CopySysDiagrams SELECT * from sysdiagrams   
        -- optional WHERE clause if you only need one diagram
  1. Use Tasks... Generate Scripts... and the script it produces will contain the binary definitions of the diagram(s) in 0x format

  2. Use a script like the following in the target base (where 19 in this case is first free ID because there were 18 before), copying the binary data 0x string into here:

    SET identity_insert sysDiagrams ON

    INSERT sysDiagrams (name, principal_id, diagram_id, version, definition) 
        VALUES  ('Your Diagram Name', 1, 19, 1, 
                    0xTHEBINARYDATA
                 )

    SET identity_insert sysDiagrams OFF
AjV Jsy
  • 5,799
  • 4
  • 34
  • 30
-1

This tutorial/example will explain it

http://www.sharpdeveloper.net/content/archive/2008/04/19/copy-a-database-diagram-to-another-database.aspx

greektreat
  • 2,329
  • 3
  • 30
  • 53