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 AS --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. BEGIN SELECT a.[name] as 'Table', b.[name] as 'Column', c.[name] as 'Datatype', b.[length] as 'Length',
Requires Free Membership to View
CASE
WHEN b.[cdefault] > 0 THEN d.[text]
ELSE NULL
END as 'Default',
CASE
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]
END
CREATE PROCEDURE spUtil_List_SPParams
AS
--This script produces a list of all Stored Procedures
--of type 'user' for a given database, including each
--procedure's parameters and parameter datatypes.
BEGIN
SELECT a.[name] AS 'SPName',
b.[name] AS 'ParamName',
CASE
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]
END
CREATE PROCEDURE spUtil_List_TableTriggers
AS
--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.
BEGIN
SELECT [name] AS 'Table',
CASE
WHEN [deltrig] > 0 THEN 'Yes'
ELSE 'No'
END AS 'DelTrig',
CASE
WHEN [instrig] > 0 THEN 'Yes'
ELSE 'No'
END AS 'InsTrig',
CASE
WHEN [updtrig] > 0 THEN 'Yes'
ELSE 'No'
END AS 'UpdTrig'
FROM sysobjects
WHERE [type] = 'u'
AND [name] <> 'dtproperties'
END
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.
This was first published in February 2005

Join the conversationComment
Share
Comments
Results
Contribute to the conversation