Database Schema to Wiki Trick

Ok, this is a really slick trick for generating wiki mark-up for the database schema in SQL Server. One of my buddies, Terry Denham from the Community Server :: Forums team (previously the ASP.NET Forums) and co-worker here at Telligent, came up with this nice little script:

select '!!!Database Schema', '1' as [dorder]
union all
select '!!!!' + o.name, '2.' + o.name + '.0' [dorder]
         from sysobjects o where o.type = 'U' and o.name like 'forums%'
union all
select         '||''''''Column Name''''''||''''''Data Type''''''||''''''Nullable''''''||''''''Key''''''||''''''Description''''''||', '2.' + o.name + '.1' [dorder]
         from sysobjects o where o.type = 'U' and o.name like 'forums%'
union all
select        '||[' + c.name + ']||' + type_name(c.xusertype) + '(' + convert(varchar(20), convert(int, c.length)) + ')' + '||' + case when c.isnullable = 0  then 'no' else 'yes' end + '||||||'
          , '2.' + o.name + '.2.' + convert(varchar(20), c.colid) [dorder]
         from sysobjects o inner join syscolumns c on o.id = c.id where o.type = 'U' and o.name like 'forums%'
order by 2, 1

Just paste the output into your favorite wiki (this has been tested with FlexWiki) and, voila, you have your schema in a nice readable wiki format!

Also, you can run the following SQL script to generate the corresponding data dictionary:

select '!!!Data Dictionary', '1' as [dorder]

union all

select distinct '[' + c.name + ']', '2.' + c.name from sysobjects o inner join syscolumns c on o.id = c.id where o.name like 'forums%' and o.type = 'U'

order by 2, 1

This works great when you have pascal cased columns because your columns will become wiki links, and you can then use the latter script to gen the corresponding data dictionary.

Very nice!

(NOTE: Apologies on the poor formatting. Somehow this editor has mangled my poor post!)

Comments

# Peter said:

Just curious...

Everyone says that it is not good to access system tables directly and everyone do that (instead of use system stored procs or INFORMATION_SCHEMA views).

Why?

p.

Friday, July 23, 2004 6:24 AM
# TrackBack said:
Friday, July 23, 2004 7:16 AM
# Jason Alexander said:

Hey Peter!


Yeah, they definitely say to use the INFORMATION_SCHEMA views. This is primarily for the gain of the abstraction, in case they change any of the underlying system tables in the future your queries should still continue to work.

Obviously, this query doesn't use the schema views, so I wouldn't recommend this in production ;) but it's still a great little script that you should be able to migrate to the schema views pretty easily.

Friday, July 23, 2004 9:36 AM
# Terry Denham said:

INFORMATION_SCHEMA views are great but honestly they don't contain all the information you may need. I guess I still use the system catalog because I've been developing against and supporting SQL Server for almost 10 years (since 4.21a) and know the system tables inside & out so it may be a bad habit of mine.

You are free to change the tables used above to use INFORMATION_SCHEMA.COLUMNS above though you'll have to change the logic a little.

Friday, July 23, 2004 10:11 AM
# TrackBack said:

^_^,Pretty Good!

Sunday, April 10, 2005 5:29 AM

Leave a Comment

(required) 
(required) 
(optional)
(required)