T-SQL 中的布尔值“NOT"不适用于“位"数据类型? [英] Boolean 'NOT' in T-SQL not working on 'bit' datatype?

查看:29
本文介绍了T-SQL 中的布尔值“NOT"不适用于“位"数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试执行单个布尔非操作,似乎在 MS SQL Server 2005 下,以下块不起作用

Trying to perform a single boolean NOT operation, it appears that under MS SQL Server 2005, the following block does not work

DECLARE @MyBoolean bit;
SET @MyBoolean = 0;
SET @MyBoolean = NOT @MyBoolean;
SELECT @MyBoolean;

相反,我越来越成功

DECLARE @MyBoolean bit;
SET @MyBoolean = 0;
SET @MyBoolean = 1 - @MyBoolean;
SELECT @MyBoolean;

然而,这看起来有点像否定这样简单的表达方式.

Yet, this looks a bit a twisted way to express something as simple as a negation.

我错过了什么吗?

推荐答案

使用 ~ 操作符:

DECLARE @MyBoolean bit
SET @MyBoolean = 0
SET @MyBoolean = ~@MyBoolean
SELECT @MyBoolean

这篇关于T-SQL 中的布尔值“NOT"不适用于“位"数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆