Click here to Skip to main content
Email Password   helpLost your password?

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:

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
AnswerIs there any tool/utility which can generate the documentation from SQL Server 2005/2008 [modified]
Amit Chaudhary
5:48 17 Apr '09  
SQLDoc Sharp
SQLDoc Sharp, an interactive tool designed to generate the SQL Server 2005/2008 documentation.
This is a free solution...!
http://www.amitchaudhary.com/ .

modified on Sunday, April 19, 2009 3:32 AM

GeneralStoredProcedure, For MS-SQL 2005
player.
21:24 25 Oct '07  
but, I can not get Description of Column.
maybe, you can help me to find it.

http://www.player.idv.tw/prog/index.php?title=SQL:StoredProcedure:MyTable

Taiwan's pauper.
Generalcouldn't make it work with sql server 2005 db!
patrickdrd
13:54 25 Sep '07  
couldn't make it work with sql server 2005 db!

does anyone know what should I do?

thanks in advance!
GeneralRe: couldn't make it work with sql server 2005 db!
jag2006
0:36 26 Sep '07  
The code will work only on SQL 2000 databases. System tables have changed in SQL 2005 and that is why it does not work. I think someone has posted the SQL 2005 queries in the comments.

The code posted here evolved to a full-featured database documenter called dbdesc.
AnswerRe: couldn't make it work with sql server 2005 db!
Amit Chaudhary
20:42 21 Apr '09  
try SQLDoc Sharp
SQLDoc Sharp, an interactive tool designed to generate the SQL Server 2005/2008 documentation.
This is a free solution...!
http://www.amitchaudhary.com/ .
GeneralTry this SQL
demogodyou
21:11 9 Aug '07  
SELECT d.name TableName,a.name FieldName,b.name TypeName,a.length Length,a.isnullable IS_NULL
FROM syscolumns a, systypes b,sysobjects d
WHERE a.xtype=b.xusertype and a.id=d.id and d.xtype='U'
AnswerRe: Try this SQL
Amit Chaudhary
20:42 21 Apr '09  
try SQLDoc Sharp
SQLDoc Sharp, an interactive tool designed to generate the SQL Server 2005/2008 documentation.
This is a free solution...!
http://www.amitchaudhary.com/ .
GeneralGet Table Descriptions
nameoraliasnameoraliastv
11:29 19 Dec '06  

Read Table Descriptions (added them with sql server 2005 management studio express)

SELECT distinct sysobjects.id, sysobjects.name AS TABLE_NAME, (select sysproperties.value from sysproperties where sysproperties.name = 'MS_Description' and sysproperties.type = 3 and sysproperties.id = sysobjects.id) as TABLE_DESCRIPTION FROM SYSOBJECTS WHERE xtype = 'U'

GeneralSQL Server 2005
pblse3
1:29 9 Jun '06  
The sysproperties is deprecated in SQL Server 2005, this query gives you the same results:

SELECT
OBJECT_NAME(c.object_id) AS TABLE_NAME,
c.object_ID AS ID,
c.name AS COLUMN_NAME,
systypes.Name AS DATA_TYPE,
c.max_length as CHARACTER_MAXIMUM_LENGTH,
ex.value AS COLUMN_DESCRIPTION,
syscomments.text as COLUMN_DEFAULT,
c.is_nullable as IS_NULLABLE
FROM
sys.columns c
INNER JOIN
systypes ON c.system_type_id = systypes.xtype
LEFT JOIN
syscomments ON c.default_object_id = syscomments.id
LEFT OUTER JOIN
sys.extended_properties ex
ON
ex.major_id = c.object_id
AND ex.minor_id = c.column_id
AND ex.name = 'MS_Description'
WHERE
OBJECTPROPERTY(c.object_id, 'IsMsShipped')=0
ORDER
BY OBJECT_NAME(c.object_id), c.column_id



PL.
GeneralRe: SQL Server 2005
pjwallace
10:02 28 Jul '09  
To match the original, you should additionally add the "AND systypes.name <> 'sysname'" to the WHERE clause
Generalhow to
canozurdo
8:25 5 May '06  
How can i enter a description for a table, view or store procedure??? and if it is possible... it would be good that the documenter generates this information among with the other...

Jhonny

Bye
GeneralDocument Foreign Keys
canozurdo
8:23 5 May '06  
That's a suggest, it would be excellent if the SQL Doccumenter gives the info about the foreign keys of each table (or field)
Thanks in advance for so good development, my english is not very good speaking.

Bye
GeneralBUG with tables with same name
canozurdo
8:21 5 May '06  
I suggest the next query for validate that not exists any table with the same name (and different owner)...


SELECT TABLE_NAME, COUNT(*) FROM INFORMATION_SCHEMA.Tables WHERE TABLE_TYPE = 'BASE TABLE' GROUP BY TABLE_NAME HAVING COUNT(*) > 1



Bye
GeneralModification
sides_dale
15:48 8 Nov '05  
If you are interested in an update, I modified the code, so that it would also document stored procedures and views.
Generalsort before it generates
Unruled Boy
18:37 13 Aug '05  
well, I want to sort the talbes according to their foreign keys before they are generated.

for example:
Table A:ID, B.ID
Table B:ID
Table C:ID, A.ID
Table D:ID, C.ID

then the generation order should be:
B->A->C->D

do you have any idea how to implement it?

Regards,
unruledboy@hotmail.com
GeneralInvalid Object
computerguru92382
7:36 30 Jul '05  
Hello,

I am trying SqlDoc and I keep getting the message:

Invalid object name 'sysproperties'.

Could you shed some light on why this is happening? It happens on any database I select.

Paul
GeneralRe: Invalid Object
gyyggyyg
4:04 23 Aug '05  
I have same error, any help
GeneralRe: Invalid Object
Jose A. Gonzalvo
6:28 23 Aug '05  
I've never got that error. However try it using user 'sa'.

I would like to track down this error. Could you post your current versions of SO, .NET, SQL Server... ?

Regards.
GeneralRe: Invalid Object
computerguru92382
13:19 25 Aug '05  
Jose,

I am using Windows XP Pro SP2, with .NET 1.1 and the database is running on MSDE that came with Office 2000.

Paul
GeneralRe: Invalid Object
Jose A. Gonzalvo
22:13 25 Aug '05  
Ok, I've just installed that MSDE version (SQL Server 7.0.xxx) and it seems that doesn't support the way I'm using to extract fields info.

To solve this problem uncomment the query SQL_FIELDS that uses INFORMATION_SCHEMA and comment the other one.

It should run properly.

Hope this helps.
GeneralRe: Invalid Object
eyal skiba
2:37 11 Jan '10  
replace with this SQL:

private const string SQL_GETFIELDS = "SELECT sys.sysobjects.name AS TABLE_NAME, c.object_id, c.name AS COLUMN_NAME, sys.systypes.name AS DATA_TYPE, c.max_length AS CHARACTER_MAXIMUM_LENGTH, ex.value AS COLUMN_DESCRIPTION, sys.syscomments.text AS COLUMN_DEFAULT, c.is_nullable AS IS_NULLABLE FROM sys.columns AS c INNER JOIN sys.systypes ON c.system_type_id = sys.systypes.xtype LEFT OUTER JOIN sys.sysobjects ON c.object_id = sys.sysobjects.id LEFT OUTER JOIN sys.extended_properties AS ex ON ex.major_id = c.object_id AND ex.minor_id = c.column_id AND ex.name = 'MS_Description' LEFT OUTER JOIN sys.syscomments ON c.default_object_id = sys.syscomments.id WHERE (c.object_id IN (SELECT id FROM sys.sysobjects AS sysobjects_1 WHERE (xtype = 'U'))) AND (sys.systypes.name <> 'sysname') ORDER BY c.column_id";
GeneralRe: Invalid Object
Paul Conrad
5:19 11 Jan '10  
Thank you, but I got it to work shortly after my last post to the author 4 years ago Roll eyes

"The clue train passed his station without stopping." - John Simmons / outlaw programmer

"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham


GeneralWould like to try it, but gets error
jb300
1:41 10 Mar '05  
I run XP but I don't have Visual Studio.net. Do I need that or what do I need to be able to run the exe file?Smile

I get an error msg similar to this:

"couldn't initialize the program correctly (0xc0000135). Press OK to exit the program"

( I have swedish version of XP with a swedish error msg, don't think you get anything out of that... ) Smile



StarCraft, Brood War, Quantum Star SE, Halo, Halo2
GeneralRe: Would like to try it, but gets error
Jose A. Gonzalvo
11:29 5 May '05  
This application requires you to have the .NET Framework v1.1 installed on your machine.

GeneralMetadata Question
Delroy
6:57 26 Jan '05  
I am trying to figure out a way to access the "Description" text of a column in a SQL 2000 database. I can get a lot of stuff using the INFORMATION_SCHEMA.columns view but I really need to get a hold of the 'user entered' description field. Can anyone point me in a direction that will help? I am using ADO for data access.


Last Updated 6 Sep 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010