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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
sigmaconCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rdrunnerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.