简体   繁体   English

SQL 的无效参数查询是否从 Excel - VBA 选择空单元格

[英]Void out parameter of SQL query if empty cell selection from Excel - VBA

Instead of using Nested If statements, I was wondering if there is a way to void out parts of a string query if cell value is left blank.我想知道如果单元格值留空,是否有办法使部分字符串查询无效,而不是使用嵌套的 If 语句。

Cell structure is as below:细胞结构如下:

在此处输入图像描述

Cell values from these parameters will get passed into vba code that queries a database.这些参数中的单元格值将传递到查询数据库的 vba 代码中。 Ideally I don't want to create an individual query for each selection type - and I have it dynamically querying from location already.理想情况下,我不想为每种选择类型创建单独的查询——我已经让它从位置动态查询。 I want to extend the query to include possible combinations of start, end, value >, value <, while also making it so that if cell value is left blank, then ignore that parameter.我想扩展查询以包括开始、结束、值 >、值 < 的可能组合,同时还使其如果单元格值留空,则忽略该参数。 So say所以说

SELECT * 
from database 
WHERE location = 'cell_loc' 
AND Value >= 'cell_value' 
AND Value <= 'cell_value' 
AND Start >= 'cell_date' 
AND End <= 'cell_date'

Now imagine that Start is left blank, meaning I want to query from first data point in the database:现在想象 Start 留空,这意味着我想从数据库中的第一个数据点查询: 在此处输入图像描述

I could write a nested if to handle this, but was wondering if there was a way to void out a query parameter so that I could just have a single query fed to database with different parameters changing based off cell data?我可以编写一个嵌套的 if 来处理这个问题,但想知道是否有办法取消查询参数,以便我可以将一个查询提供给数据库,其中不同的参数会根据单元格数据发生变化?

Something along the lines of:类似于以下内容:

SELECT * 
from database 
WHERE location = 'cell_loc' 
AND Value >= 'cell_value' 
AND Value <= 'cell_value' 
AND Start >= 'cell_date' --> this would be voided out
AND End <= 'cell_date'

Using the coalesce() function you can put an equality condition in your WHERE clause.使用 coalesce() function 您可以在 WHERE 子句中放置一个相等条件。 This is a common SQL trick to deal with null parameters or null values in the data.这是处理数据中 null 参数或 null 值的常见 SQL 技巧。

SELECT * 
from database 
WHERE location = 'cell_loc' 
AND Value >= 'cell_value' 
AND Value <= 'cell_value' 
AND (Start >= 'cell_date' OR Start = coalesce('cell date', Start))
AND End <= 'cell_date'

Here's a very basic example:这是一个非常基本的示例:

Sub Tester()
    Dim sWhere As String, sql As String
    
    sql = "Select * from myTable "
    
    sWhere = ""
    BuildWhere sWhere, "id = <v>", Range("A5")
    BuildWhere sWhere, "pName = '<v>'", Range("B5")
    BuildWhere sWhere, "pDate > '<v>'", Range("C5")
    
    If Len(sWhere) > 0 Then
        sql = sql & " where " & sWhere
        Debug.Print sql
        'run query
    Else
        'don't run if no criteria ?
    End If

End Sub

'add a where clause only if `c` has a value
Sub BuildWhere(ByRef sWhere As String, test As String, c As Range)
    Dim v
    v = Trim(c.Value)
    If Len(v) > 0 Then
        If Len(sWhere) > 0 Then sWhere = sWhere & vbLf & " and "
        sWhere = sWhere & Replace(test, "<v>", v)
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM