Magazines, Books and Articles

Friday, July 18, 2008

Copy database diagrams in SQL Server 2005/2008/2012

To copy database diagrams from database dbA to database dbB, run the following in the Query Analyser:

INSERT INTO dbB.dbo.sysdiagrams
SELECT[name],[principal_id],[version],[definition]
FROM dbA.dbo.sysdiagrams

--SELECT * FROM dbB.dbo.sysdiagrams


[Thanks to Pankaj Saha, a colleague at work, for this code snippet.]

Edit: This also works for SQL Server 2008.

Edit: This also works for SQL Server 2012, Service Pack 4.

Edit: Some other links that could be useful:
http://stackoverflow.com/questions/3310137/sql-server-2005-how-to-copy-a-database-diagram-to-another-server
If you get an error "Cannot insert the value null into column "diagram_id"", check this out: http://social.msdn.microsoft.com/Forums/en-US/sqldocumentation/thread/767de035-5509-4150-af21-8b6752653f05

Thursday, July 3, 2008

Hierarchical Data

A lot of real life data is hierarchical. Some are naturally hierarchical, like a family; others are classifications, like the folders in windows explorer. Either way, this data can be quite complex. Hierarchical data is best represented as a tree because of its ability to present the complexity in an easy to understand manner.

In data driven software, hierarchical data is often stored in a database, and presented in a tree control. Most tree controls will accept XML as data. You can retrieve hierarchical data from most databases in XML format. However the shape of this XML is hardly ever compatible with that required by the tree control, requiring transformation of the data XML to a shape acceptable by the tree control. One approach is to use XSLT to effect this transformation.

This requires the developer to develop an XSLT file. However, many developers find it hard to develop in a declarative language such as XSLT.

In this article, we discuss how to:
1. retrieve hierarchical data from the SQL Server 2005 database as XML data, using Common Table Expressions or CTE.
2. develop an
XSLT to transform this XML into a shape acceptable to the tree controls. We will develop XLSTs for the ASP.NET 3.5 tree control and a custom tree based on the article Advanced UI Design Using XML and XSL. The code download also has an example of an ExtJs tree.

Download the article and the code
here. [Click on the file name and click the Save As button.]

Or,
download the article [http://www.mediafire.com/?jmhcjz9bygm].
Download the code [http://www.mediafire.com/?xmm2xjzmxjv].