user576510
user576510

Reputation: 5905

how to have enum like strings in TSQL?

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 ?

Upvotes: 0

Views: 3763

Answers (2)

MHOOS
MHOOS

Reputation: 5306

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.

Upvotes: 0

Pரதீப்
Pரதீப்

Reputation: 93704

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

Upvotes: 2

Related Questions