SQL Server - 布尔文字?

发布于 2024-12-01 03:17:39 字数 238 浏览 2 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

ˇ宁静的妩媚 2024-12-08 03:17:39

SQL Server 没有布尔数据类型。正如@Mikael 所指出的,最接近的近似值是位。但这是数字类型,而不是布尔类型。此外,它仅支持 2 个值 - 01(以及一个非值,NULL)。

SQL(标准 SQL 以及 T-SQL 方言)描述了三值逻辑。 SQL 的布尔类型应支持 3 个值 - TRUEFALSEUNKNOWN(以及非值 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 or 1 (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 and UNKNOWN (and also, the non-value NULL). So bit 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".

沉默的熊 2024-12-08 03:17:39
select * from SomeTable where 1=1
select * from SomeTable where 1=1
墨落画卷 2024-12-08 03:17:39

大多数数据库都会接受这一点:

select * from SomeTable where true

但是有些数据库(例如 SQL Server、Oracle)没有布尔类型。在这些情况下,您可以使用:

select * from SomeTable where 1=1

顺便说一句,如果手动构建 sql where 子句,这是简化代码的基础,因为您可以避免必须知道要添加到 where 子句的条件是否是 < em>第一个一个(前面应该有“WHERE”),或者后续一个(前面应该有“AND”< /代码>)。通过始终以 "WHERE 1=1" 开头,添加到 where 子句的所有条件(如果有)都以 "AND" 开头。

Most databases will accept this:

select * from SomeTable where true

However some databases (eg SQL Server, Oracle) do not have a boolean type. In these cases you may use:

select * from SomeTable where 1=1

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".

等风来 2024-12-08 03:17:39

其他任何答案中都没有提到这一点。如果您想要一个值(应该)水合为布尔值,您可以使用

CONVERT(bit, 0) -- false
CONVERT(bit, 1) -- true

这给你一个不是布尔值的位。您不能在 if 语句中使用该值,例如:

IF CONVERT(bit, 0)
BEGIN
    print 'Yay'
END

不会解析。你仍然需要写

IF CONVERT(bit, 0) = 0

所以它不是很有用。

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:

IF CONVERT(bit, 0)
BEGIN
    print 'Yay'
END

woudl not parse. You would still need to write

IF CONVERT(bit, 0) = 0

So its not terribly useful.

望她远 2024-12-08 03:17:39

根据微软
搜索的语法是

[ WHERE <search_condition> ]*

搜索条件是:

<search_condition> ::= 
    { [ NOT ] <predicate> | ( <search_condition> ) } 
    [ { AND | OR } [ NOT ] { <predicate> | ( <search_condition> ) } ] 
[ ,...n ] 

谓词是:

<predicate> ::= 
    { expression { = | < > | ! = | > | > = | ! > | < | < = | ! < } expression 

如您所见,您总是必须编写两个表达式进行比较。
这里的搜索条件是布尔表达式,如 1=1、a!=b

不要将搜索表达式与布尔常量混淆,如'True' 或 <强>“假”。您可以将布尔常量分配给 BIT 变量,

DECLARE @B BIT
SET @B='True'

但在 TSQL 中您不能使用布尔常量来代替布尔表达式,如下所示:

SELECT * FROM Somewhere WHERE 'True'

它将不起作用。

但是您可以使用布尔常量来构建双边搜索表达式,如下所示:

SEARCH * FROM Somewhere WHERE 'True'='True' 

According to Microsoft:
syntax for searching is

[ WHERE <search_condition> ]*

And search condition is:

<search_condition> ::= 
    { [ NOT ] <predicate> | ( <search_condition> ) } 
    [ { AND | OR } [ NOT ] { <predicate> | ( <search_condition> ) } ] 
[ ,...n ] 

And predicate is:

<predicate> ::= 
    { expression { = | < > | ! = | > | > = | ! > | < | < = | ! < } expression 

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

DECLARE @B BIT
SET @B='True'

but in TSQL you can not use boolean constants instead of boolean expressions like this:

SELECT * FROM Somewhere WHERE 'True'

It will not work.

But you can use boolean constants to build two-sided search expression like this:

SEARCH * FROM Somewhere WHERE 'True'='True' 
傾城如夢未必闌珊 2024-12-08 03:17:39

SQL Server 没有字面上的 true 或 false 值。在极少数情况下,您需要使用 1=1 方法(或类似方法)。

一种选择是为 true 和 false 创建自己的命名变量

DECLARE @TRUE bit
DECLARE @FALSE bit
SET @TRUE = 1
SET @FALSE = 0

select * from SomeTable where @TRUE = @TRUE

,但这些变量仅存在于批处理范围内(您必须在要使用它们的每个批处理中重新声明它们)

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

DECLARE @TRUE bit
DECLARE @FALSE bit
SET @TRUE = 1
SET @FALSE = 0

select * from SomeTable where @TRUE = @TRUE

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)

小傻瓜 2024-12-08 03:17:39

您可以使用值'TRUE''FALSE'
来自 https://learn.microsoft。 com/en-us/sql/t-sql/data-types/bit-transact-sql

字符串值 TRUE 和 FALSE 可以转换为位值: TRUE
转换为 1,FALSE 转换为 0。

You can use the values 'TRUE' and 'FALSE'.
From https://learn.microsoft.com/en-us/sql/t-sql/data-types/bit-transact-sql:

The string values TRUE and FALSE can be converted to bit values: TRUE
is converted to 1 and FALSE is converted to 0.

奈何桥上唱咆哮 2024-12-08 03:17:39

如何在 SQL Server 中写入文字布尔值?
从 SomeTable 中选择 *,其中 PSEUDO_TRUE

没有这样的事情。

您必须使用 = <; 将该值与某些内容进行比较>就像...。在 SQL Server 中最接近的布尔值是 。这是一个整数,其值为 null01

How to write literal boolean value in SQL Server?
select * from SomeTable where PSEUDO_TRUE

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 values null, 0 and 1.

笑着哭最痛 2024-12-08 03:17:39

您应该认为“真值”是除 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.

我要还你自由 2024-12-08 03:17:39

您可以使用“True”或“False”字符串来模拟布尔类型数据。

Select *
From <table>
Where <columna> = 'True'

我认为这种方式可能比直接输入 1 慢,因为它是通过 Convert_implicit 函数解决的。

You can use 'True' or 'False' strings for simulate bolean type data.

Select *
From <table>
Where <columna> = 'True'

I think this way maybe slow than just put 1 because it's resolved with Convert_implicit function.

星星的轨迹 2024-12-08 03:17:39

我质疑在 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.

残疾 2024-12-08 03:17:39

我希望这能回答问题的意图。尽管 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!

給妳壹絲溫柔 2024-12-08 03:17:39
select * from SomeTable where null is null

或者

select * from SomeTable where null is not null

也许这就是最好的表现?

select * from SomeTable where null is null

or

select * from SomeTable where null is not null

maybe this is the best performance?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文