将布尔逻辑语句的结果分配给T-sql中的位字段

发布于 2024-11-04 17:47:00 字数 1114 浏览 5 评论 0原文

根据 这个问题,我无法分配 a 的结果将布尔逻辑语句转换为位字段。我的问题是,我正在编写一个创建脚本,并且有一些依赖标志需要布尔语句才能完成。该脚本将以所有空值开始,用户只需输入他们想要的数据。该脚本将检测记录是否已存在。如果是这样,它将仅更新不为空的参数。如果不存在,脚本将创建一个新记录。我正在使用 sql server 2005。

我使用 ISNULL(@Setting1, 0) 计算出插入语句,以在未分配变量时设置默认值。我的问题来自于一个特定的设置,该设置依赖于其中两个设置标志。如果我需要添加一个设置标志,我还希望看到一种可以支持另一个设置标志的解决方案。

DECLARE @Setting1 bit
DECLARE @Setting2 bit
DECLARE @Setting3 bit

--... code snipped for setting the value

UPDATE
    MyTable
SET
    EnableSetting1 = ISNULL(@Setting1, EnableSetting1),
    EnableSetting1 = ISNULL(@Setting2, EnableSetting2),
    EnableSetting1 = ISNULL(@Setting3, EnableSetting3),
    EnableComplexSetting1 = ISNULL(@Setting1, EnableComplexSetting1),
    EnableComplexSetting2 = ?
WHERE
    Id = @MyId

从逻辑上讲, ? 看起来像 ISNULL(@Setting2, EnableSetting2) OR ISNULL(@Setting3, EnableSetting3) 但这显然是不正确的。我应该如何设置复杂设置2?如果逻辑上它是 ISNULL(@Setting2, EnableSetting2) OR ISNULL(@Setting3, EnableSetting3) OR ISNULL(@Setting4, EnableSetting4) 等,我该怎么做?

according to this question, I can't assign the result of a boolean logic statement into a bit field. My problem is that I am working on a creation script and I have a few dependent flags that require a boolean statement to complete. This script will start as all nulls and the user just enters the data they want to. The script will detect if the record already exists. If so, it will update only the parameters that are not null. If it doesn't exist, the script will create a new record. I am using sql server 2005.

I have the insert statement figured out using ISNULL(@Setting1, 0) to set a default value when the variable is not assigned. My problem comes with one particular setting which is dependent on 2 of the setting flags. I'd also like to see a solution that could support another setting flag if I needed to add one.

DECLARE @Setting1 bit
DECLARE @Setting2 bit
DECLARE @Setting3 bit

--... code snipped for setting the value

UPDATE
    MyTable
SET
    EnableSetting1 = ISNULL(@Setting1, EnableSetting1),
    EnableSetting1 = ISNULL(@Setting2, EnableSetting2),
    EnableSetting1 = ISNULL(@Setting3, EnableSetting3),
    EnableComplexSetting1 = ISNULL(@Setting1, EnableComplexSetting1),
    EnableComplexSetting2 = ?
WHERE
    Id = @MyId

Logically, the ? would look like ISNULL(@Setting2, EnableSetting2) OR ISNULL(@Setting3, EnableSetting3) but that is obviously incorrect. How should I set the complex setting 2? How would I do it if logically it was ISNULL(@Setting2, EnableSetting2) OR ISNULL(@Setting3, EnableSetting3) OR ISNULL(@Setting4, EnableSetting4), etc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

那片花海 2024-11-11 17:47:00

ISNULL 不返回布尔值 - 它只是确保在第一个表达式为 NULL 时使用提供的第二个值。

您需要使用如下内容:

UPDATE
    MyTable
SET
    EnableSetting1 = CASE WHEN @Setting1 IS NULL THEN 1 ELSE 0 END,
...

根据您要存储的布尔值,如果 @Setting1 确实为 NULL,您可能需要切换 THEN 和 中的两个值ELSE 情况....

通过这种方法,您应该还能够检查两个条件:

UPDATE MyTable
SET EnableComplexSetting2 = 
        CASE WHEN (Complex1 IS NULL AND Complex2 IS NULL) THEN 1 ELSE 0 END 

 ......

ISNULL doesn't return a boolean - it just makes sure to use the second value provided IF the first expression is NULL.

You'll need to use something like this:

UPDATE
    MyTable
SET
    EnableSetting1 = CASE WHEN @Setting1 IS NULL THEN 1 ELSE 0 END,
...

Depending on which boolean you want to store if @Setting1 is really NULL you might need to switch the two values in the THEN and ELSE cases....

With this approach you should be able to also check two conditions:

UPDATE MyTable
SET EnableComplexSetting2 = 
        CASE WHEN (Complex1 IS NULL AND Complex2 IS NULL) THEN 1 ELSE 0 END 

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