Constants and Include files in TSQL

2019-02-22 09:22发布

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条回答
冷血范
2楼-- · 2019-02-22 09:56

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.

查看更多
登录 后发表回答