Beruflich Dokumente
Kultur Dokumente
JJ's Blog
Microsoft Business Intelligence and SharePoint in Action
Startseite
Videos
Books
Versions/Updates
About me
Documentation and communication of custom database models and objects should be a task with high
importance in every project and for every good database developer and administrator! Personally i advise one of
the following approaches for generating MSSQL Database documentation:
OR/AND
2. Use SQL Server Management Studio (SSMS) ootb functionality > Diagram Designer
This post ist all about the second (Diagram Designer) approach:
jochenj.wordpress.com/2011/03/17/howto-create-export-and-import-sql-server-2008-database-diagrams/ 1/10
10/22/13 HowTo Create, Export and Import SQL Server 2008 Database Diagrams | JJ's Blog
With the integrated Diagram Designer you can produce multiple, nice looking diagrams, position tables and
relationships as needed, create annotations
Limitations/Problems:
Unfortunately Diagram Designer misses features for printing or exporting your diagrams to formats like PDF. The
only option possible is to make a screenshoot or use copy diagram to clipboard function. Also its not possible
to copy diagrams from one database to other databases or create diagrams automatically during deploment..!
Solution:
Diagrams are stored in database table [dbo].[sysdiagrams]. So if you would copy this table by hand to
another database, the diagrams will be available in the destination DB.
To make this task easy you will find the code of a stored procedure at the end of this posting! This SP
must be created in your custom database! After that you can call this SP to script-out existing diagrams as insert
statement..Most of the honor goes to this blog-entry i found with google and adapted for my needs:
http://www.conceptdevelopment.net/Database/ScriptDiagram2008/
You have already created my helper SP and have one or multiple diagrams created in your custom database:
jochenj.wordpress.com/2011/03/17/howto-create-export-and-import-sql-server-2008-database-diagrams/ 2/10
10/22/13 HowTo Create, Export and Import SQL Server 2008 Database Diagrams | JJ's Blog
The output will create a exec statement for each diagram included in table dbo.sysdiagrams
If you execute the SP with @Name Parameter it will script-out the diagram to insert statements which can be
executed in any database:
If you execute the generated insert statements in same database or if the diagram already exists in the database, a
new/copy of the diagram will be created with timestamp in name:
jochenj.wordpress.com/2011/03/17/howto-create-export-and-import-sql-server-2008-database-diagrams/ 3/10
10/22/13 HowTo Create, Export and Import SQL Server 2008 Database Diagrams | JJ's Blog
Finally heres the Stored Procedure create statement. Just copy-paste and have fun!
IF @name IS NULL
BEGIN
PRINT WARNING! Stored Procedure was called without defined @Name parameter WARNING! +
CHAR(10)+
Please call one of following TSQL to export a specific diagram +CHAR(10)
DECLARE @y nvarchar(100)
WHILE @cnt <= @cntMax
BEGIN While Loop
WITH AbfrageLoop AS (SELECT row_number() OVER (ORDER BY diagram_id) AS Row,*
FROM dbo.sysdiagrams)
SELECT @y=name FROM AbfrageLoop WHERE Row=@cnt
RETURN (-1)
END
Get PK diagram_id using the diagrams name (which is what the user is familiar with)
SELECT @diagram_id=diagram_id , @size = DATALENGTH(definition) FROM dbo.sysdiagrams
WHERE [name] = @name
IF @diagram_id IS NULL
BEGIN
PRINT /**<error>Diagram name [' + @name + '] could not be found.</error>*/
END
ELSE Diagram exists
BEGIN Now with the diagram_id, do all the work
PRINT /**
PRINT <summary>Restore diagram + @name + </summary>
PRINT <generated> + LEFT(CONVERT(VARCHAR(23), GETDATE(), 121), 16) +
</generated>
PRINT */
PRINT PRINT === Tool_ScriptDiagram2008 restore diagram [' + @name + '] ===
PRINT If the sysdiagrams table has not been created in this database, create it!
END
Target table will now exist, if it didnt before
PRINT @line
jochenj.wordpress.com/2011/03/17/howto-create-export-and-import-sql-server-2008-database-diagrams/ 6/10
10/22/13 HowTo Create, Export and Import SQL Server 2008 Database Diagrams | JJ's Blog
jochenj.wordpress.com/2011/03/17/howto-create-export-and-import-sql-server-2008-database-diagrams/ 7/10