【发布时间】:2020-06-18 05:34:44
【问题描述】:
参考this question,我运行以下查询
a) 获取 Column A 和
中的广告系列
b) 将SQL 中的别名作为header 包含在Column A 中。
Sub ConnectDB5()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim dateVar As Date
Set conn = New ADODB.Connection
conn.ConnectionString = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=localhost; DATABASE=bi; UID=username; PWD=password; OPTION=3"
conn.Open
strSQL = " SELECT 'campaign' UNION ALL SELECT " & _
" cID AS Campaign " & _
" FROM PDW_DIM_Offers_Logistics_history " & _
" WHERE DATE(insert_timestamp) = ""2020-02-24"" "
Set rs = New ADODB.Recordset
rs.Open strSQL, conn, adOpenStatic
Sheet4.Range("A1").CopyFromRecordset rs
rs.Close
conn.Close
End Sub
此查询完美运行。
现在,我在 SQL 查询中添加了一列:
Sub ConnectDB5()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim dateVar As Date
Set conn = New ADODB.Connection
conn.ConnectionString = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=localhost; DATABASE=bi; UID=username; PWD=password; OPTION=3"
conn.Open
strSQL = " SELECT 'campaign' UNION ALL SELECT " & _
" cID AS Campaign, " & _
" SUM(order_quantity) AS Quantity" & _
" FROM PDW_DIM_Offers_Logistics_history " & _
" WHERE DATE(insert_timestamp) = ""2020-02-24"" " & _
" GROUP BY 1"
Set rs = New ADODB.Recordset
rs.Open strSQL, conn, adOpenStatic
Sheet4.Range("A1").CopyFromRecordset rs
rs.Close
conn.Close
End Sub
这样我得到:
运行时错误'-2147217887 (80040e21)'
我需要在我的 VBA 中更改什么
a) 获取 Column A 中的活动和 Column B 中的数量
b) 将SQL 中的别名作为header 包含在Column A 和Column B 中?
【问题讨论】: