all groups > sql server programming > february 2005 >
You're in the

sql server programming

group:

enum for a column


RE: enum for a column Brian Selzer
2/17/2005 10:33:02 PM
sql server programming:
the closest thing to an enum in TSQL is a check constraint.
if you use IN the definition isn't that much more tedious than an enum
definition in C#--you still have to list all of the possible values. For
example,

create table t1
(
f1 int identity(1,1) not null
constraint pk_t1 primary key clustered,
f2 char(5) not null
constraint ck_f2 check (f2 in ('NORTH', 'SOUTH', 'EAST', 'WEST'))
)



[quoted text, click to view]
enum for a column Jonathan Dodds
2/17/2005 10:42:34 PM
Is there a way to do something like an enum for a column?

I have a column that should contain one of a fixed set of values. The set of
values won't change and there is only one column in one table that uses this
set of values.

So I was thinking that there wasn't much point in creating a new table just
for this set of values. I could set the data type of the column to a fixed
size char and create a check constraint that tests for each possible value.
There are 20 values in the set so the constraint will be tedious to write.

But there is a notion of order to the set. i.e. value 1 < value 2 < value
N.

So what I really want is an enum like in C++ or C#.

What's the closest approximate in T-SQL?

AddThis Social Bookmark Button