0

How to have ENUM like strings in SQL Server ? I know SQL server don't have ENUM but how to have that sort of mechanism ? Please suggest for 2 below scenarios.

  1. Not true ENUMis required, rather just a string is required ?
  2. True ENUM with number representation is requreid ?
user576510
  • 5,777
  • 20
  • 81
  • 144
  • 1
    I'm not really sure what you're looking for here. In SQL, one can easily have a table containing two columns, one numeric and one string, and both declared as keys. What do you want to do with an enum that you cannot do with such a table? – Damien_The_Unbeliever Oct 10 '14 at 06:05

2 Answers2

2

May be with Check Constraint you can achieve ENUM in sql server

CREATE TABLE Direction
 (
   types varchar(10) NOT NULL CHECK (types IN('North', 'South', 'East','West'))
 )

In direction table it will accept only the mentioned values to get inserted

Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
0

If your problem is as simple as persisting your Enum values inside SQL Server [In fact the storage DB doesn't matter that much in such scenario] then I have a word of advice for you. Store int representation of your enums inside your database and pursue a happy life afterwords:

Lets say you have a field in your your class storing phone number types:

public enum ContactType

public enum ContactType
{
    Home = 0,
    Mobile = 1
} 

How would you go if you are asked for an additional entry, going and changing DB every time? No good! You simply add it to you enum inside your code and problem sorted.

Community
  • 1
  • 1
MHOOS
  • 5,146
  • 11
  • 39
  • 74