<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>youdidwhatwithtsql.com &#187; Database documentation</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/database-documentation/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Sat, 24 Jul 2010 16:26:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Extract Stored Procedure Comments with TSQL</title>
		<link>http://www.youdidwhatwithtsql.com/extract-stored-procedure-comments-with-tsql/563</link>
		<comments>http://www.youdidwhatwithtsql.com/extract-stored-procedure-comments-with-tsql/563#comments</comments>
		<pubDate>Sun, 24 Jan 2010 15:16:42 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Database documentation]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/extract-stored-procedure-comments-with-tsql/563</guid>
		<description><![CDATA[I&#8217;ve blogged before about documenting databases. I&#8217;m very much a fan of extracting documentation from systems themselves so it&#8217;s as up-to-date as it can be. That&#8217;s probably why I&#8217;m such a big fan of Powershell a tool that excels at this task. This week I was thinking about how to get at the comments often [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/extract-stored-procedure-comments-with-tsql/563">Extract Stored Procedure Comments with TSQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve blogged before about <a href="http://www.youdidwhatwithtsql.com/documenting-databases/204" target="_blank">documenting databases</a>. I&#8217;m very much a fan of extracting documentation from systems themselves so it&#8217;s as up-to-date as it can be. That&#8217;s probably why I&#8217;m such a big fan of <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> a tool that excels at this task. This week I was thinking about how to get at the comments often placed at the top of stored procedure definitions. I&#8217;m referring to the little block of comments that <a href="http://www.microsoft.com/en/us/default.aspx" target="_blank">Microsoft</a> are encouraging us to fill out when we create a new store procedure.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p563code3'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5633"><td class="code" id="p563code3"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- =============================================</span>
<span style="color: #008080;">-- Author:	&lt;author  ,,NAME&gt;</span>
<span style="color: #008080;">-- Create date: &lt;create date,,&gt;</span>
<span style="color: #008080;">-- Description:	&lt;description  ,,&gt;</span>
<span style="color: #008080;">-- =============================================</span></pre></td></tr></table></div>

<p>Would it not be useful to get our hands on these comments? Here&#8217;s quick <a href="http://en.wikipedia.org/wiki/Transact-SQL" target="_blank">TSQL</a> script you can use to attempt to extract those comments.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p563code4'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5634"><td class="code" id="p563code4"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Drop temporary tables if they exist</span>
<span style="color: #0000FF;">IF</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'tempdb..#ProcDoc'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #0000FF;">BEGIN</span>
	<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #ProcDoc;
<span style="color: #0000FF;">END</span>
 <span style="color: #0000FF;">IF</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'tempdb..#temp'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
 <span style="color: #0000FF;">BEGIN</span>
	<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #temp;
<span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #008080;">-- Create a table to hold procedure documentation</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #ProcDoc
<span style="color: #808080;">&#40;</span>
	Id <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Schema</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>,
	<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Proc</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>,
	<span style="color: #808080;">&#91;</span>Author<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>,
	<span style="color: #808080;">&#91;</span>CreatedDate<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>,
	<span style="color: #808080;">&#91;</span>Description<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">TEXT</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @<span style="color: #0000FF;">proc</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>,
		@<span style="color: #0000FF;">schema</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>,
		@proc_id <span style="color: #0000FF;">INTEGER</span>,
		@schema_proc <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">200</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">-- Cursor to work through our procs</span>
<span style="color: #0000FF;">DECLARE</span> procCursor <span style="color: #0000FF;">CURSOR</span> <span style="color: #0000FF;">LOCAL</span> FAST_F<span style="color: #808080;">OR</span>WARD <span style="color: #0000FF;">FOR</span> <span style="color: #0000FF;">SELECT</span> p.<span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">proc</span><span style="color: #808080;">&#93;</span>,
							s.<span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">schema</span><span style="color: #808080;">&#93;</span>
						 <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">procedures</span> p
						 <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">schemas</span> s
							<span style="color: #0000FF;">ON</span> s.<span style="color: #202020;">schema_id</span> <span style="color: #808080;">=</span> p.<span style="color: #202020;">schema_id</span>;
&nbsp;
<span style="color: #0000FF;">OPEN</span> procCursor;
<span style="color: #0000FF;">FETCH</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">FROM</span> procCursor <span style="color: #0000FF;">INTO</span> @<span style="color: #0000FF;">proc</span>,
				@<span style="color: #0000FF;">schema</span>;
&nbsp;
<span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">@@FETCH_STATUS</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
	<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #ProcDoc
	<span style="color: #808080;">&#40;</span>
		<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Schema</span><span style="color: #808080;">&#93;</span>,
		<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Proc</span><span style="color: #808080;">&#93;</span>
	<span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">VALUES</span>
	<span style="color: #808080;">&#40;</span>
		@<span style="color: #0000FF;">Schema</span>,
		@<span style="color: #0000FF;">Proc</span>
	<span style="color: #808080;">&#41;</span>;
&nbsp;
	<span style="color: #0000FF;">SET</span> @proc_id <span style="color: #808080;">=</span> <span style="color: #FF00FF;">SCOPE_IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;
&nbsp;
	<span style="color: #008080;">-- Create a temp table to hold comments</span>
	<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #temp
	<span style="color: #808080;">&#40;</span>
		<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Text</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4000</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
	<span style="color: #808080;">&#41;</span>;
&nbsp;
	 <span style="color: #008080;">-- Build schema + proc string</span>
	<span style="color: #0000FF;">SET</span> @schema_proc <span style="color: #808080;">=</span> @<span style="color: #0000FF;">schema</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.'</span> <span style="color: #808080;">+</span> @<span style="color: #0000FF;">proc</span>;
&nbsp;
	<span style="color: #008080;">-- sp_helptext to get proc definition</span>
	<span style="color: #008080;">-- and insert into a temp table</span>
	<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #temp
	<span style="color: #0000FF;">EXEC</span> sys.<span style="color: #AF0000;">sp_helptext</span> @schema_proc;
&nbsp;
	<span style="color: #008080;">-- Just an id we'll use later to identify rows</span>
	<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> #temp <span style="color: #0000FF;">ADD</span> Id <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>;
&nbsp;
	<span style="color: #008080;">-- Get proc author</span>
	<span style="color: #0000FF;">UPDATE</span> #ProcDoc
	<span style="color: #0000FF;">SET</span> <span style="color: #808080;">&#91;</span>Author<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> 
	<span style="color: #808080;">&#40;</span>
		<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Text</span><span style="color: #808080;">&#93;</span>, <span style="color: #FF00FF;">PATINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'--Author: '</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">text</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">12</span>, <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Text</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">PATINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'--Author: '</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">text</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">12</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> Author
		<span style="color: #0000FF;">FROM</span> #temp
		<span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">text</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'-- Author:%'</span>
	<span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">WHERE</span> Id <span style="color: #808080;">=</span> @Proc_Id;
&nbsp;
	<span style="color: #008080;">-- Get proc created date</span>
	<span style="color: #0000FF;">UPDATE</span> #ProcDoc
	<span style="color: #0000FF;">SET</span> CreatedDate <span style="color: #808080;">=</span>
	<span style="color: #808080;">&#40;</span>
		<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Text</span><span style="color: #808080;">&#93;</span>, <span style="color: #FF00FF;">PATINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'--Author: '</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">text</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">17</span>, <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Text</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">PATINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'--Author: '</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">text</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">17</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
		<span style="color: #0000FF;">FROM</span> #temp
		<span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">text</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'-- Create date:%'</span>
	<span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">WHERE</span> Id <span style="color: #808080;">=</span> @Proc_Id; 
&nbsp;
	<span style="color: #008080;">-- Get proc description</span>
	<span style="color: #008080;">-- Bit messy here but works for my situation</span>
	<span style="color: #008080;">-- probably need modification dpending on your commenting habits</span>
	<span style="color: #0000FF;">UPDATE</span> #ProcDoc
	<span style="color: #0000FF;">SET</span> <span style="color: #808080;">&#91;</span>Description<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4000</span><span style="color: #808080;">&#41;</span>,  
	<span style="color: #808080;">&#40;</span>
		<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
		<span style="color: #0000FF;">FROM</span>
		<span style="color: #808080;">&#40;</span> 
			<span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">text</span><span style="color: #808080;">&#93;</span>, <span style="color: #FF0000;">'-- Description:'</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'-- '</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">text</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#93;</span>
			<span style="color: #0000FF;">FROM</span> #temp
			<span style="color: #0000FF;">WHERE</span> Id <span style="color: #808080;">&gt;=</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> Id <span style="color: #0000FF;">FROM</span> #temp <span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">text</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'-- Description:%'</span><span style="color: #808080;">&#41;</span>
			<span style="color: #808080;">AND</span> Id <span style="color: #808080;">&lt;</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#40;</span>Id <span style="color: #808080;">-</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> #temp <span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">text</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'%CREATE PROCEDURE%'</span><span style="color: #808080;">&#41;</span>
		<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> t <span style="color: #0000FF;">FOR</span> XML <span style="color: #0000FF;">PATH</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>
	<span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'&amp;#x0D;'</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> <span style="color: #008080;">-- Replace CR entities</span>
	<span style="color: #0000FF;">WHERE</span> ID <span style="color: #808080;">=</span> @Proc_id;
&nbsp;
	<span style="color: #008080;">-- Drop the temp table</span>
	<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #temp;
&nbsp;
	<span style="color: #008080;">-- Get the next row</span>
	<span style="color: #0000FF;">FETCH</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">FROM</span> procCursor <span style="color: #0000FF;">INTO</span> @<span style="color: #0000FF;">proc</span>,
					@<span style="color: #0000FF;">schema</span>;
&nbsp;
<span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #008080;">-- Clean up</span>
<span style="color: #0000FF;">CLOSE</span> procCursor;
<span style="color: #0000FF;">DEALLOCATE</span> procCursor;
&nbsp;
<span style="color: #008080;">-- View procedure documentation</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> #ProcDoc
<span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span>Description<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>;</pre></td></tr></table></div>

<p>All tables are temporary so there&#8217;s nothing to clean up. You&#8217;ll probably have to tweak this script as your commenting habits will likely vary. If all goes well the script should produce something looking like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/extracting_stored_procedure_comments.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="extracting stored procedure comments" border="0" alt="extracting stored procedure comments" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/extracting_stored_procedure_comments_thumb.png" width="644" height="115" /></a></p>
<p><map name='google_ad_map_563_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/563?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_563_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=563&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fextract-stored-procedure-comments-with-tsql%2F563' title="Extract Stored Procedure Comments with TSQL" alt=" Extract Stored Procedure Comments with TSQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/extract-stored-procedure-comments-with-tsql/563">Extract Stored Procedure Comments with TSQL</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/extract-stored-procedure-comments-with-tsql/563/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Get database size With T-SQL and MySQL</title>
		<link>http://www.youdidwhatwithtsql.com/get-database-size-with-t-sql-and-mysql/414</link>
		<comments>http://www.youdidwhatwithtsql.com/get-database-size-with-t-sql-and-mysql/414#comments</comments>
		<pubDate>Sat, 31 Oct 2009 13:40:18 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Database documentation]]></category>
		<category><![CDATA[database size]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/get-database-size-with-t-sql-and-mysql/414</guid>
		<description><![CDATA[I&#8217;ve recently been busy documentation various systems at work and came up with these queries to get a list of databases and their sizes for each SQL Server. These queries will show the server name, database names, and their sizes in KB, MB and GB. For SQL Server 2005 &#38; 2008 ?View Code TSQL-- SQL [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/get-database-size-with-t-sql-and-mysql/414">Get database size With T-SQL and MySQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently been busy documentation various systems at work and came up with these queries to get a list of databases and their sizes for each <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a>. These queries will show the server name, database names, and their sizes in KB, MB and GB.</p>
<p>For SQL Server 2005 &amp; 2008</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p414code8'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4148"><td class="code" id="p414code8"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- SQL Server 2005 /2008</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #databases
<span style="color: #808080;">&#40;</span>
 DATABASE_NAME <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span>,
 DATABASE_SIZE <span style="color: #0000FF;">FLOAT</span>,
 REMARKS <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">INSERT</span> #Databases <span style="color: #0000FF;">EXEC</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'EXEC sp_databases'</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">@@SERVERNAME</span> <span style="color: #0000FF;">AS</span> SERVER_NAME,
    DATABASE_NAME,
    DATABASE_SIZE <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'(KB)'</span>,
    <span style="color: #FF00FF;">ROUND</span><span style="color: #808080;">&#40;</span>DATABASE_SIZE <span style="color: #808080;">/</span> <span style="color: #000;">1024</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'(MB)'</span>,
    <span style="color: #FF00FF;">ROUND</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>DATABASE_SIZE <span style="color: #808080;">/</span> <span style="color: #000;">1024</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">1024</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'(GB)'</span>,
    REMARKS
<span style="color: #0000FF;">FROM</span> #databases;
&nbsp;
<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #databases;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/database_sizes_sql_server.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="database sizes sql server" border="0" alt="database sizes sql server" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/database_sizes_sql_server_thumb.png" width="244" height="99" /></a> </p>
<p>For SQL Server 2000</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p414code9'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4149"><td class="code" id="p414code9"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- SQL 2000</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">@@SERVERNAME</span> <span style="color: #0000FF;">AS</span> SERVER_NAME,
    db.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> DATABASE_NAME,
    <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">size</span> <span style="color: #808080;">*</span> <span style="color: #000;">8</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'(KB)'</span>,
    <span style="color: #FF00FF;">ROUND</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">size</span> <span style="color: #808080;">*</span> <span style="color: #000;">8</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">1024</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'(MB)'</span>,
    <span style="color: #FF00FF;">ROUND</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">size</span> <span style="color: #808080;">*</span> <span style="color: #000;">8</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">1024</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">1024</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'(GB)'</span>
<span style="color: #0000FF;">FROM</span> sysaltfiles files
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sysdatabases db
 <span style="color: #0000FF;">ON</span> db.<span style="color: #202020;">dbid</span> <span style="color: #808080;">=</span> files.<span style="color: #202020;">dbid</span>
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> db.<span style="color: #202020;">name</span></pre></td></tr></table></div>

<p>For MySQL. Note the <a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_hostname" target="_blank">@@hostname</a> variable. This was introduced in MySQL 5.0.3.8 so you&#8217;ll need to remove this if you&#8217;re using an earlier version.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p414code10'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p41410"><td class="code" id="p414code10"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- @@hostname variable introduced in MySQL 5.0.38</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> @@hostname<span style="color: #000033;">,</span>
       TABLE_SCHEMA <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #008000;">`DATABASE`</span><span style="color: #000033;">,</span>
       <span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>data_length <span style="color: #CC0099;">+</span> index_length <span style="color: #CC0099;">+</span>data_free<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #008000;">'(KB)'</span><span style="color: #000033;">,</span>
       <span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>data_length <span style="color: #CC0099;">+</span> index_length <span style="color: #CC0099;">+</span>data_free<span style="color: #FF00FF;">&#41;</span><span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #008000;">'(MB)'</span><span style="color: #000033;">,</span>
       <span style="color: #000099;">ROUND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>data_length <span style="color: #CC0099;">+</span> index_length <span style="color: #CC0099;">+</span>data_free<span style="color: #FF00FF;">&#41;</span><span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span> <span style="color: #008080;">1024</span> <span style="color: #CC0099;">/</span><span style="color: #008080;">1024</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #008000;">'(GB)'</span>
<span style="color: #990099; font-weight: bold;">FROM</span> information_schema.<span style="color: #990099; font-weight: bold;">TABLES</span>
<span style="color: #990099; font-weight: bold;">GROUP BY</span> TABLE_SCHEMA<span style="color: #000033;">;</span></pre></td></tr></table></div>

</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/database_sizes_mysql.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="database sizes mysql" border="0" alt="database sizes mysql" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/database_sizes_mysql_thumb.png" width="244" height="114" /></a></p>
<p><map name='google_ad_map_414_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/414?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_414_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=414&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fget-database-size-with-t-sql-and-mysql%2F414' title="Get database size With T SQL and MySQL" alt=" Get database size With T SQL and MySQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/get-database-size-with-t-sql-and-mysql/414">Get database size With T-SQL and MySQL</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/get-database-size-with-t-sql-and-mysql/414/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>System Documentation: My Method</title>
		<link>http://www.youdidwhatwithtsql.com/system-documentation-method/319</link>
		<comments>http://www.youdidwhatwithtsql.com/system-documentation-method/319#comments</comments>
		<pubDate>Mon, 10 Aug 2009 13:40:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Database documentation]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/?p=319</guid>
		<description><![CDATA[Jorge Segarra (Blog &#124; Twitter) posed the question System Documentation: What’s your method?. In my experience documentation has either been nonexistent, out of date or even worse, plain wrong. These situations often get blamed on lack of time dedicated to documentation tasks. Therefore I’ve always aimed at making documentation easy or even fun. I’ve blogged [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/system-documentation-method/319">System Documentation: My Method</a></p>
]]></description>
			<content:encoded><![CDATA[<p align="left">Jorge Segarra (<a href="http://sqlchicken.com" target="_blank">Blog</a> | <a href="http://twitter.com/sqlchicken" target="_blank">Twitter</a>) posed the question <a href="http://sqlchicken.com/2009/08/system-documentation-whats-your-method/" target="_blank">System Documentation: What’s your method?</a>. In my experience documentation has either been nonexistent, out of date or even worse, plain wrong. These situations often get blamed on lack of time dedicated to documentation tasks.</p>
<p align="left">Therefore I’ve always aimed at making documentation easy or even fun. I’ve blogged before about <a href="http://www.youdidwhatwithtsql.com/auditing-your-sql-server-with-powershell/133" target="_blank">auditing SQL Servers with Powershell</a> and <a href="http://www.youdidwhatwithtsql.com/auditing-network-adapters-with-powershell/126" target="_blank">auditing network adapters with Powershell</a>. I’m very much a proponent of using <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> to make documentation easy and fun. Of course, it can’t help with everything, so this should only form part of the overall strategy.</p>
<p align="left">I’m also in favour of <a href="http://www.youdidwhatwithtsql.com/documenting-databases/204" target="_blank">Documenting Databases</a> in the sense of actually adding comments, in the form of extended properties, to the database itself. This seems to be a no-brainer to me. Keep it inside the database and generate your documentation from this. I recently produced a few <a href="http://msdn.microsoft.com/en-us/library/ms187956.aspx" target="_blank">views</a> to pull together this information that can be easily exported into a spreadsheet.</p>
<p align="left">Firstly I created a schema to group these views together. This makes their function clear.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p319code18'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p31918"><td class="code" id="p319code18"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">SCHEMA</span> Documentation;
GO</pre></td></tr></table></div>

<p>Then I placed a comment on this new schema.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p319code19'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p31919"><td class="code" id="p319code19"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> sys.<span style="color: #AF0000;">sp_addextendedproperty</span> @name<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'Description'</span>, @<span style="color: #0000FF;">value</span><span style="color: #808080;">=</span>N<span style="color: #FF0000;">'This schema contains views that exposes extended properties for database tables, column, view and other objects.'</span> , @level0type<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'SCHEMA'</span>,@level0name<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'Documentation'</span>
GO</pre></td></tr></table></div>

<p>Then I created these views based on SQL Server’s <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=2ec9e842-40be-4321-9b56-92fd3860fb32&amp;displaylang=en" target="_blank">system views</a>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p319code20'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p31920"><td class="code" id="p319code20"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> Documentation.<span style="color: #202020;">ColumnComments</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #008080;">-- Column Comments</span>
<span style="color: #0000FF;">SELECT</span> sch.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> SchemaName,
	   tabs.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> TableName,
	   cols.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> ColumnName,
	   ty.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> DataType,
	   cols.<span style="color: #202020;">max_length</span> <span style="color: #0000FF;">AS</span> MaxLength,
	   ep.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> PropertyName,
	   ep.<span style="color: #0000FF;">value</span> <span style="color: #0000FF;">AS</span> PropertyValue
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">columns</span> cols
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">tables</span> tabs
	<span style="color: #0000FF;">ON</span> cols.<span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> tabs.<span style="color: #FF00FF;">object_id</span>
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">schemas</span> sch
	<span style="color: #0000FF;">ON</span> sch.<span style="color: #202020;">schema_id</span> <span style="color: #808080;">=</span> tabs.<span style="color: #202020;">schema_id</span>
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">types</span> ty
	<span style="color: #0000FF;">ON</span> ty.<span style="color: #202020;">system_type_id</span> <span style="color: #808080;">=</span> cols.<span style="color: #202020;">system_type_id</span>
	<span style="color: #808080;">AND</span> ty.<span style="color: #202020;">user_type_id</span> <span style="color: #808080;">=</span> cols.<span style="color: #202020;">user_type_id</span>
<span style="color: #0000FF;">LEFT</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">extended_properties</span> ep
	<span style="color: #0000FF;">ON</span> cols.<span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> ep.<span style="color: #202020;">major_id</span>
	<span style="color: #808080;">AND</span> ep.<span style="color: #202020;">minor_id</span> <span style="color: #808080;">=</span> cols.<span style="color: #202020;">column_id</span>;
GO
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> Documentation.<span style="color: #202020;">TableComments</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #008080;">-- Table comments</span>
<span style="color: #0000FF;">SELECT</span> sch.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> SchemaName,
	   tabs.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> TableName,
	   ep.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> PropertyName,
	   ep.<span style="color: #0000FF;">value</span> <span style="color: #0000FF;">AS</span> PropertyValue
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">tables</span> tabs
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">schemas</span> sch
	<span style="color: #0000FF;">ON</span> sch.<span style="color: #202020;">schema_id</span> <span style="color: #808080;">=</span> tabs.<span style="color: #202020;">schema_id</span>
<span style="color: #0000FF;">LEFT</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">extended_properties</span> ep
	<span style="color: #0000FF;">ON</span> tabs.<span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> ep.<span style="color: #202020;">major_id</span>
	<span style="color: #808080;">AND</span> ep.<span style="color: #202020;">minor_id</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span>;
GO
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> Documentation.<span style="color: #202020;">SchemaComments</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #008080;">-- Schema comments</span>
<span style="color: #0000FF;">SELECT</span> sch.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> SchemaName,
	   ep.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> PropertyName,
	   ep.<span style="color: #0000FF;">value</span> <span style="color: #0000FF;">AS</span> PropertyValue
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">schemas</span> sch
<span style="color: #0000FF;">LEFT</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">extended_properties</span> ep
	<span style="color: #0000FF;">ON</span> sch.<span style="color: #202020;">schema_id</span> <span style="color: #808080;">=</span> ep.<span style="color: #202020;">major_id</span>
<span style="color: #0000FF;">WHERE</span> sch.<span style="color: #202020;">principal_id</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span>;
GO
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> Documentation.<span style="color: #202020;">ViewComments</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #008080;">-- View Comments</span>
<span style="color: #0000FF;">SELECT</span> sch.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> SchemaName,
	   vw.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> TableName,
	   ep.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> PropertyName,
	   ep.<span style="color: #0000FF;">value</span> <span style="color: #0000FF;">AS</span> PropertyValue
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">views</span> vw
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">schemas</span> sch
	<span style="color: #0000FF;">ON</span> sch.<span style="color: #202020;">schema_id</span> <span style="color: #808080;">=</span> vw.<span style="color: #202020;">schema_id</span>
<span style="color: #0000FF;">LEFT</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">extended_properties</span> ep
	<span style="color: #0000FF;">ON</span> vw.<span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> ep.<span style="color: #202020;">major_id</span>
	<span style="color: #808080;">AND</span> ep.<span style="color: #202020;">minor_id</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span>;
GO</pre></td></tr></table></div>

<p>You should now have the following views in your database. I’ve created these in the <a href="http://msdn.microsoft.com/en-us/library/ms124501.aspx" target="_blank">AdventureWorks</a> sample database.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image9.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Documentation Views in SSMS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image_thumb9.png" border="0" alt="Documentation Views in SSMS" width="244" height="147" /></a></p>
<p><strong>Documentation.ColumnComments</strong></p>
<p>This view contains information about each tables columns, their data types and comments.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p319code21'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p31921"><td class="code" id="p319code21"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> Documentation.<span style="color: #202020;">ColumnComments</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image10.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Documentation.ColumnComments  View Results" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image_thumb10.png" border="0" alt="Documentation.ColumnComments  View Results" width="244" height="106" /></a></p>
<p><strong>Documentation.SchemaComments</strong></p>
<p><span style="color: #666666;">This view shows the schemas in the database along with any comments.</span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p319code22'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p31922"><td class="code" id="p319code22"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> Documentation.<span style="color: #202020;">SchemaComments</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image11.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Documentation.SchemaComments View Results" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image_thumb11.png" border="0" alt="Documentation.SchemaComments View Results" width="244" height="61" /></a></p>
<p><strong>Documentation.TableComments</strong></p>
<p><span style="color: #666666;">This view exposes comments that have been added to tables.</span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p319code23'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p31923"><td class="code" id="p319code23"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> Documentation.<span style="color: #202020;">TableComments</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image12.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Documentation.TableComments View Results" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image_thumb12.png" border="0" alt="Documentation.TableComments View Results" width="244" height="64" /></a></p>
<p><strong>Documentation.ViewComments</strong></p>
<p>This view exposes comments that have been added to views.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p319code24'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p31924"><td class="code" id="p319code24"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> Documentation.<span style="color: #202020;">ViewComments</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image13.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Documentation.ViewComments View Results" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image_thumb13.png" border="0" alt="Documentation.ViewComments View Results" width="244" height="106" /></a></p>
<p><map name='google_ad_map_319_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/319?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_319_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=319&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fsystem-documentation-method%2F319' title="System Documentation: My Method" alt=" System Documentation: My Method" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/system-documentation-method/319">System Documentation: My Method</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/system-documentation-method/319/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Documenting Databases</title>
		<link>http://www.youdidwhatwithtsql.com/documenting-databases/204</link>
		<comments>http://www.youdidwhatwithtsql.com/documenting-databases/204#comments</comments>
		<pubDate>Tue, 23 Jun 2009 19:05:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Database documentation]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/documenting-databases/204</guid>
		<description><![CDATA[Asking for database documentation in many tech shops will result in blank stares. Other places do see the value of but it forever remains on the to-do list. There are a few commercial products available hoping to help with this; SchemaToDoc &#8211; http://www.schematodoc.com/ SqlSpec &#8211; http://www.elsasoft.org/ SQL Doc &#8211; http://www.red-gate.com/products/SQL_Doc/index.htm I’m not convinced of their [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/documenting-databases/204">Documenting Databases</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Asking for database documentation in many tech shops will result in blank stares. Other places do see the value of but it forever remains on the to-do list. There are a few commercial products available hoping to help with this;</p>
<p>SchemaToDoc &#8211; <a title="http://www.schematodoc.com/" href="http://www.schematodoc.com/">http://www.schematodoc.com/</a>     <br />SqlSpec &#8211; <a title="http://www.elsasoft.org/" href="http://www.elsasoft.org/">http://www.elsasoft.org/</a>     <br />SQL Doc &#8211; <a title="http://www.red-gate.com/products/SQL_Doc/index.htm" href="http://www.red-gate.com/products/SQL_Doc/index.htm">http://www.red-gate.com/products/SQL_Doc/index.htm</a></p>
<p>I’m not convinced of their value especially when important object metadata, or business information,&#160; is missing from your database. </p>
<p>All databases objects should be tagged if appropriate with need-to-know information; Tables should be tagged with some basic information, if that data is licensed and needs annual renewal why not document this inside the database itself? </p>
<p>Columns should have a brief description; it may be obvious to you what the column holds but is it to everyone else? One system I worked with had a <a href="http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html" target="_blank">TINYINT</a> column called phone_permission with values of 0 or 1. My first guess was that ‘1’ meant you could call the telephone number on the record. Luckily I didn&#8217;t run the telesales department so no problem was caused from this misunderstanding.</p>
<p><strong>Adding comments to database tables</strong></p>
<p><font color="#666666">In <a href="http://www.mysql.com" target="_blank">MySQL</a> we can add a comment to a table like so;</font></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p204code32'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20432"><td class="code" id="p204code32"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">ALTER</span> <span style="color: #990099; font-weight: bold;">TABLE</span> TestTable <span style="color: #990099; font-weight: bold;">COMMENT</span> <span style="color: #008000;">'Data supplied by ACME Corp.'</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Table comments can then be viewed by using the <a href="http://dev.mysql.com/doc/refman/5.0/en/show-create-table.html" target="_blank">SHOW CREATE TABLE</a> syntax.</p>
<pre>mysql&gt; SHOW CREATE TABLE TestTable \G
*************************** 1. row ***************************
       Table: TestTable
Create Table: CREATE TABLE `TestTable` (
  `idnm` int(11) default NULL,
  `fullName` varchar(255) default NULL,
  `old_id` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='The data in this table is supplied by ACME Corp.'
1 row in set (0.00 sec)

mysql&gt;</pre>
<p>Or by querying <a href="http://dev.mysql.com/doc/refman/5.0/en/information-schema.html" target="_blank">INFORMATION_SCHEMA</a>;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p204code33'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20433"><td class="code" id="p204code33"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- View table comments for the current database</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> TABLE_NAME<span style="color: #000033;">,</span> TABLE_COMMENT
<span style="color: #990099; font-weight: bold;">FROM</span> information_schema.<span style="color: #990099; font-weight: bold;">tables</span>
<span style="color: #990099; font-weight: bold;">WHERE</span> TABLE_SCHEMA <span style="color: #CC0099;">=</span> <span style="color: #000099;">DATABASE</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>For <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> it’s a little more complicated. We have to add an <a href="http://msdn.microsoft.com/en-us/library/ms190243.aspx" target="_blank">extended property</a> to attach a comment onto the table.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p204code34'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20434"><td class="code" id="p204code34"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> sys.<span style="color: #AF0000;">sp_addextendedproperty</span> @name<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'Description'</span>, 
				@<span style="color: #0000FF;">value</span><span style="color: #808080;">=</span>N<span style="color: #FF0000;">'hello table Customer'</span>,
				@level0type<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'SCHEMA'</span>, 
				@level0name<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'dbo'</span>, 
				@level1type<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'TABLE'</span>, 
				@level1name<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'Customer'</span>;</pre></td></tr></table></div>

<p>Viewing this comment isn’t as easy as <a href="http://www.mysql.com" target="_blank">MySQL</a> either. Use the below <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> to view all table level comments in the current database (dbo schema);</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p204code35'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20435"><td class="code" id="p204code35"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Table comments</span>
<span style="color: #0000FF;">SELECT</span> objtype, objname, name, <span style="color: #0000FF;">value</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #AF0000;">fn_listextendedproperty</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #FF0000;">'schema'</span>, 
	<span style="color: #FF0000;">'dbo'</span>, 
	<span style="color: #FF0000;">'table'</span>, 
	<span style="color: #0000FF;">default</span>, 
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
GO</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image18.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Viewing Table comments in SSMS" border="0" alt="Viewing Table comments in SSMS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb18.png" width="644" height="134" /></a> </p>
<p>Obviously <a href="http://www.microsoft.com/en/us/default.aspx" target="_blank">Microsoft</a> didn’t intend people to hand-code <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> to add comments to their databases, but rather use tool support like <a href="http://msdn.microsoft.com/en-us/library/ms174173.aspx" target="_blank">SSMS</a>. This would probably be rather tedious if you needed to comment a large number of tables. Here’s a suggested method for making this task a little easier. First get the schema and name for all tables in the database you wish to document.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p204code36'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20436"><td class="code" id="p204code36"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> TABLE_SCHEMA, TABLE_NAME
<span style="color: #0000FF;">FROM</span> <span style="color: #808080;">IN</span>F<span style="color: #808080;">OR</span>MATION_SCHEMA.<span style="color: #202020;">TABLES</span>
<span style="color: #0000FF;">WHERE</span> TABLE_TYPE <span style="color: #808080;">=</span> <span style="color: #FF0000;">'BASE TABLE'</span>;</pre></td></tr></table></div>

<p>Copy and paste this into Excel, or other <a href="http://www.openoffice.org/product/calc.html" target="_blank">spreadsheet program</a>, so you can comment each table easily.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image19.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Comment your tables in Excel" border="0" alt="Comment your tables in Excel" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb19.png" width="644" height="147" /></a> </p>
<p>When you are finished import this data into <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> and run this <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> statement against it. N.B. This statement assumes you imported the table comment data into a table called <strong>temp_table_comments</strong>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p204code37'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20437"><td class="code" id="p204code37"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Generate T_SQL to add extended properties to all tables</span>
<span style="color: #008080;">-- in temp_table_comments</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'EXEC sys.sp_addextendedproperty @name=N'</span><span style="color: #FF0000;">'TABLE_COMMENT'</span><span style="color: #FF0000;">', 
					@value=N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> COMMENT <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">',
					@level0type=N'</span><span style="color: #FF0000;">'SCHEMA'</span><span style="color: #FF0000;">', 
					@level0name=N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> TABLE_SCHEMA <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">',
					@level1type=N'</span><span style="color: #FF0000;">'TABLE'</span><span style="color: #FF0000;">', 
					@level1name=N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> TABLE_NAME <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">';'</span>
<span style="color: #0000FF;">FROM</span> temp_table_comments;</pre></td></tr></table></div>

<p>Here’s the <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> it generates if run against the <a href="http://www.microsoft.com/downloads/details.aspx?familyid=e719ecf7-9f46-4312-af89-6ad8702e4e6e" target="_blank">AdventureWorks</a> database table names. Once you have generated the <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> then you’re ready to run it against your database and apply your comments to the tables.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image20.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Running the script against the AdventureWorks database" border="0" alt="Running the script against the AdventureWorks database" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb20.png" width="644" height="130" /></a></p>
<p>View the table comments added for the Sales schema.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p204code38'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20438"><td class="code" id="p204code38"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Table comments for AdventureWorks</span>
<span style="color: #008080;">-- Sales schema</span>
<span style="color: #0000FF;">SELECT</span> objtype, objname, name, <span style="color: #0000FF;">value</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #AF0000;">fn_listextendedproperty</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #FF0000;">'schema'</span>, 
	<span style="color: #FF0000;">'Sales'</span>, 
	<span style="color: #FF0000;">'table'</span>, 
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'TABLE_COMMENT'</span>
GO</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image21.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Table comments in the Sales schema" border="0" alt="Table comments in the Sales schema" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb21.png" width="644" height="202" /></a></p>
<p>It would be fairly easy to take a similar approach for commenting your table columns too. Once your database is nicely documented it’ll be easy to knock up a couple of reports in <a href="http://msdn.microsoft.com/en-us/library/ms159106.aspx" target="_blank">SSRS</a> and surprise the next person who asks for “<em>the documentation”.</em></p>
<p><map name='google_ad_map_204_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/204?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_204_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=204&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fdocumenting-databases%2F204' title="Documenting Databases" alt=" Documenting Databases" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/documenting-databases/204">Documenting Databases</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/documenting-databases/204/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
