以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  [求助]sql后台多条件查询问题  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=194900)

--  作者:bigeng
--  发布时间:2025/2/3 21:30:00
--  [求助]sql后台多条件查询问题
我用的多条件查询代码一直用的都是参照帮助里面的查询代码,但是会存在个问题,就是特别容易出现查询超时的情况,因此希望改成直接后台查询。但是上网查了一下,使用sql进行多条件查询,涉及到动态拼接等情况,看的我云里雾里的,也没有看懂,因此,希望各位大佬给看一下,如何将下面的查询改成直接后台查询的情况(数据库为sql server2008)。
Dim Filter As String
With e.Form.Controls("SFLB")
    If .Value IsNot Nothing Then
        If Filter > "" Then
            Filter = Filter & " And "
        End If
        Filter = Filter & "收发类别 like \'%" & .Value & "%\'"
    End If
End With

With e.Form.Controls("HT")
    If .Value IsNot Nothing Then
        If Filter > "" Then
            Filter = Filter & " And "
        End If
        Filter = Filter & "提货单号 like \'%" & .Value & "%\'"
    End If
End With

With e.Form.Controls("StartDate")
    If .Value IsNot Nothing Then
        If Filter > "" Then
            Filter = Filter & " And "
        End If
        Filter = Filter & "过磅日期 >= \'" & .Value & "\'"
    End If
End With
With e.Form.Controls("EndDate")
    If .Value IsNot Nothing Then
        If Filter > "" Then
            Filter = Filter & " And "
        End If
        Filter = Filter & "过磅日期 <= \'" & .Value & "\'"
    End If
End With

if Filter > "" Then
    DataTables("收货登记明细表").LoadFilter = Filter
    DataTables("收货登记明细表").Load
End If

--  作者:有点蓝
--  发布时间:2025/2/4 9:51:00
--  
上面的代码就已经是后台查询了呀。测试有什么问题?
--  作者:bigeng
--  发布时间:2025/2/4 10:53:00
--  
就是特别容易出现查询超时的情况。
--  作者:有点蓝
--  发布时间:2025/2/4 11:10:00
--  
那是数据库优化的问题。把条件里的列都添加到索引里