views:

30

answers:

1

Is it possible to include a set of 'constant' values in a TSQL stored procedure? I have a situation where I'm using an integer field to store bit values and I have small set of 'constant' values that I use to insert/select against that field

DECLARE @CostsCalculated int = 32
DECLARE @AggregatedCalculated int = 64

--Set CostCalculated bit
update MyTable set DataStatus = ISNULL(DataStatus, 0) | @CostsCalculated
where Id = 10

--How many rows have that bit set
select count(*) from MyTable where ISNULL(DataStatus, 0) & @CostsCalculated = @CostsCalculated

I could repeat the same set of DECLARES at the top of every SP but I'd rather include the code, which means I can change in one place as new bit values are added.

+1  A: 

Off the top of my head, you can't include constants like that.

How many constants are you talking about, though? Instead of declared constants, I suppose you could create a function for each constant you want, and call the function instead of @CostsCalculated, but I'm not sure how realistic that is.

Alternately, store the values in a designated table.

LittleBobbyTables
+1: There isn't any functionality to support constants in SQL Server - has to be via function or table. Stuff like this is why Oracle Packages are so awesome, and odd why no other database provides similar functionality.
OMG Ponies