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

2021-09-10 00:00:00 sql tsql 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.

相关文章