Unix Technical Forum

How do I use constants in stored procedures?

This is a discussion on How do I use constants in stored procedures? within the SQL Server forums, part of the Microsoft SQL Server category; --> I have several instances of "magic number" variables (tinyints). In my program, I have assigned an enumeration to make ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:03 PM
Robin Tucker
 
Posts: n/a
Default How do I use constants in stored procedures?

I have several instances of "magic number" variables (tinyints). In my
program, I have assigned an enumeration to make the meaning clear, such as:

enum Condition {

Green = 0,
Yellow,
Red
}

In my database, one of the tables contains a "Condition" field (tinyint),
which stores the number 0, 1 or 2. However, in my Stored Procedures I am
having to use magic numbers as follows:

SELECT * From Nodes Where Condition = 1

(to select all nodes with yellow condition)

Obviously, meaning is obfuscated here. I would rather use constants but not
have to re-define them in every stored procedure I use.

I there any way to do this?



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:03 PM
Bruce Loving
 
Posts: n/a
Default Re: How do I use constants in stored procedures?

You could create a UDF

create function uf_Condition( Status as varchar(6)) as integer
set uf_condition = case Status when 'Green' then 0
when 'Yellow' then 1
when 'Red' then 2
end

then
SELECT * From Nodes Where Condition = uf_Condition('Green')


On Wed, 3 Dec 2003 17:54:41 -0000, "Robin Tucker"
<idontwanttobespammedanymore@reallyidont.com> wrote:

>I have several instances of "magic number" variables (tinyints). In my
>program, I have assigned an enumeration to make the meaning clear, such as:
>
>enum Condition {
>
> Green = 0,
> Yellow,
> Red
>}
>
>In my database, one of the tables contains a "Condition" field (tinyint),
>which stores the number 0, 1 or 2. However, in my Stored Procedures I am
>having to use magic numbers as follows:
>
>SELECT * From Nodes Where Condition = 1
>
>(to select all nodes with yellow condition)
>
>Obviously, meaning is obfuscated here. I would rather use constants but not
>have to re-define them in every stored procedure I use.
>
>I there any way to do this?
>
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:08 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com