SQL Server - How to copy the database design diagram and paste in MS-Word

Asked By rama chandran
20-Sep-11 02:22 AM
Hi,

i created a database diagram in the database, now i want to copy and paste the diagram in MS-Word ie, i want that database diagram in MS-Word.
  smr replied to rama chandran
20-Sep-11 02:27 AM
HI

follow these steps

I have had this problem before and it is very frustrating as it seems to be printer/printer driver dependent. There are two ways around this problem that I know of.
  • Right click on the diagram and select "copy to clipboard"
  • Get Gadwin Screen Print from http://forums.asp.net/t/1190828.aspx/1?printing+a+database+diagram  and paste the image into Wrd to printit from there.
  Suchit shah replied to rama chandran
20-Sep-11 02:27 AM

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/

Here’s the step-by-step howto:

You have already created my helper SP and have one or multiple diagrams created in your custom database:

http://jochenj.files.wordpress.com/2011/03/image2.png

Now you execute the stored procedure without parameters:

http://jochenj.files.wordpress.com/2011/03/image3.png

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:

http://jochenj.files.wordpress.com/2011/03/image4.png

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:http://jochenj.files.wordpress.com/2011/03/image5.png

 

Finally here’s the Stored Procedure create statement. Just copy-paste and have fun!

 

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = ‘Tool_ScriptDiagram2008′)
BEGIN DROP PROCEDURE dbo.Tool_ScriptDiagram2008
END
GO

CREATE PROCEDURE [dbo].[Tool_ScriptDiagram2008]( @name VARCHAR(128) = NULL)
AS
BEGIN
DECLARE @diagram_id INT
DECLARE @index INT
DECLARE @size INT
DECLARE @chunk INT
DECLARE @line VARCHAR(max)
— Set start index, and chunk ‘constant’ value
SET @index = 1
SET @chunk = 32 — values that work: 2, 6 — values that fail: 15,16, 64

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 @cnt int
DECLARE @cntMax int
SELECT @cnt=1;
SELECT @cntMax =Count(*) FROM dbo.sysdiagrams

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

do something based on the query
print ‘EXEC [dbo].[Tool_ScriptDiagram2008] @Name = ‘ + @y +

SELECT @cnt = @cnt+1
END –While Loop
RETURN (-1)

END

— Get PK diagram_id using the diagram‘s 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!

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = sysdiagrams)
BEGIN
— Create table script generated by Sql Server Management Studio
— _Assume_ this is roughly equivalent to what Sql Server/Management Studio
— creates the first time you add a diagram to a 2008 database

CREATE TABLE [dbo].[sysdiagrams]( [name] [sysname] NOT NULL
,[principal_id] [int] NOT NULL
,[diagram_id] [int] IDENTITY(1,1) NOT NULL
,[version] [int] NULL
,[definition] [varbinary](max) NULL
,PRIMARY KEY CLUSTERED
([diagram_id] ASC)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF)
,CONSTRAINT [UK_principal_name] UNIQUE NONCLUSTERED
([principal_id] ASC,[name] ASC)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF)
)

EXEC sys.sp_addextendedproperty @name=Nmicrosoft_database_tools_support, @value=1 , @level0type=NSCHEMA,@level0name=Ndbo, @level1type=NTABLE,@level1name=Nsysdiagrams

PRINT [sysdiagrams] table was created as it did not already exist
END
— Target table will now exist, if it didnt before

PRINT ‘
SET NOCOUNT ON — Hide (1 row affected) messages
PRINT ‘
DECLARE @newid INT
PRINT ‘
DECLARE @DiagramSuffix varchar (50)
PRINT ‘

PRINT ‘
PRINT Suffix diagram name with date, to ensure uniqueness
PRINT ‘
SET @DiagramSuffix = + LEFT(CONVERT(VARCHAR(23), GETDATE(), 121), 16)
PRINT ‘

PRINT ‘
PRINT Create row for new diagram
— Output the INSERT that _creates_ the diagram record, with a non-NULL [definition],
— important because .WRITE *cannot* be called against a NULL value (in the WHILE loop)
— so we insert 0x so that .WRITE has ‘
something‘ to append to…
PRINT ‘
BEGIN TRY
PRINT ‘
PRINT Write diagram ‘ + @name + ‘ into new row (and get [diagram_id])
SELECT @line = ‘

INSERT INTO sysdiagrams ([name], [principal_id], [version], [definition])
+ ‘
VALUES (‘ + [name] + ‘+@DiagramSuffix, ‘+ CAST (principal_id AS VARCHAR(100))+’,
+CAST (version AS VARCHAR(100))+’
, 0x)
FROM dbo.sysdiagrams WHERE diagram_id = @diagram_id
PRINT @line
PRINT ‘
SET @newid = SCOPE_IDENTITY()
PRINT ‘
END TRY
PRINT ‘
BEGIN CATCH
PRINT ‘
PRINT XxXxX + Error_Message() + XxXxX
PRINT ‘
PRINT XxXxX END Tool_ScriptDiagram2008 – fix the error before running again XxXxX
PRINT ‘
RETURN
PRINT ‘
END CATCH
PRINT ‘

PRINT ‘
PRINT Now add all the binary data…
PRINT ‘
BEGIN TRY

WHILE @index < @size
BEGIN
— Output as many UPDATE statements as required to append all the diagram binary
— data, represented as hexadecimal strings
SELECT @line =
UPDATE sysdiagrams SET [definition] .Write (‘ + ‘
+ UPPER(sys.fn_varbintohexstr (SUBSTRING (definition, @index, @chunk)))
+ ‘
, null, 0) WHERE diagram_id = @newid — index:‘ + CAST(@index AS VARCHAR(100))
FROM sysdiagrams WHERE diagram_id = @diagram_id

PRINT @line
SET @index = @index + @chunk
END
PRINT ‘

PRINT ‘
PRINT === Finished writing diagram id + CAST(@newid AS VARCHAR(100)) + ===
PRINT ‘
PRINT === Refresh your Databases-[DbName]-Database Diagrams to see the new diagram ===
PRINT ‘
END TRY
PRINT ‘
BEGIN CATCH
PRINT ‘
— If we got here, the [definition] updates didnt complete, so delete the diagram row
PRINT ‘
— (and hope it doesnt fail!)
PRINT ‘
DELETE FROM sysdiagrams WHERE diagram_id = @newid
PRINT ‘
PRINT XxXxX + Error_Message() + XxXxX
PRINT ‘
PRINT XxXxX END Tool_ScriptDiagram2008 – fix the error before running again XxXxX
PRINT ‘
RETURN
PRINT ‘
END CATCH’
END
END
GO

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, „Courier New“, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

  dipa ahuja replied to rama chandran
20-Sep-11 02:31 AM
You can Use any screen capture software and copy it and paste it as an image in the word document

or you use the "print screen" option of keyboard..


Create New Account
help
MSDE on Windows 2003 R2 box, new DL385G6 - Install Fails during SQL Services SQL Server I have been finding that I am having trouble with the Crystal Reports Server XI installation failing when it is dealing with SQL. So, as a thought and in case there was something wrong with my SQL portion of the isntall. I thought ok, I will try installing the actual MSDE application direct from Microsoft. So, I downloaded the MSDE for SQL 2000 (which is msde2000a.exe), set my switches and off to the races. It quit
Can't get Sql Server 2005 to install and start SQL Server I am trying to install Sql Server 2005 to my Laptop and it seems to install but it can't start the Product : SQLXML4 Product Version : 9.00.1399.06 Install : Successful Log File : C: \ Program Files \ Microsoft SQL Server \ 90 \ Setup Bootstrap \ LOG \ Files \ SQLSetup0006_CATH-SATELLITE_SQLXML4_1.log - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - Machine : CATH-SATELLITE Product : SQL Server
MS SQL ? SQL Server I've always thought that SQL was associated with databases? I have the following on my machine. . . Microsoft SQL Server 2005 Microsoft SQL Server Native Client Microsoft SQL Server Setup Support Files (English) Microsoft SQL Server VSS
Any ideas? New to SQL & installing ACT - why does the install fail? SQL Server Microsoft SQL Server 2005 9.00.3042.00 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = OS Version : Microsoft Windows XP Home Edition Service Pack 3 (Build 2600) Time : Sat Dec 26 22:38:29 2009 Machine : D89YH1G1 Product : Microsoft SQL Server Setup Support Files (English) Product Version : 9.00.3042.00 Install : Successful Log
SQL Express: Failed Reinstall SQL Server Dear Setup Experts: I am trying to reinstall SQL Express after playing with it some. I uninstalled it, and now, it refuses to reinstall in the face. Any ideas what this log file means and how I can get SQL Express installed? I really do not want to have to reinstall my whole system. * ** ** Start of Log File Overall summary: Final result: SQL Server installation failed. To continue, investigate the reason for the failure, correct the problem, uninstall SQL Server, and then rerun SQL Server Setup. Exit code (Decimal): -2068643839 Exit facility code: 1203