【发布时间】:2021-06-05 19:18:48
【问题描述】:
在 SQL Server 中,我有多个 bit 列。如果 product = False 或 Category = False,我想返回 False。
我的测试代码:
declare @b0 bit
declare @b1 bit
set @b0 = 0
set @b1 = 1
select (@b0 and @b1)
select (@b0 + @b1)
select (@b0 = @b1)
但是所有 3 个选择都崩溃了。
我的实际代码sn-p:
select
c.bCatActive, -- I want to dispose of this line.
p.bProdActive, -- I want to dispose of this line.
(c.bCatActive and p.bProdActive) as IsActive -- I want to retrieve this line but doesn't work.
from
Category c, Product p
where
p.ProductID = 999
and c.CategoryID = p.CategoryID
当然,我可以用两个 If-Thens 来做到这一点,但是我当前的查询是一个干净的查询,所以我希望能在一行中做到这一点。
【问题讨论】:
-
Bad habits to kick : using old-style JOINs - 旧式 逗号分隔的表格列表 样式已替换为 ANSI 中的 proper ANSI
JOIN语法-92 SQL 标准(差不多 30 年前),不鼓励使用它 -
好奇:如果这两个字段一起有效地定义了四个独立的状态,那么它们可能会更好地作为带有检查约束的单个
tinyint或char
标签: sql sql-server-2008