我需要更改此代码,但不知道该怎么做。
我需要它按顺序检查这些表中的数据
NodeMACAddresses,
MAC地址,
如果找不到数据,则显示“没有Mac地址”
来自SNMP的Mac地址存储在自动称为NodeMacAddresses的表中,但是某些单元不响应SNMP,因此我将它们手动添加到我创建的名为MAC_address的表中。因此,我需要首先检查NodeMACAddresses中是否有数据,然后检查MAC_address,然后如果两个表都包含NULL,则键入“ No Mac Address”。
select coalesce(max(MAC), 'No Mac Address')
from NodeMACAddresses
where NodeID = $@nodeid@;
答案 0 :(得分:2)
嗯。 。 。这是一种方法:
select coalesce((select max(MAC)
from NodeMACAddresses
where NodeID = $@nodeid@
),
(select max(MAC)
from MACAddress
where NodeID = $@nodeid@
),
'No Mac Address'
)
答案 1 :(得分:0)
您可以对所有结果进行排名,然后拉出第一个。
SELECT
*
FROM
(
SELECT
NodeId,MAC,Priority,
RN=ROW_NUMBER() OVER(PARTITION BY NodeID ORDER BY Priority DESC)
FROM
(
SELECT NodeID,MAC, Priority=1 FROM NodeMACAddresses WHERE NodeID=@NodeID
UNION
SELECT NodeID,MAC, Priority=2 FROM MAC_Address WHERE NodeID=@NodeID
UNION
SELECT NodeID=@nodeid, MAC = 'No Mac Address', Priority=3
)AS A
)AS B
WHERE
RN=1
或者没有排名
SELECT
NodeId=@NodeID,
MAC=COALESCE(A.MAC,B.MAC,C.MAC)
FROM
(
SELECT MAC = 'No Mac Address'
)AS C
LEFT OUTER JOIN NodeMACAddresses A ON A.NodeID=@NodeID
LEFT OUTER JOIN MAC_Address B ON B.NodeID=@NodeID