Enums in Sql-Server

Hi,
   Can we have enums in sql-server? If not, is there any equivalent of enum in sql server.
I'm working on tsql and need to use enum in a particualar scenario in a stored procedure.
Thanks in Advance
LVL 10
answer_meAsked:
Who is Participating?
 
sigmaconConnect With a Mentor Commented:
OK, compared to

CREATE TABLE SomeTABLE (
    ...
    SomeEnumColumn ENUM('Value1', 'Value2', 'Value3') NOT NULL
    ...
)

What kind of table overhead are you talking about? I may misunderstand what you are referring to with ENUM? In my example above, the column is part of whatever table you already have and does not need to be created extra:

CREATE TABLE YourTableDefinition (
    ...
    SomeEnumColumn varchar(10) not null
        constraint CkSomeEnumColumn check (
            SomeEnumColumn in ('Value1', 'Value2', 'Value3')
        ),
    ...
)
0
 
sigmaconCommented:
SQL server doesn't support enums directly - at least not like MySQL (that's the only place where I know about enums ...), but you can create a column as text and put a check constraint on it:

CREATE TABLE SomeTable (
    SomeEnumColumn varchar(10) not null
        constraint CkSomeEnumColumn check (
            SomeEnumColumn in ('Value1', 'Value2', 'Value3')
        )
)

Hope that helps. If not, please post more specifics about what you are trying to do in the stored proc.
0
 
answer_meAuthor Commented:
This is one of feasible approach i already know but i don't want to go this way. coz there is an overhead of maintaing a table and insertions for this.
0
 
rdrunnerConnect With a Mentor Commented:
Hello...

I would implement an enumeration like this.. I would create one "master enum" table which contains ALL enums for ALL fields in your Database.

The master enaum table would look like this

ID,Value,EnumCode,Valid
1,1,1,true
2,2,1,true

As an aditional feature you could create an aditional table which could hold all laguages for the same enumeration. This would allow for a good localisation support.
ID,EnumID,Text,Language
1,1,"Male","EN"
2,1,"männlich","DE"
3,2,"Female","EN"
3,2,"weiblich","DE"

etc..

You can also create a function that will validate if the current code is valid.


0
All Courses

From novice to tech pro — start learning today.