sql-server - 具有选择和空检查的情况

标签 sql-server t-sql sql-server-2012

select a.AgentId,
       case(select acs.IsConnected
            from rmm.tblAgentConnectionStatus acs
            where acs.AgentName = a.AgentName)
        when 1 then 'True'
        when 0 then 'False'
        when null then 'False'
        end as ConnectionStatus
from Sentilan2.rmm.tblAgent a
order by AgentName asc

我有上述内容,但当它们在tblAgent中没有相应的行时,它会导致ConnectionStatus为空。

当列为空时,结果是否可能为False

电流输出

AgentId                                 ConnectionStatus
010D0206-5D8C-4AB1-90B6-7BD0C2773E22    True
CA4C48DD-3D2E-4948-9F93-254CDF081658    True
1DB90EE5-D96A-4071-8F51-26B3130EC6D4    NULL
ACA694D0-0C1D-45BA-80DD-273F41BD70B1    NULL
941B539B-7CA0-4472-ABCD-2777AE8B2E5D    NULL
1E7DDA4D-C119-4E47-8478-277952024FD1    NULL

我希望这些 null 为 false。

最佳答案

您可以使用 coalesce()isnull() 来替换 null 的值:

select a.AgentId,
       case coalesce((select acs.IsConnected
            from rmm.tblAgentConnectionStatus acs
            where acs.AgentName = a.AgentName),0)
        when 1 then 'True'
        when 0 then 'False'
        end as ConnectionStatus
from Sentilan2.rmm.tblAgent a
order by AgentName asc

或者只是else 'False':

select a.AgentId,
       case (select acs.IsConnected
            from rmm.tblAgentConnectionStatus acs
            where acs.AgentName = a.AgentName)
        when 1 then 'True'
        else 'False'
        end as ConnectionStatus
from Sentilan2.rmm.tblAgent a
order by AgentName asc

关于sql-server - 具有选择和空检查的情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46176796/

相关文章:

sql - 从包含每月条目的表生成日期范围

sql-server - Microsoft SQL Server Management Studio - 带有锁定附加信息的警报

python - 使用 Python 脚本在 MS SQL 数据库中创建表时出现问题

sql - 如何根据字母排序顺序设置 int 值

sql - 创建存储过程语法,与 GO 的使用相关

sql-server - 如何查找 SQL Server *.ndf 文件内容

t-sql - 如何在 NVARCHAR 类型列上使用聚合函数获取列中的第一个可用值(不是 MIN)?

sql - 选择 'EXIST' 或 'NotExist' 作为列

SQL Server XQuery 错误 : A node or set of nodes is required for number()

t-sql - 在 SQL Server 中转换 MS Access 动态列