将布尔逻辑语句的结果分配给T-sql中的位字段
根据 这个问题,我无法分配 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ISNULL
不返回布尔值 - 它只是确保在第一个表达式为 NULL 时使用提供的第二个值。您需要使用如下内容:
根据您要存储的布尔值,如果 @Setting1 确实为 NULL,您可能需要切换 THEN 和 中的两个值
ELSE
情况....通过这种方法,您应该还能够检查两个条件:
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:
Depending on which boolean you want to store if
@Setting1
is really NULL you might need to switch the two values in theTHEN
andELSE
cases....With this approach you should be able to also check two conditions: