Solved

How do I implement enum data type?

Posted on 2004-08-31
11
2,016 Views
Last Modified: 2006-11-17
Hi.

I'm coming from an environment where I can have a column which can have enum columns.

I don't think MSSQL has this column type.

How best can I implement this?

Regards,

Richard Quadling.
0
Comment
Question by:Richard Quadling
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 18

Expert Comment

by:ShogunWade
ID: 11942093
An enum in a virtual type its not real.  

Most enums are implemented as integer datatypes.

The SQL equivelant is a Table  
eg:  CREATE TABLE (MyLookupID int,MyLookupDescription varchar(20))
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 11942433
Could you be more precise about what you want to achieve ?

User-defined datatypes in SQL Server do not allow (for the moment) to build complex datatypes.
Yet there are workarounds, like table variables, check constraints, ... depending on what you need to do.

0
 
LVL 2

Expert Comment

by:nexusSam
ID: 11942663
You can create an IDENTITY column which self-increments giving you an enumerated list
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 40

Author Comment

by:Richard Quadling
ID: 11942726
I have a column which needs to be ...

SQL, Total or Percent (1,2,3).

This setting then dictates which table to link to get additional information.

e.g.

If the row is an SQL row, then we look at a table which contains elements dealing with the creation of an SQL statement from a database.
If the row is a Percent row, then we look at a table which contains elements dealing with calculation percentages.
If the row is a Total row, then we look at a table which contains elements dealing with totals.

Initially, only these three types are defined, but others will come. One that I know of is Regress.

The table I am looking at is a table which describes "elements". Each element gets its data from somewhere. Each element has a name, desciption, output label, rules of comparison, colour logic.

Then there is a table for each specific type of element. There may be multiple records in one of the additional tables.

I think a constraint is good enough.

If I use an integer type I can also order them (I want SQL first, then totals then percentages. The Regress type may come in at all levels, RegressSQL, RegressTotals, RegressPercent - I do not know yet).

Richard.
0
 
LVL 40

Author Comment

by:Richard Quadling
ID: 11942754
IDENTITY (or autoinc as I know it) is not the right way. I want to control the values being inserted, not have then assigned as I insert.

Richard.
0
 
LVL 18

Accepted Solution

by:
ShogunWade earned 50 total points
ID: 11942755
Sounds to me like a standard lookup table with 3 values.
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 11942764
given that there are only 3 possible values you may aswell use a tinyint
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11944026
If there are only 3 possible values you can use a computed column.
0
 
LVL 40

Author Comment

by:Richard Quadling
ID: 11944929
Oh. More on the computed column please?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11944982
On second thoughts, seeing that you have more than one attribute, namely: "name, desciption, output label, rules of comparison, colour logic" ShogunWade's solution is best.
0
 
LVL 40

Author Comment

by:Richard Quadling
ID: 11949646
OK.

I now realise that the type (SQL, Percent, Total, etc) have other properties which had previously been hardcoded. This column is now a lookup with an evaluation order (i.e. you can't work out percentages or totals on data without having first got data from the DB!).

So. Look up table it is!

Thanks for this.

Richard.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
T-SQL: Episode III - Revenge of The Dude 24 52
Help with SQL Server Polygon 2 32
create an aggregate function 9 31
Help in Bulk Insert 9 30
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now