The following lengthy SQL script will create a system function in SQL Server that returns a data dictionary for any database on the server when called like this:
select * from ::fn_datadictionary()
The output includes the Description of the objects.
Oskar Austegard
http://mo.notono.us 1
2 EXEC SP_CONFIGURE 'ALLOW UPDATES', 1
3 RECONFIGURE WITH OVERRIDE
4 USE master
5 go
6 CREATE FUNCTION system_function_schema.fn_datadictionary ()
7 RETURNS @DataDictionary TABLE (
8 object_id int,
9 object_name nvarchar(128),
10 object_type nvarchar(128),
11 column_order int NULL,
12 column_name varchar(60) NULL,
13 column_datatype varchar(20) NULL,
14 column_length int NULL,
15 object_description varchar(500) NULL
16 )
17 AS
18 BEGIN
19
20 DECLARE @object_name nvarchar(128)
21 -------------------------
22 --Tables and Columns
23 -------------------------
24 DECLARE table_cursor CURSOR FOR
25 SELECT name FROM sysobjects
26 WHERE type IN ('U') AND status > 1 ORDER BY name
27
28 OPEN table_cursor
29 FETCH NEXT FROM table_cursor INTO @object_name
30 WHILE @@FETCH_STATUS = 0
31 BEGIN
32 --Tables
33 INSERT @DataDictionary
34 SELECT object_id = o.[id], object_name = o.[name], object_type = 'Table',
35 column_order = 0, column_name = NULL,
36 column_datatype = NULL, column_length = NULL,
37 object_description = LTRIM(CAST(e.value AS varchar(500)))
38 FROM sysobjects o
39 LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'table', @object_name, null, default) e
40 ON o.name = e.objname
41 WHERE o.name = @object_name
42
43 --Columns
44 INSERT @DataDictionary
45 SELECT object_id = o.[id], object_name = o.[name], object_type = 'Table Column',
46 column_order = c.colorder, column_name = c.[name],
47 column_datatype = t.[name], column_length = c.[length],
48 object_description = LTRIM(CAST(e.value AS varchar(500)))
49 FROM sysobjects o INNER JOIN syscolumns c ON o.id = c.id INNER JOIN systypes t ON c.xtype = t.xtype
50 LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'table', @object_name, N'column', null) e
51 ON c.name = e.objname
52 WHERE o.name = @object_name
53 ORDER BY c.colorder
54
55 FETCH NEXT FROM table_cursor INTO @object_name
56 END
57 CLOSE table_cursor
58 DEALLOCATE table_cursor
59
60 -------------------------
61 --Views and Columns
62 -------------------------
63 DECLARE view_cursor CURSOR FOR
64 SELECT name FROM sysobjects
65 WHERE type IN ('V') AND status > 1 ORDER BY name
66
67 OPEN view_cursor
68 FETCH NEXT FROM view_cursor INTO @object_name
69 WHILE @@FETCH_STATUS = 0
70 BEGIN
71 --Views
72 INSERT @DataDictionary
73 SELECT object_id = o.[id], object_name = o.[name], object_type = 'View',
74 column_order = 0, column_name = NULL,
75 column_datatype = NULL, column_length = NULL,
76 object_description = LTRIM(CAST(e.value AS varchar(500)))
77 FROM sysobjects o
78 LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'view', @object_name, null, default) e
79 ON o.name = e.objname
80 WHERE o.name = @object_name
81
82 --Columns
83 INSERT @DataDictionary
84 SELECT object_id = o.[id], object_name = o.[name], object_type = 'View Column',
85 column_order = c.colorder, column_name = c.[name],
86 column_datatype = t.[name], column_length = c.[length],
87 object_description = LTRIM(CAST(e.value AS varchar(500)))
88 FROM sysobjects o INNER JOIN syscolumns c ON o.id = c.id INNER JOIN systypes t ON c.xtype = t.xtype
89 LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'view', @object_name, N'column', null) e
90 ON c.name = e.objname
91 WHERE o.name = @object_name
92 ORDER BY c.colorder
93
94 FETCH NEXT FROM view_cursor INTO @object_name
95 END
96 CLOSE view_cursor
97 DEALLOCATE view_cursor
98
99
100 ---------------------------
101 --Procedures and Parameters
102 ---------------------------
103 DECLARE proc_cursor CURSOR FOR
104 SELECT name FROM sysobjects
105 WHERE type IN ('P') AND status > 1 ORDER BY name
106
107 OPEN proc_cursor
108 FETCH NEXT FROM proc_cursor INTO @object_name
109 WHILE @@FETCH_STATUS = 0
110 BEGIN
111 --Procedures
112 INSERT @DataDictionary
113 SELECT object_id = o.[id], object_name = o.[name], object_type = 'Procedure',
114 column_order = 0, column_name = NULL,
115 column_datatype = NULL, column_length = NULL,
116 object_description = LTRIM(CAST(e.value AS varchar(500)))
117 FROM sysobjects o
118 LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'procedure', @object_name, null, default) e
119 ON o.name = e.objname
120 WHERE o.name = @object_name
121
122 --Parameters
123 INSERT @DataDictionary
124 SELECT object_id = o.[id], object_name = o.[name], object_type = 'Procedure Parameter',
125 column_order = c.colorder, column_name = c.[name],
126 column_datatype = t.[name], column_length = c.[length],
127 object_description = LTRIM(CAST(e.value AS varchar(500)))
128 FROM sysobjects o INNER JOIN syscolumns c ON o.id = c.id INNER JOIN systypes t ON c.xtype = t.xtype
129 LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'procedure', @object_name, N'parameter', null) e
130 ON c.name = e.objname
131 WHERE o.name = @object_name
132 AND t.name <> 'sysname'
133 ORDER BY c.colorder
134
135 FETCH NEXT FROM proc_cursor INTO @object_name
136 END
137 CLOSE proc_cursor
138 DEALLOCATE proc_cursor
139
140
141
142 ---------------------------
143 --Functions and Parameters
144 ---------------------------
145 DECLARE func_cursor CURSOR FOR
146 SELECT name FROM sysobjects
147 WHERE type IN ('FN', 'TF', 'IF') AND status > 1 ORDER BY name
148
149 OPEN func_cursor
150 FETCH NEXT FROM func_cursor INTO @object_name
151 WHILE @@FETCH_STATUS = 0
152 BEGIN
153 --Functions
154 INSERT @DataDictionary
155 SELECT object_id = o.[id], object_name = o.[name], object_type = 'Function',
156 column_order = 0, column_name = NULL,
157 column_datatype = NULL, column_length = NULL,
158 object_description = LTRIM(CAST(e.value AS varchar(500)))
159 FROM sysobjects o
160 LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'function', @object_name, null, default) e
161 ON o.name = e.objname
162 WHERE o.name = @object_name
163
164 --Parameters
165 INSERT @DataDictionary
166 SELECT object_id = o.[id], object_name = o.[name], object_type = 'Function Parameter',
167 column_order = c.colorder,
168 column_name = CASE WHEN c.[name] = '' THEN '' ELSE c.[name] END,
169 column_datatype = t.[name], column_length = c.[length],
170 object_description = LTRIM(CAST(e.value AS varchar(500)))
171 FROM sysobjects o INNER JOIN syscolumns c ON o.id = c.id INNER JOIN systypes t ON c.xtype = t.xtype
172 LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'function', @object_name, N'parameter', null) e
173 ON c.name = e.objname
174 WHERE o.name = @object_name
175 AND t.name <> 'sysname'
176 ORDER BY c.colorder
177
178 FETCH NEXT FROM func_cursor INTO @object_name
179 END
180 CLOSE func_cursor
181 DEALLOCATE func_cursor
182 RETURN
183 END
184 GO
185 EXEC SP_CONFIGURE 'ALLOW UPDATES', 0
186 RECONFIGURE WITH OVERRIDE
187 GO