Manage Learn to apply best practices and optimize your operations.

Tips to automate documentation tasks for SQL Server 2000

These procedures help automate documentation tasks during the development of production databases for Microsoft SQL Server 2000.

Development of production databases in a commercial setting requires a high degree of responsibility on the part of the developer and DBA. This includes not only constructing the most appropriate schema for a given situation, but also providing the documentation and ongoing support so often lacking in commercial projects today.

By automating some of the documentation tasks for your databases, you are doing yourself and your sponsor/client a big favour. The procedures below are examples of simple routines that can be stored in the MODEL database, and therefore propagated to all new databases. The tabular output from each is easily formatted for presentation in other tools, or simply provided to Web developers as reference material/metadata.

By adding these procedures to the model database, all new databases will become somewhat self-documenting. Result sets are easily accessed via the EXECUTE statement, called into ADO recordsets, or simply pasted into Excel. They have been tested on SQL Server 2000 (SP1-SP3) and will presumably work in 7.0 as well, assuming the utilized sys tables haven't changed format.

CREATE PROCEDURE spUtil_List_TableFields


--This script produces a list of all Tables
--of type 'user' for a given database, including each
--table's field names, field datatypes, and field lengths.


SELECT a.[name] as 'Table',
  b.[name] as 'Column',
  c.[name] as 'Datatype',
  b.[length] as 'Length',
   WHEN b.[cdefault] > 0 THEN d.[text]
  END as 'Default',
   WHEN b.[isnullable] = 0 THEN 'No'
   ELSE 'Yes'
  END as 'Nullable'
FROM  sysobjects a
INNER JOIN syscolumns b
ON  a.[id] = b.[id]
INNER JOIN systypes c
ON  b.[xtype] = c.[xtype]
LEFT JOIN syscomments d
ON  b.[cdefault] = d.[id]
WHERE a.[xtype] = 'u'
AND  a.[name] <> 'dtproperties'
ORDER BY a.[name],b.[colorder]



--This script produces a list of all Stored Procedures
--of type 'user' for a given database, including each
--procedure's parameters and parameter datatypes.


SELECT  a.[name] AS 'SPName',
   b.[name] AS 'ParamName',
      WHEN  c.[name] LIKE '%char%' THEN c.[name] + '(' + convert(varchar,b.[length]) + ')' 
      ELSE  c.[name]
   END AS 'DataType'
FROM   sysobjects a
LEFT OUTER JOIN syscolumns b
ON   a.[id] = b.[id]
INNER JOIN  systypes c
ON   b.[xtype] = c.[xtype]
WHERE   a.[type] = 'p'
AND    [category] <> 2 
ORDER BY  a.[name],b.[colid]

CREATE PROCEDURE spUtil_List_TableTriggers


--This script produces a list of all Tables
--of type 'user' for a given database, and flags whether
--each table posesses an Insert, Delete or Update Trigger.


SELECT [name] AS 'Table',
   WHEN [deltrig] > 0 THEN 'Yes'
   ELSE 'No'
  END AS 'DelTrig',
   WHEN [instrig] > 0 THEN 'Yes'
   ELSE 'No'
  END AS 'InsTrig',
   WHEN [updtrig] > 0 THEN 'Yes'
   ELSE 'No'
  END AS 'UpdTrig'

FROM  sysobjects

WHERE [type] = 'u'
AND  [name] <> 'dtproperties'

Reader Feedback

Martin S. writes: Documentation should be done properly using processes and standards ideally via a database modelling tool prior to building the actual database elements and not afterwards in a slapdash listing format derived from the DB. Where's the controlled development environment with signoffs, etc?

The author responds: Exactly what part of the submitted tip suggests that the procedures should be used to the exclusion of any other sound documentation and planning mechanism? This, in fact, is not indicated by the tip. Rather, the procedures are offered as a time saving tool for post-documentation and collection of meta-data by web programmers. The tip does not refer at all to the process that led to the development of the database in the first place. Further, what good will come of posting ill-planned criticism within such a forum? It is exactly this sort of expression of personal insecurity that clogs technical forums to the point where many people are discouraged from participating. In the end we are left with forums where only the aggressive or ignorant compete in a battle to prove their own superiority, rather than a resource that strives to benefit everyone.

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free SQL Server tips and scripts.
  • Tip contest: Have a SQL Server tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical SQL Server questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: SQL Server tips, tutorials, and scripts from around the Web.

Dig Deeper on SQL Server Database Modeling and Design

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.