SQL Server - 布尔文字?
如何在 SQL Server 中写入文字布尔值?请参阅示例使用:
select * from SomeTable where PSEUDO_TRUE
另一个示例:
if PSEUDO_TRUE
begin
select 'Hello, SQL!'
end
注意:上面的查询与我将如何使用它无关。它只是为了测试文字布尔值。
How to write literal boolean value in SQL Server? See sample use:
select * from SomeTable where PSEUDO_TRUE
another sample:
if PSEUDO_TRUE
begin
select 'Hello, SQL!'
end
Note: The query above has nothing to do with how I'm going to use it. It is just to test the literal boolean.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
SQL Server 没有布尔数据类型。正如@Mikael 所指出的,最接近的近似值是位。但这是数字类型,而不是布尔类型。此外,它仅支持 2 个值 -
0
或1
(以及一个非值,NULL
)。SQL(标准 SQL 以及 T-SQL 方言)描述了三值逻辑。 SQL 的布尔类型应支持 3 个值 -
TRUE
、FALSE
和UNKNOWN
(以及非值NULL
)代码>)。所以bit
实际上并不是一个很好的匹配。鉴于 SQL Server 不支持该数据类型,我们不应期望能够写入该“类型”的文字。
SQL Server doesn't have a boolean data type. As @Mikael has indicated, the closest approximation is the bit. But that is a numeric type, not a boolean type. In addition, it only supports 2 values -
0
or1
(and one non-value,NULL
).SQL (standard SQL, as well as T-SQL dialect) describes a Three valued logic. The boolean type for SQL should support 3 values -
TRUE
,FALSE
andUNKNOWN
(and also, the non-valueNULL
). Sobit
isn't actually a good match here.Given that SQL Server has no support for the data type, we should not expect to be able to write literals of that "type".
大多数数据库都会接受这一点:
但是有些数据库(例如 SQL Server、Oracle)没有布尔类型。在这些情况下,您可以使用:
顺便说一句,如果手动构建 sql where 子句,这是简化代码的基础,因为您可以避免必须知道要添加到 where 子句的条件是否是 < em>第一个一个(前面应该有
“WHERE”
),或者后续一个(前面应该有“AND”< /代码>)。通过始终以
"WHERE 1=1"
开头,添加到 where 子句的所有条件(如果有)都以"AND"
开头。Most databases will accept this:
However some databases (eg SQL Server, Oracle) do not have a boolean type. In these cases you may use:
BTW, if building up an sql where clause by hand, this is the basis for simplifying your code because you can avoid having to know if the condition you're about to add to a where clause is the first one (which should be preceded by
"WHERE"
), or a subsequent one (which should be preceded by"AND"
). By always starting with"WHERE 1=1"
, all conditions (if any) added to the where clause are preceded by"AND"
.其他任何答案中都没有提到这一点。如果您想要一个值(应该)水合为布尔值,您可以使用
CONVERT(bit, 0) -- false
CONVERT(bit, 1) -- true
这给你一个不是布尔值的位。您不能在 if 语句中使用该值,例如:
不会解析。你仍然需要写
所以它不是很有用。
This isn't mentioned in any of the other answers. If you want a value that orms (should) hydrate as boolean you can use
CONVERT(bit, 0) -- false
CONVERT(bit, 1) -- true
This gives you a bit which is not a boolean. You cannot use that value in an if statement for example:
woudl not parse. You would still need to write
So its not terribly useful.
根据微软:
搜索的语法是
搜索条件是:
谓词是:
如您所见,您总是必须编写两个表达式进行比较。
这里的搜索条件是布尔表达式,如 1=1、a!=b
不要将搜索表达式与布尔常量混淆,如'True' 或 <强>“假”。您可以将布尔常量分配给 BIT 变量,
但在 TSQL 中您不能使用布尔常量来代替布尔表达式,如下所示:
它将不起作用。
但是您可以使用布尔常量来构建双边搜索表达式,如下所示:
According to Microsoft:
syntax for searching is
And search condition is:
And predicate is:
As you can see, you always have to write two expressions to compare.
Here search condition is boolean expression like 1=1, a!=b
Do not confuse search expressions with boolean constants like 'True' or 'False'. You can assign boolean constants to BIT variables
but in TSQL you can not use boolean constants instead of boolean expressions like this:
It will not work.
But you can use boolean constants to build two-sided search expression like this:
SQL Server 没有字面上的 true 或 false 值。在极少数情况下,您需要使用
1=1
方法(或类似方法)。一种选择是为 true 和 false 创建自己的命名变量
,但这些变量仅存在于批处理范围内(您必须在要使用它们的每个批处理中重新声明它们)
SQL Server does not have literal true or false values. You'll need to use the
1=1
method (or similar) in the rare cases this is needed.One option is to create your own named variables for true and false
But these will only exist within the scope of the batch (you'll have to redeclare them in every batch in which you want to use them)
您可以使用值
'TRUE'
和'FALSE'
。来自 https://learn.microsoft。 com/en-us/sql/t-sql/data-types/bit-transact-sql:
You can use the values
'TRUE'
and'FALSE'
.From https://learn.microsoft.com/en-us/sql/t-sql/data-types/bit-transact-sql:
没有这样的事情。
您必须使用
= <; 将该值与某些内容进行比较>就像...
。在 SQL Server 中最接近的布尔值是 位。这是一个整数,其值为null
、0
和1
。There is no such thing.
You have to compare the value with something using
= < > like ...
. The closest you get a boolean value in SQL Server is the bit. And that is an integer that can have the valuesnull
,0
and1
.您应该认为“真值”是除 0 之外的所有值,而不仅仅是 1。
因此,您应该写 1<>0,而不是 1=1。
因为当您使用参数(@param <> 0)时,您可能会遇到一些转换问题。
最知名的是 Access,它将控件上的 True 值转换为 -1 而不是 1。
You should consider that a "true value" is everything except 0 and not only 1.
So instead of 1=1 you should write 1<>0.
Because when you will use parameter (@param <> 0) you could have some conversion issue.
The most know is Access which translate True value on control as -1 instead of 1.
您可以使用“True”或“False”字符串来模拟布尔类型数据。
我认为这种方式可能比直接输入 1 慢,因为它是通过 Convert_implicit 函数解决的。
You can use 'True' or 'False' strings for simulate bolean type data.
I think this way maybe slow than just put 1 because it's resolved with Convert_implicit function.
我质疑在 TSQL 中使用布尔值的价值。
每次我开始希望得到布尔值和布尔值时对于循环,我意识到我正在像 C 程序员一样解决这个问题。不是 SQL 程序员。当我换档时,问题就变得微不足道了。
在 SQL 中,您正在操作数据集。 “WHERE BOOLEAN”无效,因为不会更改您正在使用的集合。您需要将每一行与某些内容进行比较,以使过滤子句有效。表/结果集是 iEnumerable,SELECT 语句是 FOREACH 循环。
是的,“WHERE IsAdmin = True”比“WHERE IsAdmin = 1”更易读
。是的,动态生成 TSQL 时,“WHERE True”比“WHERE 1=1, ...”更好。
也许,将布尔值传递给存储过程可能会使 if 语句更具可读性。
但大多数情况下,IF、WHILE 和 IF 的数量越多。 TSQL 中的临时表越多,您就越有可能应该重构它。
I question the value of using a Boolean in TSQL.
Every time I've started wishing for Booleans & For loops I realised I was approaching the problem like a C programmer & not a SQL programmer. The problem became trivial when I switched gears.
In SQL you are manipulating SETs of data. "WHERE BOOLEAN" is ineffective, as does not change the set you are working with. You need to compare each row with something for the filter clause to be effective. The Table/Resultset is an iEnumerable, the SELECT statement is a FOREACH loop.
Yes, "WHERE IsAdmin = True" is nicer to read than "WHERE IsAdmin = 1"
Yes, "WHERE True" would be nicer than "WHERE 1=1, ..." when dynamically generating TSQL.
and maybe, passing a Boolean to a stored proc may make an if statement more readable.
But mostly, the more IF's, WHILE's & Temp Tables you have in your TSQL, the more likely you should refactor it.
我希望这能回答问题的意图。尽管 SQL Server 中没有布尔值,但如果您的数据库具有从 Access 翻译而来的布尔类型,则在 Access 中有效的短语是“...WHERE Foo”(Foo 是布尔列名称)。它可以替换为“...WHERE Foo<>0”...并且这有效。祝你好运!
I hope this answers the intent of the question. Although there are no Booleans in SQL Server, if you have a database that had Boolean types that was translated from Access, the phrase which works in Access was "...WHERE Foo" (Foo is the Boolean column name). It can be replaced by "...WHERE Foo<>0" ... and this works. Good luck!
或者
也许这就是最好的表现?
or
maybe this is the best performance?