SQL Server位逻辑操作 - 需要一些帮助

时间:2011-03-31 11:51:22

标签: sql-server

说我有以下内容:

Declare @LineA int
Declare @LineB int
Declare @LineC int
Declare @LineD int
Declare @LineRequested int
Declare @LineTaken int

Set @LineA = 1
Set @LineB = 2
Set @LineC = 4
Set @LineD = 8

Set @LineRequested = 11 -- @LineA | @LineB | @LineD

Set @LineTaken = 2 -- Matches a line in @LineRequested (@LineB)
Set @LineTaken = 4 -- Does not match (Line C was not one of the lines OR'd)

如何测试@LineTaken中的值是否构成@LineRequested中 ONE 的值?在上面的例子中,当@LineTaken = 2时,它确实匹配其中一个值(@LineA | @LineB)。但是,在第二个,它没有。我怎样才能以编程方式确定这个?

2 个答案:

答案 0 :(得分:2)

SELECT CASE WHEN @LineRequested | 2 = @LineRequested THEN 'Y' ELSE 'N'  END,
       CASE WHEN @LineRequested | 4 = @LineRequested THEN 'Y' ELSE 'N'  END

答案 1 :(得分:2)

尝试:

SELECT CASE WHEN @LineRequested & @LineTaken > 0 THEN 'Y' ELSE 'N' END