Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export a SQL Server 2008 Database Diagram to another DB?

I use the handy Database Diagramming tool in SQL Server 2008 for creating and managing relationships. I have exported the sourceDB to the destinationDB but the diagram doesn't come across.

I am looking around trying to figure out how to export just the diagram I have in one database to another... This online KB article fails since select * from dtproperties doesn't exist anymore.

like image 272
Ash Machine Avatar asked Oct 25 '10 23:10

Ash Machine


People also ask

How do I export a SQL Server database diagram?

Right click on the database containing the diagrams. Click on All Tasks then on "Export data". The Import / Export option will be displayed, click Next. Define the source and then click Next.

How do I export a database in SQL Server 2008?

Right click on your database and hover on Tasks, you got another dropdown, Select Back Up. You got a pop-up like this. Click on OK button. You got your exported database.

Where are SQL Server diagrams stored?

Where the database diagrams are stored? Actually the diagrams are stored in a table called “sysdiagrams”. The sysdiagrams contains a column named “definition”. The structure of the diagram is stored in the “definition” column using varbinary data type.


3 Answers

@Ash I was having the same problem. Here's what we did to get around it...

It seems that System Diagrams are stored within the "sysdiagrams" table. So the first thing you need to do is determine the diagram_id of the Diagram you wish to copy. Run the following query to list them all. ** Note you need to replace "SourceDB" with the name of your database.

-- List all database diagrams
SELECT * FROM [SourceDB].[dbo].sysdiagrams

Then you can use INSERT to duplicate the diagram from one database to another as follows. ** Note again replace "SourceDB" with the name of the Database containing the existing diagram and "DestinationDB" with the name of the Database you wish to copy to. Also @SourceDiagramId should be set to the id retrieved above.

-- Insert a particular database diagram
DECLARE @SourceDiagramId int = 1

INSERT INTO [DestinationDB].[dbo].sysdiagrams
SELECT [name],diagram_id , version,definition from [SourceDB].[dbo].sysdiagrams
WHERE diagram_id = @SourceDiagramId

Then you need to set the "principal_id" to 1 manually.

-- Update the principal id (no idea why, but it set the owner as some asp_net user
UPDATE [DestinationDB].[dbo].sysdiagrams
SET principal_id = 1

This worked for us it seems pretty hacky especially since the Diagram is stored entirely in a single binary field "definition".

Answer comes from:
http://www.dotnetspider.com/resources/21180-Copy-or-move-database-digram-from-for.aspx

like image 194
Justin Avatar answered Nov 09 '22 09:11

Justin


You can get rid of the UPDATE statement by fixing your INSERT statement - specifically the select portion. You are inserting the diagram_id column into the principal_id column (diagram_id is an identity).

Change it to:

DECLARE @SourceDiagramId int = 1
INSERT INTO [DestinationDB].[dbo].sysdiagrams
SELECT [name],principal_id,version,definition from [SourceDB].[dbo].sysdiagrams
WHERE diagram_id = @SourceDiagramId

And presto, it's all in there right the first time.

like image 23
WayneS Avatar answered Nov 09 '22 07:11

WayneS


This generates an import string:

SELECT
    'DECLARE @def AS VARBINARY(MAX) ; ' +
    'SELECT @def = CONVERT(VARBINARY(MAX), 0x' + CONVERT(NVARCHAR(MAX), [definition], 2) + ', 2) ;' +
    ' EXEC dbo.sp_creatediagram' +
        ' @diagramname=''' + [name] + ''',' +
        ' @version=' + CAST([version] AS NVARCHAR(MAX)) + ',' +
        ' @definition=@def'
    AS ExportQuery
FROM
    [dbo].[sysdiagrams]
WHERE
    [name] = '' -- Diagram Name

Next, you run the generated string in other DB.

As PROCEDURE:

-- =============================================
-- Author:      Eduardo Cuomo
-- Description: Export Database Diagrama to SQL Query
-- =============================================
CREATE PROCEDURE [dbo].[Sys_ExportDatabaseDiagram]
    @name SYSNAME -- Diagram Name
AS

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

SELECT
    'DECLARE @def AS VARBINARY(MAX) ; ' +
    'SELECT @def = CONVERT(VARBINARY(MAX), 0x' + CONVERT(NVARCHAR(MAX), [definition], 2) + ', 2) ; ' +
    ' EXEC dbo.sp_creatediagram' +
        ' @diagramname=''''' + [name] + ''''',' +
        ' @version=' + CAST([version] AS NVARCHAR(MAX)) + ',' +
        ' @definition=@def'
    AS ExportQuery
FROM
    [dbo].[sysdiagrams]
WHERE
    [name] = @name
like image 26
Eduardo Cuomo Avatar answered Nov 09 '22 09:11

Eduardo Cuomo