CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Raymond Lewallen

Professional Learner

Know the contents of the system tables in your database

If you are working in Sql Server, one of the most important things you can learn is the contents of the system tables within your database. Objects are stored in sysobjects. Stored procedures are stored in syscomments. Column names for tables are stored in syscolumns by object id (just ID in the database), and there are others. Please go explore them, just don't change anything.

This came up because I was talking about pivot with someone and how a cool little keyword helps to do what takes a bunch of code in current release versions of Sql Server. I'm going to save pivot/unpivot topic for later this week. This friend-o-mine had hard coded tables that "pivoted" tables around for his purposes, with the column names of the table to pivot being hardcoded (isn't that confusing?). Now, Sql Server 2005 solves this scenario for us, as far as having to code it manually. But for those of you who are currently in 2000 or 7.0, you don't need to hardcode those column names for each row in a temp table. They already exist in the syscolumns table of your database. *NOTE* This only works well if you are actually using all the columns in the table to pivot. Outside of pivoting information, pulling this information from the syscolumns table still provides useful in many other scenarios, most commonly in data dictionary code.
Take a look at the following:
declare @tableName as sysname
set @tableName = 'Orders'

select [name] As ColumnName from syscolumns where [id] =
(select id from sysobjects where [name] = @tableName
    and xtype = 'U')
order by colorder

The output are the column names, in physical order as they occur in the table itself, as follows:
ColumnName
-------------------
OrderID
CustomerID
EmployeeID
OrderDate
RequiredDate
ShippedDate
ShipVia
Freight
ShipName
ShipAddress
ShipCity
ShipRegion
ShipPostalCode
ShipCountry

(14 row(s) affected)

In a few days, I'll show you the what PIVOT and UNPIVOT do, and I'll show you the really hard and crappy way you have to do it in Sql Server 2000 and earlier (or at least the hard and crappy way I do it).


Comments

Mark Bonafe said:

Very nice Raymond! The dictionary code is very useful. Looking forward to see how YOU pivot.
# March 21, 2005 7:14 AM

Sahil Malik said:

I can show you that now !!

Lewallen
Raymond

There ya go !! :)
# March 21, 2005 7:41 AM

Raymond Lewallen said:

LOL... yep, that's pretty much how I pivot :)
# March 21, 2005 7:47 AM

Mark Bonafe said:

hmmm... I think I can guess how the unpivot goes.
# March 21, 2005 11:54 AM

John Papa said:

Here is another way to get the list of columns, too. Thus avoiding hitting the system table directly.

DECLARE @table as sysname
SET @table = 'Orders'

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @table
ORDER BY
ORDINAL_POSITION
# April 4, 2005 12:28 PM

Raymond Lewallen said:

Yeah, many people are unaware that the information schemas exists, so I was going to save that for a new post, I just haven't gotten around to doing it yet. Either way, good point John :)
# April 4, 2005 12:32 PM

John Papa said:

Sorry about that.

Yeah, I get a ton of emails about how to use system tables ... i end up pointing then to the Info Schema views. Actually wrote an article that discusses them in MSDN Mag.
# April 4, 2005 12:44 PM

Raymond Lewallen said:

Don't be sorry! Had you not pointed out, I probably would have probably never gotten around to putting up a new post on information schemas. Is your article online somewhere? If so, do you have the URL handy? I'd love to be able to point people over that direction.
# April 4, 2005 12:57 PM

John Papa said:

Its the Nov 2004 issue. Kind of a Q&A format.

http://msdn.microsoft.com/msdnmag/issues/04/11/DataPoints/
# April 4, 2005 1:32 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Raymond Lewallen

Working primarily in the public sector during his career, Raymond has designed and built several high profile enterprise level applications for all levels of the government. Raymond now works as a solutions architect for EMC. Raymond is an agile coach, Microsoft MVP C# and also president of the Oklahoma City Developers Group and Oklahoma Agile Developers Group. Raymond spends a lot of his time learning and teaching such things as Test Driven Development, Domain Driven Design, Design Patterns and Extreme Programming practices and principles, to name a few. Raymond is also an advocate of Alt.Net. Raymond is primarily a framework guy, so don't ask him anything about UI :) Check out Devlicio.us!

Our Sponsors

Free Tech Publications