I recently accidently wrote a really ugly stored proc where I wished I had enums,
Eg.
CREATE PROCEDURE Proc_search_with_enum @user int, @account_category {enum}
I understand that SQL 2000 doesn't have enums as a first class language construct, which coding conventions do you use to simulate enums or otherwise address the same issue?
Or am I'm I doomed to just using VARCHAR and IF @account_category='cat1'?
EDIT: T-SQL and C# are the client languages.
EDIT: Thanks all! Lots of good advice, I wish I could accept several answers, I've voted everyone up-
Summary of answers
You could take a look at the answer to this question. As far as I know, enum types are not part of SQL Server.
Anyhow, it's better to use an integral (INT
, TINYINT
, ...) type for your enums than a string type. Usually the enum types in your programming language of choice correspond better to integers than to strings.
In C#, by default every enum value corresponds to an integer, starting from 0. You can even cast between them, so this is valid code:
public enum MyEnum
{
FirstEnumValue = 0,
SecondEnumValue = 1
}
...
// Assuming you have opened a SqlDataReader.
MyEnum enumValue = (MyEnum) reader["account_category"];
And LINQtoSQL also supports this. If you have an enum-typed property and your database column is an integer type, conversion is automatic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With