从多个多选列表框构建 SQL 时遇到困难

发布于 2024-12-29 19:37:31 字数 939 浏览 0 评论 0原文

我有一个表单,其中大约有 6 个多选列表框。列表框由不同的表填充。我可以将列表框中的选择存储在变量中,然后在 SQL 查询中使用这些变量。该变量存储从 listBox 中选择的项目,如下所示:

If clientList.SelCount > 0 Then

    For i = 0 To clientList.ListCount - 1

      If clientList.Selected(i) Then
           If Len(criteria_cl) = 0 Then
               criteria_cl = Chr(39) & clientList.List(i) & Chr(39)
      Else
         criteria_cl = criteria_cl & "," & Chr(39) & clientList.List(i) & Chr(39)
      End If

 End If
Next

 Else 
 End If

SQL 查询如下,带有更多称为 criteria_xx 的变量,

 strsql = "select * from pmt_hist_dmart_step2 where dbr_portfolio in (" & criteria_pf & ") and DBR_CLIENT in (" & criteria_cl & _
 ") and DBR_ACCT_TYPE in (" & criteria_ac & ")..... ;"

如何处理用户未进行选择的情况。目前,criteria_xx 变量变为空白,并且 IN 子句包含 ('') 并引发错误。

请提供一些关于如何克服这个问题的建议?如果只有两个或三个列表框,我会编写不同的查询,但这是六个多选列表框,我不知道。

提前致谢。

I have a form in which there are around 6 multiselect listBoxes. the listBoxes are populated from different tables. I have no problem in storing the selections from listBoxes in variables and then using the variables in a SQL query. the variable stores the selected item from listBox as follows:

If clientList.SelCount > 0 Then

    For i = 0 To clientList.ListCount - 1

      If clientList.Selected(i) Then
           If Len(criteria_cl) = 0 Then
               criteria_cl = Chr(39) & clientList.List(i) & Chr(39)
      Else
         criteria_cl = criteria_cl & "," & Chr(39) & clientList.List(i) & Chr(39)
      End If

 End If
Next

 Else 
 End If

the SQL query is as follows with more variables which are called criteria_xx

 strsql = "select * from pmt_hist_dmart_step2 where dbr_portfolio in (" & criteria_pf & ") and DBR_CLIENT in (" & criteria_cl & _
 ") and DBR_ACCT_TYPE in (" & criteria_ac & ")..... ;"

How do I deal with a situation when the user makes no selection. Currently, the criteria_xx variable becomes blank and the IN clause contains ('') and throws an error.

Please provide some advice on how to overcome this? If it were only two or three listBoxes, I would written different queries but this is six multiselect listBoxes and I have no clue.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠佳期 2025-01-05 19:37:31

这是一种方法:

strsql = " select *                          " &                        _
         "   from pmt_hist_dmart_step2       " &                        _
         "  where " & iif(len(criteria_pf) = 0,                         _
                          "",                                           _
                          "dbr_portfolio in (" & criteria_pf & ") and " _
                         ) &                                            _
                      iif(len(criteria_cl) = 0,                         _
                          "",                                           _
                          "DBR_CLIENT in (" & criteria_cl & ") and "    _
                         ) &                                            _
                      iif(len(criteria_ac) = 0,                         _
                          "",                                           _
                          "DBR_ACCT_TYPE in (" & criteria_ac & ") and " _
                         ) &                                            _
                      ... &                                             _
                  " 1 = 1;"

这里的想法是,仅当 ... 非空白时才应包含 dbr_portfolio in (...) 形式的每个子句。 iif 是一个带有三个参数的内置函数;如果第一个参数为 true,则返回第二个参数;如果第一个参数为 false,则返回第三个参数。例如,iif(1 = 1, 5, 10) 返回 5iif(1 = 2, 5, 10) 返回 <代码>10。 (注意,第二个和第三个参数始终都会被求值,即使其中一个随后被忽略。)

由于像 where andand and 这样的东西不是有效的 SQL,我将每个 and 包含在上一个子句的 iif 控制代码中。然后,由于最终的and 不是有效的 SQL,因此我添加了最终的1 = 1 子句,该子句没有实际效果。

(顺便说一句,抱歉,如果我的缩进方案很难看;自从我上次编写 VB6 以来已经有十几年了,所以我真的不记得 VB6 代码通常是如何格式化的。)

Here's one approach:

strsql = " select *                          " &                        _
         "   from pmt_hist_dmart_step2       " &                        _
         "  where " & iif(len(criteria_pf) = 0,                         _
                          "",                                           _
                          "dbr_portfolio in (" & criteria_pf & ") and " _
                         ) &                                            _
                      iif(len(criteria_cl) = 0,                         _
                          "",                                           _
                          "DBR_CLIENT in (" & criteria_cl & ") and "    _
                         ) &                                            _
                      iif(len(criteria_ac) = 0,                         _
                          "",                                           _
                          "DBR_ACCT_TYPE in (" & criteria_ac & ") and " _
                         ) &                                            _
                      ... &                                             _
                  " 1 = 1;"

The idea here is that each clause of the form dbr_portfolio in (...) should only be included if the ... is non-blank. iif is a built-in function that takes three arguments; if its first argument is true, then it returns its second argument, and if its first argument is false, then it returns its third argument. For example, iif(1 = 1, 5, 10) returns 5, and iif(1 = 2, 5, 10) returns 10. (N.B. The second and third arguments are both always evaluated, even though one of them is ignored afterward.)

Since something like where and or and and would not be valid SQL, I include each and within the iif-controlled code of the previous clause. Then, since a final and would not be valid SQL, I tacked on a final 1 = 1 clause that has no actual effect.

(By the way, sorry if my indentation scheme is ugly; it's been over a dozen years since I last wrote VB6, so I really don't remember how VB6 code is usually formatted.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文