20

长期观众第一次海报。我正在开发一个带有 Excel 前端的数据库应用程序。因此,我使用 VBA 来访问 MySQL 数据库。我使用的驱动程序是 ODBC 驱动程序 (Windows),并且我与数据库有有效的连接,因为许多其他查询运行良好。

我要做的是从一系列 SQL 语句中返回结果,而不是试图将它们全部组合成一个庞大的语句(这可能会起作用,但很难维护/理解)。我已将标志添加FLAG_MULTI_STATEMENTS = 67108864到抑制驱动程序语法错误的连接字符串。

但是现在当我运行以下命令时:

queryDB.Open SQLquery, conn

遗体关闭,recordset (queryDB)没有明显错误。sql 语句可以在这里找到。

我可能会生成未返回到 VBA 的错误,因此非常感谢这里的任何帮助。

注意: sql 语句可以正常工作,因为我可以将该语句粘贴到phpMyAdmin其中并返回正确的(非空)结果。我不知道这些语句是否是具体的问题,但CREATE TEMPORARY TABLE ...一般来说可能是使用或使用多个语句。

另外我猜驱动程序可能正在尝试为每个 sql 语句返回一个结果,而 VBA 只得到第一个或其他东西......

编辑:供将来参考的 sql 语句。

CREATE TEMPORARY TABLE tmpOrders AS
SELECT
o.customerName,
SUM(o.Sales) AS Sales,
SUM(TotalFobCost + TotalLandedCost + TotalLocalCost + TotalCmtCost) AS TotalCost,
YEAR(o.deliveryDate) AS YEAR,
MONTH(o.deliveryDate) AS MONTH
FROM devere_costing.orders_fixed_extras AS o
WHERE o.orderApproved = TRUE
AND o.orderCanceled = FALSE
AND o.deliveryDate BETWEEN '2014-01-01' AND '2014-03-31'
GROUP BY customerName, YEAR, MONTH
ORDER BY YEAR ASC, MONTH ASC, customerName ASC;

CREATE TEMPORARY TABLE tmpProj AS
SELECT p.customerName,
   IF(p.MONTH > 9, p.YEAR, p.YEAR - 1) AS TrueYear,
   1 + ((p.MONTH + 2) MOD 12) AS TrueMonth,
   SUM(p.actualSalesInvoiced) AS salesInvoiced,
   SUM(p.budget) AS budget
FROM devere_costing.sales_projection_data AS p
GROUP BY p.customerName, p.YEAR, p.MONTH
HAVING TrueYear BETWEEN YEAR('2014-01-01') AND YEAR('2014-03-31')
AND TrueMonth BETWEEN MONTH('2014-01-01') AND MONTH('2014-03-31');

CREATE TEMPORARY TABLE tmpLeft AS
SELECT
IF(o.customerName IS NULL, p.customerName, o.customerName) AS customerName,
p.budget AS TotalBudget,
o.Sales AS Sales,
p.salesInvoiced,
0 AS varianceToBudget,
o.TotalCost,
0 AS directMargin,
0 AS directMarginPercent,
IF(o.YEAR IS NULL, p.TrueYear, o.YEAR) AS YEAR,
IF(o.MONTH IS NULL, p.TrueMonth, o.MONTH) AS MONTH
FROM tmpOrders AS o
LEFT JOIN tmpProj AS p
ON (o.customerName = p.customerName
AND o.YEAR = p.TrueYear
AND o.MONTH = p.TrueMonth);

CREATE TEMPORARY TABLE tmpRight AS
SELECT
IF(o.customerName IS NULL, p.customerName, o.customerName) AS customerName,
p.budget AS TotalBudget,
o.Sales AS Sales,
p.salesInvoiced,
0 AS varianceToBudget,
o.TotalCost,
0 AS directMargin,
0 AS directMarginPercent,
IF(o.YEAR IS NULL, p.TrueYear, o.YEAR) AS YEAR,
IF(o.MONTH IS NULL, p.TrueMonth, o.MONTH) AS MONTH
FROM tmpOrders AS o
RIGHT JOIN tmpProj AS p
ON (o.customerName = p.customerName
AND o.YEAR = p.TrueYear
AND o.MONTH = p.TrueMonth);

(SELECT * FROM tmpLeft) UNION DISTINCT (SELECT * FROM tmpRight);

我已经回答了我自己的问题!

秘密就在这里

所以我是对的,返回的记录集不止一个。我只需要遍历它们即可找到我想要的数据。该集合未编入索引,因此您必须搜索每个集合。在我的情况下,每个 sql 语句都不会返回记录集(这就是为什么我的记录集在我尝试打开它时仍然关闭的原因)。唯一的例外是返回记录的最后一条 sql 语句。我的循环看起来像:

Dim rs As ADODB.Recordset
Set rs = queryDB(Sql)

' Loop through returned recordsets to find the data
Do
    If Not rs Is Nothing Then
        If rs.State = adStateOpen Then
            ' we have an open recordset.  This means that the final select statement
            ' has returned this data.
            Exit Do
        Else
            ' Otherwise iterate through to the next recordset
            Set rs = rs.NextRecordset
        End If
    Else
        MsgBox "No recordset returned by sql statement"
        GoTo ExitCode
    End If
Loop
4

1 回答 1

1

从问题正文中复制的答案:

我已经回答了我自己的问题!

秘密就在这里

所以我是对的,返回的记录集不止一个。我只需要遍历它们即可找到我想要的数据。该集合未编入索引,因此您必须搜索每个集合。在我的情况下,每个 sql 语句都不会返回记录集(这就是为什么我的记录集在我尝试打开它时仍然关闭的原因)。唯一的例外是返回记录的最后一条 sql 语句。我的循环看起来像:

Dim rs As ADODB.Recordset
Set rs = queryDB(Sql)

' Loop through returned recordsets to find the data
Do
    If Not rs Is Nothing Then
        If rs.State = adStateOpen Then
            ' we have an open recordset.  This means that the final select statement
            ' has returned this data.
            Exit Do
        Else
            ' Otherwise iterate through to the next recordset
            Set rs = rs.NextRecordset
        End If
    Else
        MsgBox "No recordset returned by sql statement"
        GoTo ExitCode
    End If
Loop
于 2013-07-11T12:55:49.410 回答