0

我有一个县参数,如果它为空,那么我想选择协作中的所有县,如果它不为空,那么我只想要那个县的值。如何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
4

2 回答 2

0

利用:

CountryId = case when @counrtyid is null then CountryId else @CounrtyId end
于 2013-11-08T23:29:08.430 回答
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)
于 2013-11-08T23:15:05.810 回答