检查 SQL Server 中列的值是否为空

Check if a column#39;s value is null in SQL Server(检查 SQL Server 中列的值是否为空)
本文介绍了检查 SQL Server 中列的值是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

不敢相信我坚持这个,但我如何检查我返回的值在 select 语句中是否为空

Can't believe I am stuck with this but how can I check that value I am returning is null in select statement

IF EXISTS(SELECT TU.Tagged FROM TopicUser TU 
          WHERE TU.TopicId = @TopicId and TU.UserId = @UserId)
BEGIN
    --do stuff
END

TU.Tagged 的值是 NULL 但它确实进入了条件.心目中它不存在.

The value of TU.Tagged is NULL but yet it does go into the condition. In mind it does not exist.

推荐答案

看起来你需要这样的东西:

It looks like you need something like:

IF EXISTS(SELECT TU.Tagged 
          FROM TopicUser TU 
          WHERE TU.TopicId = @TopicId 
               AND TU.UserId = @UserId 
               AND TU.Tagged IS NOT NULL)
BEGIN
    --do stuff

END

否则,您只会检查是否存在符合您条件的记录,但这些记录在 TU.Tagged 列中可能具有 NULL 值.

Otherwise, you're checking only if records meeting your criteria exist, but those records could have a NULL value in the TU.Tagged column.

这篇关于检查 SQL Server 中列的值是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Error casting varchar containing a number expressed in scientific notation to decimal(将包含以科学记数法表示的数字的 varchar 转换为十进制时出错)
Calculate cumulative product value(计算累积产品价值)
SQL Server loop for changing multiple values of different users(用于更改不同用户的多个值的 SQL Server 循环)
SQL Server 2008: Can a multi-statement UDF return a UDT?(SQL Server 2008:多语句 UDF 能否返回 UDT?)
Merge columns in sql(合并sql中的列)
SQL Column is invalid in ORDER BY, not contained in aggregate or GROUP BY(SQL 列在 ORDER BY 中无效,未包含在聚合或 GROUP BY 中)