[authorityID] (TinyInt)
列永远不会是null
。
如果没有行,我想要的是返回15
。通过下面的查询,如果没有行,我什么也得不到:
select top 1 isnull([authorityID],15)
from [docAuthority] with (nolock)
where [grpID] = 0 and [sID] = 42
答案 0 :(得分:3)
SELECT authorityId = isnull(( SELECT [authorityID]
FROM [docAuthority] WITH ( NOLOCK )
WHERE [grpID] = 0
AND [sID] = 42
), 15)
答案 1 :(得分:1)
如您所述,如果查询未返回任何行,则无法应用isnull
。一个肮脏的技巧是使用union all
和(ab)使用top
构造:
SELECT TOP 1 authorityID
FROM (SELECT authorityID
FROM [docAuthority] WITH (nolock)
WHERE [grpID] = 0 AND [sID] = 42
UNION ALL
SELECT 15) t