从Excel VBA我连接到MySQL以获取my_stored_procedure。工作正常,但当我使用日期过滤器作为参数传递给MySQL SP时,我收到运行时错误。
用户在两个单元格中输入日期过滤器作为开始日期和结束日期。单元格定义为范围dateFrom和dateTo。
所以我使用这些范围传递给mySql rs,但是VBA会抛出:
运行时错误' -2147217900(80040e14)'
您在MySQL语法中有错误;检查对应的手册 到您的MYSQL服务器版本...' my_stored_procedure' 2015-10-01', '二○一五年十月三十〇日''在第1行。
以下是我的VBA代码:
Const ConnStr As String = "Driver={MySQL ODBC 5.3 ANSI Driver};Server=0.0.0.0;Database=db;User=user;Password=pw;Option=3;PORT=3306;Connect Timeout=20;"
Function MySqlCommand(strSql As String) As Variant
Dim conMySQL
Set conMySQL = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
conMySQL.ConnectionString = ConnStr
conMySQL.Open
Set cmd.ActiveConnection = conMySQL
cmd.CommandText = strSql
ReturnValue = cmd.Execute
conMySQL.Close
End Function
Function MySqlRS(strSql As String) As ADODB.Recordset
Dim conMySQL
Set conMySQL = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
conMySQL.ConnectionString = ConnStr
conMySQL.Open
Set rs.ActiveConnection = conMySQL
rs.Open strSql
Set MySqlRS = rs
End Function
_____
Public Sub fetchReport()
Dim rs As ADODB.Recordset
Set rs = MySqlRS("my_stored_procedure '" & Format(Range("dateFrom"), "yyyy-mm-dd") & "' ,'" & Format(Range("dateTo"), "yyyy-mm-dd") & "' ")
Dim hd As Integer
If Not rs.EOF Then
For hd = 0 To rs.Fields.Count - 1
Worksheets("data").Cells(1, hd + 1).Value = rs.Fields(hd).Name 'insert column headers
Next
Sheets("data").Range("A2").CopyFromRecordset rs 'insert data
Sheets("data").Range("A1").CurrentRegion.Name = "rsRange" 'set named range for pivot
End If
ActiveWorkbook.RefreshAll
End Sub
这是我的SP语法:
CREATE DEFINER=`user`@`%` PROCEDURE `my_stored_procedure`
(
in fromDate date,
in toDate date
)
BEGIN
SELECT ...
WHERE dateColumn >= fromDate AND dateColumn <= toDate
END
感谢任何帮助。
乔尔
答案 0 :(得分:1)
我希望这会有所帮助。我做完这件事已经很久了。
create table myTable
( id int auto_increment primary key,
dateColumn date not null,
stuff varchar(100) not null
);
insert myTable(dateColumn,stuff) values
('2014-12-21','dogs'),('2015-02-21','frogs'),('2015-07-21','snakes'),('2015-12-21','cats');
drop procedure if exists `my_stored_procedure`;
DELIMITER $$
CREATE PROCEDURE `my_stored_procedure`
(
in fromDate date,
in toDate date
)
BEGIN
SELECT *
from myTable
WHERE dateColumn >= fromDate AND dateColumn <= toDate;
END
$$
DELIMITER ;
Function MySqlStoredProcRS(strSql As String) As ADODB.Recordset
Dim conMySQL
Set conMySQL = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
conMySQL.ConnectionString = ConnStr ' not shown but like yours in Op Question
conMySQL.Open
Set rs.ActiveConnection = conMySQL
rs.CursorLocation = adUseClient
rs.Open strSql, conMySQL, , , adCmdText
Set MySqlStoredProcRS = rs
End Function
Public Sub fetchReport()
Dim rs As ADODB.Recordset
Dim sql As String
sql = "call my_stored_procedure ('" & Format(Range("dateFrom"), "yyyy-mm-dd") & "' ,'" & Format(Range("dateTo"), "yyyy-mm-dd") & "')"
Set rs = MySqlStoredProcRS(sql)
Dim hd As Integer
If Not rs.EOF Then
For hd = 0 To rs.Fields.Count - 1
Worksheets("data").Cells(1, hd + 1).Value = rs.Fields(hd).Name 'insert column headers
Next
Sheets("data").Range("A2").CopyFromRecordset rs 'insert data
Sheets("data").Range("A1").CurrentRegion.Name = "rsRange" 'set named range for pivot
End If
ActiveWorkbook.RefreshAll
End Sub
我想使用MySqlStoredProcRS
探索对新函数call
的更改,对ADODB.RecordSet的处理,以及在调用字符串中包装参数的括号。
答案 1 :(得分:0)
嗨试试看看它是怎么回事
Option Explicit
Const strCONNECTION As String = "Driver={MySQL ODBC 5.3 ANSI Driver};Server=0.0.0.0;Database=db;User=user;Password=pw;Option=3;PORT=3306;Connect Timeout=20;"
Function GetConnection(ByVal strConnString As String) As ADODB.Connection
Dim ret As ADODB.Connection
Set ret = New ADODB.Connection
ret.Open strConnString
' Return the connection object
Set GetConnection = ret
End Function
Function GetCommand(ByRef oConn As ADODB.Connection, ByVal strCommand As String) As ADODB.Command
Dim ret As ADODB.Command
Set ret = New ADODB.Command
With ret
Set .ActiveConnection = oConn
.CommandText = strCommand
.CommandType = adCmdText
End With
' Return the command
Set GetCommand = ret
End Function
Function GetRecordSet(ByRef oCommand As ADODB.Command) As ADODB.Recordset
Set GetRecordSet = oCommand.Execute
End Function
Public Sub fetchReport()
Dim oCon As ADODB.Connection
Dim oCom As ADODB.Command
Dim oRecordSet As ADODB.Recordset
Dim strCommand As String
Dim hd As Integer
' Create and load the objects
strCommand = "exec my_stored_procedure '" & Format(Range("dateFrom"), "yyyy-mm-dd") & "' ,'" & Format(Range("dateTo"), "yyyy-mm-dd") & "' "
' Connection string > Connection > Command > RecordSet
Set oCon = GetConnection(strCONNECTION)
Set oCom = GetCommand(oCon, strCommand)
Set oRecordSet = GetRecordSet(oCom)
If Not oRecordSet.EOF Then
For hd = 0 To oRecordSet.Fields.Count - 1
Worksheets("data").Cells(1, hd + 1).Value = oRecordSet.Fields(hd).Name 'insert column headers
Next
Sheets("data").Range("A2").CopyFromRecordset oRecordSet 'insert data
Sheets("data").Range("A1").CurrentRegion.Name = "rsRange" 'set named range for pivot
End If
ActiveWorkbook.RefreshAll
End Sub
我希望这会有所帮助:)