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.
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.
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 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.
@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
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.
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.
-- =============================================
-- 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With