SQL Case语句如何获取值而不管其他结果如何

时间:2014-05-12 14:37:19

标签: sql sql-server-2008 tsql

以下是我的发言:

SELECT  
    CASE 
        WHEN @UserRole = 1 THEN 1
        ELSE 0
    END AS [CanEdit],
        F.FundingStreamName       
FROM    FundingStream AS F
        LEFT JOIN Projects AS P ON P.FundingStream = F.FundingStreamID 
WHERE   ProjectNumber = @ProjectNumber 

我注意到如果FundingStreamID为空,case语句也不会返回任何内容,无论是否有资金流,我如何才能执行case语句?感谢。

2 个答案:

答案 0 :(得分:0)

我认为问题在于你的where条款是"撤消"你的左外连接。尝试将条件移至on子句:

SELECT (CASE WHEN @UserRole = 1 THEN 1
             ELSE 0
        END) AS [CanEdit],
       F.FundingStreamName       
FROM FundingStream F LEFT JOIN
     Projects P
     ON P.FundingStream = F.FundingStreamID AND
        P.ProjectNumber = @ProjectNumber ;

答案 1 :(得分:0)

使用@MicSim回答:

SELECT  
    CASE 
        WHEN @UserRole = 1 THEN 1
        ELSE 0
    END AS [CanEdit],
        F.FundingStreamName       
FROM    FundingStream AS F
        RIGHT JOIN Projects AS P ON P.FundingStream = F.FundingStreamID 
WHERE   ProjectNumber = @ProjectNumber 

再次感谢您的帮助!