试图解决位列的三个条件
Trying to resolve three conditions for a bit column
早上好,
这似乎不难做到,但我缺乏 TSQL 知识。如何解决位列的三个条件?这就是我所拥有的,但编译器当然不喜欢 CASE 中的 OR。
为了清楚起见,在 All 的情况下,我需要零列值和一列值。 Null 将被排除。
我的表格
Id Name LockedState Deleted
12 Doe, John 0 1
14 Doe, Johnny 0 1
15 Fischer, Julia 1 1
16 Hemsworth, Christopher 0 1
17 Getty, Kristyn 1 1
SELECT *
FROM MyTable
WHERE Deleted = @Deleted
AND LockState = CASE
WHEN @Assigned = 'ALL' THEN 0 OR 1
WHEN @Assigned = 'Assigned' THEN 1
WHEN @Assigned = 'UnAssigned' THEN 0
END
'All'
的期望输出
12 Doe, John 0 1
14 Doe, Johnny 0 1
15 Fischer, Julia 1 1
16 Hemsworth, Christopher 0 1
17 Getty, Kristyn 1 1
'Assigned'
的期望输出
15 Fischer, Julia 1 1
17 Getty, Kristyn 1 1
'Unassigned'
的期望输出
12 Doe, John 0 1
14 Doe, Johnny 0 1
16 Hemsworth, Christopher 0 1
你可以这样使用:
SELECT *
FROM MyTable
WHERE Deleted = @Deleted
AND (@Assigned = 'Assigned' AND LockState = 1
OR @Assigned = 'UnAssigned' AND LockState = 0
OR @Assigned = 'ALL')
或者像这样:
SELECT *
FROM MyTable
WHERE Deleted = @Deleted
AND LockState = CASE
WHEN @Assigned = 'ALL' THEN LockState
WHEN @Assigned = 'Assigned' THEN 1
WHEN @Assigned = 'UnAssigned' THEN 0
END
早上好,
这似乎不难做到,但我缺乏 TSQL 知识。如何解决位列的三个条件?这就是我所拥有的,但编译器当然不喜欢 CASE 中的 OR。
为了清楚起见,在 All 的情况下,我需要零列值和一列值。 Null 将被排除。
我的表格
Id Name LockedState Deleted
12 Doe, John 0 1
14 Doe, Johnny 0 1
15 Fischer, Julia 1 1
16 Hemsworth, Christopher 0 1
17 Getty, Kristyn 1 1
SELECT *
FROM MyTable
WHERE Deleted = @Deleted
AND LockState = CASE
WHEN @Assigned = 'ALL' THEN 0 OR 1
WHEN @Assigned = 'Assigned' THEN 1
WHEN @Assigned = 'UnAssigned' THEN 0
END
'All'
的期望输出12 Doe, John 0 1
14 Doe, Johnny 0 1
15 Fischer, Julia 1 1
16 Hemsworth, Christopher 0 1
17 Getty, Kristyn 1 1
'Assigned'
的期望输出15 Fischer, Julia 1 1
17 Getty, Kristyn 1 1
'Unassigned'
的期望输出12 Doe, John 0 1
14 Doe, Johnny 0 1
16 Hemsworth, Christopher 0 1
你可以这样使用:
SELECT *
FROM MyTable
WHERE Deleted = @Deleted
AND (@Assigned = 'Assigned' AND LockState = 1
OR @Assigned = 'UnAssigned' AND LockState = 0
OR @Assigned = 'ALL')
或者像这样:
SELECT *
FROM MyTable
WHERE Deleted = @Deleted
AND LockState = CASE
WHEN @Assigned = 'ALL' THEN LockState
WHEN @Assigned = 'Assigned' THEN 1
WHEN @Assigned = 'UnAssigned' THEN 0
END