开发者

Constants and Include files in TSQL

开发者 https://www.devze.com 2023-01-06 21:11 出处:网络
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

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 @AggregatedCalcula开发者_如何学运维ted 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消