如果SQL WHERE条款中的ELSE条件

时间:2013-08-08 05:35:39

标签: sql sql-server-2008 select conditional where-clause

我的查询

DECLARE @AutoApprove BIT    
    SET @AutoApprove = (
            SELECT AutoApprove
            FROM dbo.CommentBox_Setting
            WHERE UserModuleID = @myModuleID
                AND PortalID = @portalID
                AND CultureCode = @cultureCode
            )

从此我将获得1 OR 0(TRUE或FALSE)此外我还有

   SELECT * FROM ComentBox_Comment
                WHERE UpperModuleID = @UpperModuleID
                    AND ModuleID = @myModuleID
                    AND portalID = @portalID
                    AND cultureCode = @cultureCode
                    AND //Here i need to check condition 

(IF(@AutoApprove=0){ THEN isapprove=1}else {do not check})

请注意,此处isapprove是table filedName

我知道,我可以通过长时间的查询来做到这一点,我需要简单易行的方法。 帮助我。

1 个答案:

答案 0 :(得分:2)

尝试类似

的内容
AND CASE WHEN @AutoApprove=0 THEN isapprove ELSE 1 END = 1

这将检查isapprove = 1 @AutoApprove = 0,或1=1(ignore)

CASE (Transact-SQL)