记录集过滤器的问题

发布于 2024-08-06 01:02:02 字数 1225 浏览 7 评论 0原文

我在旧版 ASP Classic 代码中的 ADO 记录集上使用筛选器时遇到问题,并且我试图了解我想要执行的操作是否不受支持,或者我是否只是做错了。

我有一个项目记录集,它们的状态为 1(活动)或 0(非活动),以及一个可选的 End_Date。在我的管理用户界面中,我有一个控件来显示所有项目或仅显示应向最终用户显示的项目: Status = 1 AND ( End_Date is null OR End_Date > Date() )

To实现该逻辑,我尝试过:

rs.Filter = "Status = 1 AND ( End_Date = null OR End_Date > #" & Date() & "# )"

ADODB.Recordset (0x800A0BB9)
Unknown runtime error

经过多次闲逛,ADO 似乎不喜欢 End_Date 条件周围的分组括号与 AND 条件相结合。如果我把括号去掉,这是可行的:

rs.Filter = "Status = 1 AND End_Date = null OR End_Date > #" & Date() & "#"

但这只是一个意外——看起来过滤条件是按顺序评估的,所以我得到了我想要的结果。如果我将 AND 更改为 OR,则括号起作用:

rs.Filter = "Status = 1 OR ( End_Date = null OR End_Date > #" & Date() & "# )"

但当然,这种逻辑是错误的 - 它显示活动但过期的项目。

奇怪的是,如果我改变条件,它会再次中断:

rs.Filter = "End_Date = null OR Status = 1 AND End_Date > #" & Date() & "# "

因相同的 ADODB 错误而崩溃。

我似乎无法预测什么会起作用,什么不会起作用,而且我读过的文档对于预期的语法(它不是纯粹的 T-SQL!)、限制等以及我的所有示例都非常粗略。我见过最多有两个条件。我不认为我的条件有那么复杂。谁能告诉我我想做的事情是否受到支持,是否有更好的方法来做到这一点,或者向我指出与这种逻辑相匹配的综合文档和示例?

谢谢!

I'm having trouble with a filter on an ADO Recordset in legacy ASP Classic code, and I'm trying to understand if what I'm trying to do is not supported, or if I'm just doing it wrong.

I have a recordset of Items, and they have a Status of 1 (active) or 0 (inactive), and an optional End_Date. In my administrative user interface, I have a control to show all items or only those that should be displayed to end-users: Status = 1 AND ( End_Date is null OR End_Date > Date() )

To implement that logic, I tried:

rs.Filter = "Status = 1 AND ( End_Date = null OR End_Date > #" & Date() & "# )"

but I get

ADODB.Recordset (0x800A0BB9)
Unknown runtime error

After much fooling around, it seems that ADO doesn't like the grouping parens around the End_Date conditions in combination with the AND condition. If I take the parens out, this works:

rs.Filter = "Status = 1 AND End_Date = null OR End_Date > #" & Date() & "#"

But that's just an accident -- it looks like the filter conditions are evaluated in order, and so I get the results I want. If I change the AND to OR, the parens work:

rs.Filter = "Status = 1 OR ( End_Date = null OR End_Date > #" & Date() & "# )"

But of course that logic is wrong -- it shows Active but expired items.

Strangely, if I move the conditions around, it breaks again:

rs.Filter = "End_Date = null OR Status = 1 AND End_Date > #" & Date() & "# "

crashes with the same ADODB error.

I can't seem to predict what will and won't work, and the docs I've read are very sketchy on the syntax expected (it's not pure T-SQL!), the limitations, etc. and all the examples I've seen have at most two conditions. I don't think my conditions are all that complex. Can anyone tell me if what I'm trying to do is supported, if there's a better way to do it, or point me to comprehensive docs and samples that match this kind of logic?

Thanks!

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

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

发布评论

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

评论(3

鹿港巷口少年归 2024-08-13 01:02:02

ADO 记录集对象过滤器属性

AND 和 OR 之间没有优先级。子句可以分组在括号内。但是,您不能将通过 OR 连接的子句分组,然后使用 AND 将组连接到另一个子句,如下所示:

(姓氏 = '史密斯' 或姓氏 =
'琼斯') AND 名字 = '约翰'

相反,你可以构建这个
过滤为:

(姓氏 = '史密斯' AND 名字 =
'约翰') 或

(姓氏 = '琼斯' AND 名字 =
“约翰”)

所以你必须像这样构建你的过滤器:

rs.Filter = "( Status = 1 AND End_Date = null ) OR ( Status = 1 AND End_Date > #" & Date() & "# )"

ADO Recordset Object Filter Property:

There is no precedence between AND and OR. Clauses can be grouped within parentheses. However, you cannot group clauses joined by an OR and then join the group to another clause with an AND, like this:

(LastName = 'Smith' OR LastName =
'Jones') AND FirstName = 'John'

Instead, you would construct this
filter as:

(LastName = 'Smith' AND FirstName =
'John') OR

(LastName = 'Jones' AND FirstName =
'John')

So you would have to construct your filter like this:

rs.Filter = "( Status = 1 AND End_Date = null ) OR ( Status = 1 AND End_Date > #" & Date() & "# )"
奈何桥上唱咆哮 2024-08-13 01:02:02

我知道您正在使用遗留代码,并且可能其他事情正在工作,但是如何在这个特定页面中打开记录集?您是否使用 adovbs.inc 中定义的某些常量?

例如:

rs.Open "SELECT * FROM table1", db, adOpenStatic, adLockPessimistic

I know that you are working with legacy code and probably other things are working, but how do you open the recordset in this specific page? Are you using some constant defined in adovbs.inc?

For example:

rs.Open "SELECT * FROM table1", db, adOpenStatic, adLockPessimistic
农村范ル 2024-08-13 01:02:02

当你尝试这个时会发生什么......

Dim strToday
strToday = Now()
rs.Filter = "Status=1 AND (End_Date = null OR End_Date > """ & strToday & """)" 

What happens when you try this...

Dim strToday
strToday = Now()
rs.Filter = "Status=1 AND (End_Date = null OR End_Date > """ & strToday & """)" 

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