1

I perform a query on an Access 2010 DB from a VB6 application. The SQL looks like that:

SELECT *, NetPrice AS Price 
FROM Products 
WHERE Len(ProductName)<>0 AND ProductID IN (1,5,8,13);

The following Code performs the query:

Set ResultRecordset = myDataBase.OpenRecordset(SqlCode, dbOpenSnapshot)

So far, everything is fine, but when the Number of the IDs in the List increses to about 50, then the ResultRecordset is Nothing. I have found some limitations for Access here, but I am far below the values provided there (1 Table, 200 characters query length, no nested subqueries,...). Any ideas what's going wrong?

4

3 回答 3

1

我相信 Filburt 实际上更符合您的 IN(列表中大约 50 个)部分的查询。我知道在 FoxPro 中(并回到这个),它也支持 IN() 子句。但是,IN 功能仅设计为接受列表中最多 20 个参数。如果您想做的不止这些,则需要将其拆分为多个 IN() 语句,每个语句之间使用 OR ... 例如

where
   ProductID IN ( 1, 2, 20, 32, ...)
   OR ProductID IN (46, 50, 52, ...)

通过拆分它们可能是你正在运行的。

于 2013-05-16T10:41:56.257 回答
0

我找到了一个解决方案:

SqlCode="SELECT *, NetPrice AS Price FROM Products WHERE Len(ProductName)<>0"
Set TempRecordset = myDataBase.OpenRecordset(SqlCode, dbOpenSnapshot)
TempRecordset.Filter="ProductID IN (1,2,..,50)"
Set ResultRecordset=TempRecordset.OpenRecordset
于 2013-05-17T08:09:24.560 回答
0

您可以在查询中尝试 UNION ALL 运算符,以便发送到 Access 的查询如下所示:

SELECT *, NetPrice AS Price 
FROM Products 
WHERE Len(ProductName)<>0 AND ProductID IN (1,2,3,4,5,6,7,8,9)

Union All

SELECT *, NetPrice AS Price 
FROM Products 
WHERE Len(ProductName)<>0 AND ProductID IN (10,11,12,13,14,15,16)

假设您的 ProductID 列已编入索引,这也应该使您的查询运行得更快。

于 2013-05-16T16:44:39.320 回答