我有一个县参数,如果它是null,那么我想选择协作中的所有县,如果它不为空,那么我只想要那个县的值。如何正确设置IF
语句? IF
语句的第一部分可能会带回多个县ID,这就是我将WHERE CountyId
放入其中的原因。
SELECT a.CountyId, co.Description as County
FROM Application as a
INNER JOIN Dictionary.Counties as co on a.CountyId = co.Id
WHERE co.Id in
IF @pCountyId is null then
BEGIN
SELECT cc1.CountyId
FROM CountyCollaboration as cc1
WHERE cc1.CollaborationId = (SELECT cc2.CollaborationId
FROM CountyCollaboration as cc2
WHERE cc2.CountyId = @pHiddenCountyId)
END
ELSE
BEGIN
SELECT @pCountyId
END
答案 0 :(得分:0)
select
a.CountyId,
co.Description as County
from
Application as a
inner join
Dictionary.Counties as co
on a.CountyId = co.Id
inner join
CountyCollaboration cc1
on co.id = cc1.CountyId
inner join
CountyCollaboration cc2
on cc1.CollaborationId = cc2.CollaborationId
where
c2.CountyId = IsNull(@CountyId, @pHiddenCountyId)
答案 1 :(得分:0)
使用:
CountryId = case when @counrtyid is null then CountryId else @CounrtyId end