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/