5,698,535 members and growing! (17,641 online)
Email Password   helpLost your password?
Database » Database » Utilities     Intermediate

SqlDoc: Document your SQL Server database

By Jose A. Gonzalvo

A small command prompt utility to help you document your SQL Server/MSDE database.
C#, SQL.NET 1.1, Win2K, WinXP, Win2003, Windows, .NETSQL Server, Visual Studio, SQL 2000, VS.NET2003, DBA, Dev

Posted: 5 Sep 2004
Updated: 5 Sep 2004
Views: 111,168
Bookmarked: 80 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
33 votes for this Article.
Popularity: 7.04 Rating: 4.64 out of 5
1 vote, 3.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
9 votes, 27.3%
4
23 votes, 69.7%
5

Sample Image - sqldoc.gif

Introduction

This article presents a small command prompt utility to help you document your SQL Server/MSDE database.

Background

In SQL Server, every property of tables, fields, relations, etc. are stored in a few system tables. You can query those tables to extract info about your database. SQL Server provides a mechanism to simplify the queries to those system tables: Information Schema.

Try to run the command "SELECT * FROM INFORMATION_SCHEMA.columns" in any database. A list of every column in every table in the database will be returned.

I code this utility to auto document my databases, extracting table names, columns names, field types, lengths, and nullable columns, and generate a HTML file with that info. You can tweak the queries to extract any other info that you need.

There is a special piece of info that is not available through Information Schema: column's description field. I find it very useful to fill that field when I design a table, to clarify the purpose of a column. To extract that description, you must bypass Information Schema and query directly the system tables.

How it works

The inner working of this app is very simple. It gets the results of special queries, and writes an XML file which is used in combination with an XSLT file to produce the final HTML file.

Using the code

You can quick test the utility, extracting info about the Northwind database, typing:

SqlDoc.exe -E -d northwind

This will produce the file output.html.

I've tried to mimic the OSQL command switches. The available switches are:

  • -E - uses integrated security to connect SQL Server.
  • -S server_name - connect to a specific server.
  • -U user_id - connect using a user name.
  • -P password - self explained.
  • -o outputFileName - self explained.

The source code, it's very simple to follow and it's self-describing.

Note that as commented before, in order to extract the column's description field, the code bypasses the standard way to extract columns info. This means that in future releases of SQL Server, the query could fail. If you don't find useful the 'description field', you can comment/uncomment these lines to use the method you want.

// most compatible way to retrieve column info but lacks 

// description field

//private const string SQL_GETFIELDS = "SELECT TABLE_NAME, " +

//    "COLUMN_NAME, COLUMN_DEFAULT, DATA_TYPE, " +

//    "CHARACTER_MAXIMUM_LENGTH, IS_NULLABLE " +

//    "FROM INFORMATION_SCHEMA.Columns " + 

//    "WHERE TABLE_NAME IN (" + SQL_GETTABLES + ")";


// this one gets columns info + the description field

private const string SQL_GETFIELDS = 
    "SELECT Sysobjects.name AS TABLE_NAME, " +
    "syscolumns.Id, syscolumns.name AS COLUMN_NAME, " + 
    "systypes.name AS DATA_TYPE, syscolumns.length" + 
    " as CHARACTER_MAXIMUM_LENGTH, " +
    "sysproperties.[value] AS COLUMN_DESCRIPTION,  " +
    "syscomments.text as COLUMN_DEFAULT, " +
    "syscolumns.isnullable as IS_NULLABLE " +
    "FROM syscolumns INNER JOIN systypes " +
    "ON syscolumns.xtype = systypes.xtype " +
    "LEFT JOIN sysobjects ON syscolumns.id = sysobjects.id " + 
    "LEFT OUTER JOIN sysproperties ON " +
    "(sysproperties.smallid = syscolumns.colid" + 
    " AND sysproperties.id = syscolumns.id) " +
    "LEFT OUTER JOIN syscomments ON syscolumns.cdefault = syscomments.id " +
    "WHERE syscolumns.id IN " + 
    "(SELECT id FROM SYSOBJECTS WHERE xtype = 'U') " + 
    "AND (systypes.name <> 'sysname')" +
    "ORDER BY syscolumns.colid";

Points of Interest

Leaving apart the purpose of this app, I was interested in learning about the XSLT transformations. Included in the demo project is a test XSLT file. You can edit this file in order to improve the output format.

Improvements

SQL Server stores tons of info about each database, relations, key columns, views, stored procedures, etc. So fully documenting the database, it's the way to go.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jose A. Gonzalvo


Software developer

Occupation: Web Developer
Location: Spain Spain

Other popular Database articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 38 (Total in Forum: 38) (Refresh)FirstPrevNext
GeneralStoredProcedure, For MS-SQL 2005memberplayer.21:24 25 Oct '07  
Generalcouldn't make it work with sql server 2005 db!memberpatrickdrd13:54 25 Sep '07  
GeneralRe: couldn't make it work with sql server 2005 db!memberjag20060:36 26 Sep '07  
GeneralTry this SQLmemberdemogodyou21:11 9 Aug '07  
GeneralGet Table Descriptionsmembernameoraliasnameoraliastv11:29 19 Dec '06  
GeneralSQL Server 2005memberpblse31:29 9 Jun '06  
Generalhow tomembercanozurdo8:25 5 May '06  
GeneralDocument Foreign Keysmembercanozurdo8:23 5 May '06  
GeneralBUG with tables with same namemembercanozurdo8:21 5 May '06  
GeneralModificationmembersides_dale15:48 8 Nov '05  
Generalsort before it generatesmemberUnruled Boy18:37 13 Aug '05  
GeneralInvalid Objectmembercomputerguru923827:36 30 Jul '05  
GeneralRe: Invalid Objectmembergyyggyyg4:04 23 Aug '05  
GeneralRe: Invalid ObjectmemberJose A. Gonzalvo6:28 23 Aug '05  
GeneralRe: Invalid Objectmembercomputerguru9238213:19 25 Aug '05  
GeneralRe: Invalid ObjectmemberJose A. Gonzalvo22:13 25 Aug '05  
GeneralWould like to try it, but gets errormemberjb3001:41 10 Mar '05  
GeneralRe: Would like to try it, but gets errormemberJose A. Gonzalvo11:29 5 May '05  
GeneralMetadata QuestionmemberDelroy6:57 26 Jan '05  
GeneralRe: Metadata QuestionmemberJose A. Gonzalvo7:08 26 Jan '05  
GeneralRe: Metadata QuestionmemberDelroy7:14 26 Jan '05  
GeneralBug with User Defined Data Typesmembermrphonig22:20 13 Sep '04  
GeneralRe: Bug with User Defined Data TypesmemberJose A. Gonzalvo3:42 14 Sep '04  
GeneralCollation / Case SensitivitymemberJosh Blair11:50 8 Sep '04  
GeneralRe: Collation / Case SensitivitymemberJose A. Gonzalvo8:30 26 Aug '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Sep 2004
Editor: Smitha Vijayan
Copyright 2004 by Jose A. Gonzalvo
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project