使用WHERE子句重新计算简单的SQL语句

时间:2015-05-14 18:00:06

标签: c# sql asp.net .net sql-server

我觉得必须有一种更有效的方法来做到这一点。我想允许来电者拉出所有书籍或未隐藏的书籍(见下文)

if isnull(@ShowHiddenBooks, 0) = 1
    begin
        select
            (long list of fields)
        from
            MyTable
        where
            MyField = @SomeField
    end
else
    begin
        select
            (long list of fields)
        from
            MyTable
        where
            MyField = @SomeField and
            IsHidden = 0    
    end

有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分:4)

Select *
from MyTable
where MyField = @SomeField and
(isHidden = 0 or @showHiddenBooks = 1)

答案 1 :(得分:2)

这与以下内容相同:

 select (long list of fields)
    from
        MyTable
    where
        MyField = @SomeField and
        (IsHidden = 0 or isnull(@ShowHiddenBooks, 0) = 1)