Find User Tables and Their Columns Info in SQL Server Using Object Catalog Views

2008 June 15
[Update 07-09-09: Added MSDN Link at the bottom]

Here’s a quick TSQL solution that you can use to find all the User Tables and their Columns, inluding Data Types, and Column size. This is useful when you need a quick way of finding info on creating your Database’s documentation. Whenever I am asked to document a new system/application or review an existing one, I always want to start from the very core of the system – mostly, this is a Database backend.

SELECT t.name AS [TABLE Name],
c.name AS [COLUMN Name],
p.name AS [DATA Type],
p.max_length AS[SIZE],
CAST(p.PRECISION AS VARCHAR)
+'/'
CAST(p.scale AS VARCHARAS [PRECISION/Scale]

FROM sys.objects AS t

JOIN sys.columns AS c
ON t.OBJECT_ID=c.OBJECT_ID
JOIN sys.types AS p
ON c.system_type_id=p.system_type_id

WHERE t.type_desc='USER_TABLE';

UPDATE: MSDN LINK

Querying the SQL Server System Catalog FAQ
SQL Server 2005 | SQL Server 2008


-Marlon Ribunal
kick it on DotNetKicks.com

4 Responses leave one →

Trackbacks & Pingbacks

  1. Frequently Used Catalog Views In SQL Server 2005 « Linked DBA Group - A Professional Network
  2. Find All Primary Keys and The Tables They Are Associated With « Dangling On The Shoulders Of Giants
  3. Querying the Object Catalog and Information Schema Views « Dangling On The Shoulders Of Giants
  4. Querying the Object Catalog and Information Schema Views - αρχάριος

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS