How to define ENUM in SQL Server 2005? [duplicate]

可紊 提交于 2020-01-13 10:11:54

问题


Possible Duplicate:
Does SQL Server 2005 have an equivalent to MySql’s ENUM data type?

Is there any way to define ENUM in SQL Server 2005?

I have fixed values which I need to use in procedures and functions.


回答1:


Use one or more scalar UDFs?

One per constant:

  • dbo.CONST_Bicycle returns 1
  • dbo.CONST_Car returns 2

One per enum:

  • dbo.CONST_Types('Bicycle') returns 1
  • dbo.CONST_Types('Car') returns 2

Or use a table with ID, Name per enum

Use a client side enum to match this (perhaps with validation against the table solution)

There is no quick or clean way to do this like there is in .net (as per your comment).




回答2:


You might want to have lookup table named LuVehicle With columns Id and Name.

Values may look like

1,Bicycle
2,Car
3,MotorCycle

Then you can have foriegn key of Id column wherever you need in your database tables.

To retrieve the exact name of the value, you can have a simple inner join with LuVehicle table. Something like this

select empname, vehicleId, LuVehicle.Name from employees, LuVehicle 
where employees.vehicleId = LuVehicle.Id



回答3:


SQL Server supports user defined data types. You might want to do something with CREATE TYPE (Transact-SQL). But I don't know even if it is possible through User defined datatypes and not aware of its pros and cons. May be someone else will throw more light on it.



来源:https://stackoverflow.com/questions/2947353/how-to-define-enum-in-sql-server-2005

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!