Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constants and Include files in TSQL

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.

like image 667
MrTelly Avatar asked Nov 06 '22 11:11

MrTelly


1 Answers

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.

like image 156
LittleBobbyTables - Au Revoir Avatar answered Dec 07 '22 22:12

LittleBobbyTables - Au Revoir