Click here to Skip to main content
15,867,568 members
Articles / Database Development / SQL Server

SqlDoc: Document your SQL Server Database

Rate me:
Please Sign up or sign in to vote.
4.73/5 (36 votes)
5 Sep 2004CPOL2 min read 251.3K   5.7K   108   47
A small command prompt utility to help you document your SQL Server/MSDE database

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 information 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, extract table names, columns names, field types, lengths, and nullable columns, and generate a HTML file with that information. You can tweak the queries to extract any other information that you need.

There is a special piece of information 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 information 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 - connects to a specific server
  • -U user_id - connects using a user name
  • -P password - self explained
  • -o outputFileName - self explained

The source code is 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 information. This means that in future releases of SQL Server, the query could fail. If you don't find the 'description field' useful, you can comment/uncomment these lines to use the method you want.

C#
// 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 information about each database, relations, key columns, views, stored procedures, etc. So fully documenting the database, it's the way to go.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Spain Spain
Software developer

Comments and Discussions

 
BugEl nombre de objeto 'sysproperties' no es válido. Pin
ingenvzla2-Jun-12 5:36
ingenvzla2-Jun-12 5:36 
GeneralRe: El nombre de objeto 'sysproperties' no es válido. Pin
Anders Gustafsson23-May-14 2:52
Anders Gustafsson23-May-14 2:52 
QuestionProgram update for SQL2008, also added primary key identification. Pin
WillemSe22-Feb-12 2:14
WillemSe22-Feb-12 2:14 
AnswerIs there any tool/utility which can generate the documentation from SQL Server 2005/2008 [modified] Pin
Amit Chaudhary17-Apr-09 4:48
Amit Chaudhary17-Apr-09 4:48 
GeneralStoredProcedure, For MS-SQL 2005 Pin
player.25-Oct-07 20:24
player.25-Oct-07 20:24 
Generalcouldn't make it work with sql server 2005 db! Pin
patrickdrd25-Sep-07 12:54
patrickdrd25-Sep-07 12:54 
GeneralRe: couldn't make it work with sql server 2005 db! Pin
jag200625-Sep-07 23:36
jag200625-Sep-07 23:36 
AnswerRe: couldn't make it work with sql server 2005 db! Pin
Amit Chaudhary21-Apr-09 19:42
Amit Chaudhary21-Apr-09 19:42 
GeneralTry this SQL Pin
RayShaw9-Aug-07 20:11
RayShaw9-Aug-07 20:11 
AnswerRe: Try this SQL Pin
Amit Chaudhary21-Apr-09 19:42
Amit Chaudhary21-Apr-09 19:42 
GeneralGet Table Descriptions Pin
nameoraliasnameoraliastv19-Dec-06 10:29
nameoraliasnameoraliastv19-Dec-06 10:29 
GeneralSQL Server 2005 Pin
pblse39-Jun-06 0:29
pblse39-Jun-06 0:29 
GeneralRe: SQL Server 2005 Pin
pjwallace28-Jul-09 9:02
pjwallace28-Jul-09 9:02 
Questionhow to Pin
canozurdo5-May-06 7:25
canozurdo5-May-06 7:25 
GeneralDocument Foreign Keys Pin
canozurdo5-May-06 7:23
canozurdo5-May-06 7:23 
GeneralBUG with tables with same name Pin
canozurdo5-May-06 7:21
canozurdo5-May-06 7:21 
GeneralModification Pin
sides_dale8-Nov-05 14:48
sides_dale8-Nov-05 14:48 
Generalsort before it generates Pin
Huisheng Chen13-Aug-05 17:37
Huisheng Chen13-Aug-05 17:37 
GeneralInvalid Object Pin
Paul Conrad30-Jul-05 6:36
professionalPaul Conrad30-Jul-05 6:36 
GeneralRe: Invalid Object Pin
gyyggyyg23-Aug-05 3:04
gyyggyyg23-Aug-05 3:04 
GeneralRe: Invalid Object Pin
Jose A. Gonzalvo23-Aug-05 5:28
Jose A. Gonzalvo23-Aug-05 5:28 
GeneralRe: Invalid Object Pin
Paul Conrad25-Aug-05 12:19
professionalPaul Conrad25-Aug-05 12:19 
GeneralRe: Invalid Object Pin
Jose A. Gonzalvo25-Aug-05 21:13
Jose A. Gonzalvo25-Aug-05 21:13 
GeneralRe: Invalid Object Pin
eyal skiba11-Jan-10 1:37
eyal skiba11-Jan-10 1:37 
GeneralRe: Invalid Object Pin
Paul Conrad11-Jan-10 4:19
professionalPaul Conrad11-Jan-10 4:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.