views:

163

answers:

1

Is there some kind of mechanism in SQL Server to allow Enumerated type like functionality?

For example, if I have a column Called "UpdateStatus" it usually gets setup with single letter values like so:

  1. D
  2. X
  3. U
  4. I

This could equate to a lot of things. That leads to confusion. The alternative is to have it be a string column like this:

  1. Downloaded
  2. Deleted
  3. Updated
  4. Initialized

But that has its own problems. Eventually someone is going to write something like this: where UpdateStatus = 'Initalized' (spelled wrong). Plus I hear that keying off of strings is not all that performant.

So, is there any kind of enumerated type for SQL Server that can help out with this? Basically I am looking for compile time checking that a value being compared (ie "Initialized") is part of a list of values.

I am using SQL Server 2008.

+6  A: 

Why not have lookup table that contains the code and description. Creating a foreign key to this lookup table will result in only valid codes being used.

Philip Fourie
@Philip Fourie: I can do that, (and have that kind of stuff in the db already) But what "I am looking for [is] **compile time** checking that a value being compared (ie "Initialized") is part of a list of values." Does this solution offer compile time checking?
Vaccano
@Vaccano: no, but it offers protection against data modification anomalies. However, you simply set up a matching enumeration if it's that important and cross-check on application start. And +1.
gbn
@Vaccano, sorry didn't realise *compile time* checking was that important when I responded. Sorry I don't know of a better way to it than mentioned here.
Philip Fourie