Click here to Skip to main content
15,887,433 members
Articles / Database Development / SQL Server

Document SQL Server 2000/2005 Database

Rate me:
Please Sign up or sign in to vote.
4.83/5 (103 votes)
17 Oct 2009CPOL1 min read 242.4K   27.2K   113   81
T-SQL script to generate the document of SQL 2000/2005 database

Introduction

Here is the script to generate the HTML document of your SQL Server 2000/2005 database. It is reverse engineering after the database is created. I hope it will be useful to many of you.

Background

While working as a part time DBA for some projects, I encountered difficulties in documenting and publishing my databases because I do not have any tool available to generate the document. I have written this script that gives the output in HTML that is easy to be modified/published/distributed.

Using the Code

Follow these steps to use this script:

  • Download the attached script file and open it in Management Studio (tested on version 9.00).
  • Select the database for which you want to generate document.
  • Select Result to File (Ctrl+Shift+F) from Query - Results To menu.
  • Execute (F5) the script.
  • In the Save Result dialogue box, type a file name (filename.htm/filename.html) in the File Name text box and select All Files (*.*) in Save As Type dropdown.
  • After successful execution of query, check the output file.

Note: For populating the description of table and columns, update the Description property of table and columns while creating database diagram or add extended property for them with name as MS_Description.

I hope this information will be helpful. I am planning to provide details of code in my next update.

Updates

  • Bug in getting index column is resolved
  • Bug in getting check constraints list is resolved
  • Definition of check constraints is added to document
  • Table Summary is changed to Table Description
  • Table and column description is populated from extended property

Download documentation application from here.

License

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


Written By
Database Developer
United Kingdom United Kingdom
With 10 years of experience of working on Microsoft platform, currently I am working as Database/BI expert.

Comments and Discussions

 
GeneralThanks Pin
b minor6-Mar-08 3:40
b minor6-Mar-08 3:40 
GeneralWow, thanks Pin
Thomas Wells4-Mar-08 6:20
Thomas Wells4-Mar-08 6:20 
GeneralComming up with newer versions... Pin
virtualleye28-Feb-08 17:49
virtualleye28-Feb-08 17:49 
GeneralAmazing Work. Pin
Dipak V Bava28-Feb-08 17:25
Dipak V Bava28-Feb-08 17:25 
GeneralRe: Amazing Work. Pin
Nitinpatel28-Feb-08 22:07
Nitinpatel28-Feb-08 22:07 
GeneralRe: Amazing Work. Pin
Dipak V Bava6-Mar-09 4:34
Dipak V Bava6-Mar-09 4:34 
Generalthanx Pin
KINGCHEMICAL25-Feb-08 18:30
KINGCHEMICAL25-Feb-08 18:30 
GeneralAdd like from fk column to referenced table Pin
gilad12325-Feb-08 0:44
gilad12325-Feb-08 0:44 
I add link from fk column to the referenced table, to add it U nead to change 3 places:

1. change the #Columns declareation to:
create Table #Columns (id int identity(1,1), Name varchar(155), Type Varchar(155), Nullable varchar(2), [description] varchar(4000), refName varchar(155), refObj varchar(155), refCol varchar(155), refOnerTable varchar(155))

2. the #column insert into query:
else if @SqlVersion = '2005' <br />
	begin<br />
	insert into #Columns  (Name, Type, Nullable, [description],refName, refObj, refCol, refOnerTable )<br />
	--FOR 2005	<br />
Select c.name, <br />
	type_name(user_type_id) + (<br />
	case when (type_name(user_type_id) = 'varchar' or type_name(user_type_id) = 'nvarchar' or type_name(user_type_id) ='char' or type_name(user_type_id) ='nchar')<br />
		then '(' + cast(max_length as varchar) + ')' <br />
	 when type_name(user_type_id) = 'decimal'  <br />
			then '(' + cast([precision] as varchar) + ',' + cast(scale as varchar)   + ')' <br />
	else ''<br />
	end				<br />
	), <br />
	case when is_nullable = 1 then 'Y' else 'N'  end,<br />
	cast(p.value as varchar(4000)),<br />
	f.name, <br />
	object_name(fc.referenced_object_id) , <br />
	COL_NAME (fc.referenced_object_id, fc.referenced_column_id) ,<br />
	'[' +fs.name + '].[' + fo.name + ']' <br />
from sys.columns c<br />
	inner join #Tables t on t.object_id = c.object_id<br />
	left outer join sys.extended_properties p on p.major_id = c.object_id and p.minor_id  = c.column_id and p.name = 'MS_Description' <br />
	left outer join sys.foreign_key_columns  fc  on t.object_id = fc.parent_object_id <br />
												AND c.column_id = fc.parent_column_id<br />
	left outer join sys.foreign_keys f on t.object_id = f.parent_object_id<br />
										and f.object_id = fc.constraint_object_id <br />
	left outer join sys.objects fo on fo.object_id = f.referenced_object_id<br />
	left outer join sys.schemas fs on fs.schema_id = fo.schema_id <br />
where t.id = @i<br />
order by c.column_id


3. the column display to:
select @Output =<br />
			case WHEN refObj is null then<br />
				 '<tr><td width="20px" align="center">' + Cast((@j) as varchar) + '</td><td width="150px">' + isnull(name,'')  + '</td><td width="150px">' +  upper(isnull(Type,'')) + '</td><td width="50px" align="center">' + isnull(Nullable,'N') + '</td><td>' + isnull([description],'') + '</td></tr>' <br />
				else '<tr><td width="20px" align="center">' + Cast((@j) as varchar) + '</td><td width="150px" title="'+isnull(refName, '')+'"><a href="#Table:' + isnull(refOnerTable,'') + '">' +  isnull(name,'') + '</a></td><td width="150px">' +  upper(isnull(Type,'')) + '</td><td width="50px" align="center">' + isnull(Nullable,'N') + '</td><td>' + isnull([description],'') + '</td></tr>' <br />
			end<br />
			from #Columns  where id = @j


as U can see I add the link only fot the 2005 server, the 2000 act as usual (or U change the 2000 query...)

thanks for the code
GeneralNice Script Pin
esolomonidis18-Feb-08 8:58
esolomonidis18-Feb-08 8:58 
GeneralExcellent work! Pin
sys2matk18-Feb-08 7:28
sys2matk18-Feb-08 7:28 
GeneralCool Quesry!!!! Pin
alex_kl18-Feb-08 5:58
alex_kl18-Feb-08 5:58 
GeneralGreat! But... Pin
PKV113-Feb-08 1:22
PKV113-Feb-08 1:22 
GeneralRe: Great! But... Pin
Nitinpatel17-Feb-08 22:55
Nitinpatel17-Feb-08 22:55 
QuestionWhere is the Description? Pin
IamHanson12-Feb-08 15:22
IamHanson12-Feb-08 15:22 
AnswerRe: Where is the Description? Pin
Nitinpatel18-Feb-08 2:28
Nitinpatel18-Feb-08 2:28 
GeneralExcellent work Pin
SleepyCrat12-Feb-08 3:33
SleepyCrat12-Feb-08 3:33 
GeneralRe: Excellent work Pin
Nitinpatel18-Feb-08 2:29
Nitinpatel18-Feb-08 2:29 
GeneralVery useful Pin
Vikas Mehta12-Feb-08 3:13
Vikas Mehta12-Feb-08 3:13 
GeneralVery good indeed Pin
crchambers144212-Feb-08 1:23
crchambers144212-Feb-08 1:23 
GeneralRe: Very good indeed Pin
Nitinpatel17-Feb-08 21:52
Nitinpatel17-Feb-08 21:52 
GeneralRe: Very good indeed Pin
rjf6061817-Jul-08 5:06
rjf6061817-Jul-08 5:06 
QuestionExcellent script thanks! Pin
PKBoone12-Feb-08 0:47
PKBoone12-Feb-08 0:47 
GeneralRe: Excellent script thanks! Pin
Nitinpatel17-Feb-08 21:51
Nitinpatel17-Feb-08 21:51 
GeneralRe: Excellent script thanks! Pin
PKBoone17-Feb-08 22:10
PKBoone17-Feb-08 22:10 
GeneralRe: Excellent script thanks! Pin
Nitinpatel17-Feb-08 23:32
Nitinpatel17-Feb-08 23:32 

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.