There is a column in a database that is of type INT (Sql server).
数据库中有一个类型为INT的列(Sql server)。
This int value is used at a bit flag, so I will be AND'ing and OR'ing on it.
这个int值用于位标志,所以我将对它进行AND运算和运算。
I have to pass a parameter into my sproc, and that parameter will represent a specific flag item.
我必须将参数传递给我的sproc,该参数将代表一个特定的标志项。
I would normally use an enumeration and pass the int representation to the sproc, but since many different modules will be accessing it it won't be practicial for them all to have my enum definition (if it is changed, it will be a headache to roll it out).
我通常会使用枚举并将int表示传递给sproc,但由于许多不同的模块将访问它,因此对于所有人来说,拥有我的枚举定义并不实际(如果它被更改,那将是一个令人头疼的问题。滚出来)。
So should I use a 'string' or a magic-number as the parameter value, then in my sproc I will do:
所以我应该使用'string'或magic-number作为参数值,然后在我的sproc中我会这样做:
IF(@blah = 'approved')
BEGIN
// bit banging here
END
2 个解决方案
#1
2
You could use a string, and a CASE construct:
您可以使用字符串和CASE构造:
CREATE PROCEDURE BitBang(@Flag AS VARCHAR(50), @Id AS INT)
AS
BEGIN
DECLARE @Bit INT
SET @BIT = CASE @Flag
WHEN 'approved' THEN 16
WHEN 'noapproved' THEN 16
WHEN 'fooflag' THEN 8
WHEN 'nofooflag' THEN 8
END
IF @Bit IS NOT NULL
BEGIN
IF LEFT(@Flag, 2) = 'no'
BEGIN
UPDATE TheTable SET BitField = BitField & ~@Bit WHERE Id = @Id
END
ELSE
BEGIN
UPDATE TheTable SET BitField = BitField | @Bit WHERE Id = @Id
END
END
END
#2
0
Why not use the old 0 and 1 for the flag? It is widely accepted as a bit switch already and there would be no confusion or misspelling as to what 0 and 1 mean. Unless you are saying that there will be more than 2 flags and that more than 1 flag will have the same ultimate meaning
为什么不使用旧的0和1作为标志?它已被广泛接受为一个位开关,并且不存在关于0和1意味着什么的混淆或拼写错误。除非您说将有超过2个标志,并且超过1个标志将具有相同的终极含义
#1
2
You could use a string, and a CASE construct:
您可以使用字符串和CASE构造:
CREATE PROCEDURE BitBang(@Flag AS VARCHAR(50), @Id AS INT)
AS
BEGIN
DECLARE @Bit INT
SET @BIT = CASE @Flag
WHEN 'approved' THEN 16
WHEN 'noapproved' THEN 16
WHEN 'fooflag' THEN 8
WHEN 'nofooflag' THEN 8
END
IF @Bit IS NOT NULL
BEGIN
IF LEFT(@Flag, 2) = 'no'
BEGIN
UPDATE TheTable SET BitField = BitField & ~@Bit WHERE Id = @Id
END
ELSE
BEGIN
UPDATE TheTable SET BitField = BitField | @Bit WHERE Id = @Id
END
END
END
#2
0
Why not use the old 0 and 1 for the flag? It is widely accepted as a bit switch already and there would be no confusion or misspelling as to what 0 and 1 mean. Unless you are saying that there will be more than 2 flags and that more than 1 flag will have the same ultimate meaning
为什么不使用旧的0和1作为标志?它已被广泛接受为一个位开关,并且不存在关于0和1意味着什么的混淆或拼写错误。除非您说将有超过2个标志,并且超过1个标志将具有相同的终极含义