gpt4 book ai didi

mysql - 获取错误到 : MySQL stored procedure with parameters in Access frontend via VBA

转载 作者:行者123 更新时间:2023-11-30 21:54:26 24 4
gpt4 key购买 nike

Mysql 数据库有参数化存储过程(插入语句),它从两个日期范围以 1 年为间隔生成多个日期列表,如下所示:

autoid  id  tenant_id   ag_id   interval_start  interval_end    rate
10 1 28 1 24/07/2016 23/07/2017 95
11 2 28 1 24/07/2017 23/07/2018 105
12 3 28 1 24/07/2018 23/07/2019 115

我尝试使用下面的 vba 代码从我的 Access 前端调用过程:

单行插入语句

Dim cmd As ADODB.Command
Dim rst As ADODB.Recordset
Dim tid, int_r, inc_r As Long
Dim sdate, edate As Date
Dim strConn, strServer As String

strServer = "192.168.20.2"
strConn = "ODBC;MySQL ODBC 5.2 Unicode Driver;UID=admin;PORT=3306;DATABASE=tenant_db;PASSWORD=1DBServer;SERVER=" & strServer & ";FILEDSN=C:\Users\abzalali\Dropbox\tenant_db\tenant_db.dsn;"

tid = Me.tenant_id
int_r = Me.initial_rate
inc_r = Me.increase_rate
sdate = Format(Me.startdate, "yyyy-mm-dd")
edate = Format(Me.enddate, "yyyy-mm-dd")

Set cmd = New ADODB.Command

With cmd
.ActiveConnection = strConn
.CommandText = "CALL make_intervals_v2(" & tid & ", " & int_r & ", " & inc_r & ", '" & sdate & "', '" & edate & "')"
.CommandType = adCmdText
.Execute
End With

MsgBox ("Done")
Me.Refresh

Set cmd = Nothing

程序可以很好地运行这段代码,但只插入第一行。之后,我添加了总记录集的循环,如下所示:

多行插入语句

Dim cmd As ADODB.Command
Dim rst As ADODB.Recordset
Dim tid, int_r, inc_r As Long
Dim sdate, edate As Date
Dim strConn, strServer As String

strServer = "192.168.20.2"
strConn = "ODBC;MySQL ODBC 5.2 Unicode Driver;UID=admin;PORT=3306;DATABASE=tenant_db;PASSWORD=1DBServer;SERVER=" & strServer & ";FILEDSN=C:\Users\abzalali\Dropbox\tenant_db\tenant_db.dsn;"

tid = Me.tenant_id
int_r = Me.initial_rate
inc_r = Me.increase_rate
sdate = Format(Me.startdate, "yyyy-mm-dd")
edate = Format(Me.enddate, "yyyy-mm-dd")

Set cmd = New ADODB.Command

With cmd
.ActiveConnection = strConn
.CommandText = "CALL make_intervals_v2(" & tid & ", " & int_r & ", " & inc_r & ", '" & sdate & "', '" & edate & "')"
.CommandType = adCmdText
Set rst=.Execute
End With

Do Until rst.EOF
rst.MoveNext
Loop

MsgBox ("Done")
Me.Refresh

Set rst = Nothing
Set cmd = Nothing

最后我得到了错误:

enter image description here

不知道我实际上错过了什么。

这是我实际的存储过程:

DELIMITER @@
DROP PROCEDURE make_intervals_v2 @@
CREATE PROCEDURE tenant_db.make_intervals_v2
(IN `t_id` INT, IN `InitRate` INT, IN `IncrRate` INT, IN `dateStart` DATE, IN `dateEnd` DATE)
BEGIN
DECLARE unitval VARCHAR(10);
DECLARE intval INT;
DECLARE done INT DEFAULT FALSE;
DECLARE thisDate,nextDate DATE;
DECLARE rate DECIMAL;
DECLARE ag_id INT;



DECLARE l_ID integer;
DECLARE l_startdate,l_enddate DATE;
DECLARE l_ag_id integer;
DECLARE theCursor CURSOR FOR SELECT tenant_id, dateStart, dateEnd, 1 FROM tbl_tenant_basic_info WHERE active=1 AND tenant_id=t_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

SET unitval='YEAR';
SET intval=1;

drop temporary table if exists year_intervals2;
create temporary table year_intervals2
( id int AUTO_INCREMENT primary key,
tenant_id INT NOT NULL,
ag_id INT NOT NULL,
interval_start DATE NOT NULL,
interval_end DATE NOT NULL,
rate DECIMAL
);

OPEN theCursor;
read_loop: LOOP
FETCH theCursor INTO l_ID,l_startdate,l_enddate, l_ag_id;
IF done THEN
LEAVE read_loop;
END IF;
set thisDate = l_startdate;
set ag_id= l_ag_id;
set rate = InitRate;

repeat
select
case unitval
when 'MICROSECOND' then timestampadd(MICROSECOND, intval, thisDate)
when 'SECOND' then timestampadd(SECOND, intval, thisDate)
when 'MINUTE' then timestampadd(MINUTE, intval, thisDate)
when 'HOUR' then timestampadd(HOUR, intval, thisDate)
when 'DAY' then timestampadd(DAY, intval, thisDate)
when 'WEEK' then timestampadd(WEEK, intval, thisDate)
when 'MONTH' then timestampadd(MONTH, intval, thisDate)
when 'QUARTER' then timestampadd(QUARTER, intval, thisDate)
when 'YEAR' then timestampadd(YEAR, intval, thisDate)
end into nextDate;

insert into year_intervals2 (tenant_id,interval_start,interval_end, rate, ag_id) select l_ID,thisDate, date_add(nextDate,INTERVAL -1 DAY), rate, ag_id;
set thisDate = nextDate;
set rate = rate+IncrRate;
until thisDate >= l_enddate
end repeat;
END LOOP;
insert into tbl_agreement_years (id, tenant_id, ag_id, interval_start,interval_end, rate)
select * from year_intervals2;
END @@
DELIMITER ;

最佳答案

您必须添加到数据库的连接

Dim conn As ADODB.Connection

...

Set conn = New ADODB.Connection
conn.Open strConn

...

With cmd
.ActiveConnection = conn

...

Set rst=.Execute
End With

编辑 1:向记录集循环添加额外检查

If rst.State= adStateOpen then
Do Until rst.EOF
rst.MoveNext
Loop
End If

编辑2:关于“单行插入语句”问题:

这行存储过程捕获所有 select|fetch ... into ... 语句:

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

read_loop block 中有两个,解决方案:

更改行:

  select
case unitval
when 'MICROSECOND' then timestampadd(MICROSECOND, intval, thisDate)
when 'SECOND' then timestampadd(SECOND, intval, thisDate)
when 'MINUTE' then timestampadd(MINUTE, intval, thisDate)
when 'HOUR' then timestampadd(HOUR, intval, thisDate)
when 'DAY' then timestampadd(DAY, intval, thisDate)
when 'WEEK' then timestampadd(WEEK, intval, thisDate)
when 'MONTH' then timestampadd(MONTH, intval, thisDate)
when 'QUARTER' then timestampadd(QUARTER, intval, thisDate)
when 'YEAR' then timestampadd(YEAR, intval, thisDate)
end into nextDate;

到:

  set nextDate = (select
case unitval
when 'MICROSECOND' then timestampadd(MICROSECOND, intval, thisDate)
when 'SECOND' then timestampadd(SECOND, intval, thisDate)
when 'MINUTE' then timestampadd(MINUTE, intval, thisDate)
when 'HOUR' then timestampadd(HOUR, intval, thisDate)
when 'DAY' then timestampadd(DAY, intval, thisDate)
when 'WEEK' then timestampadd(WEEK, intval, thisDate)
when 'MONTH' then timestampadd(MONTH, intval, thisDate)
when 'QUARTER' then timestampadd(QUARTER, intval, thisDate)
when 'YEAR' then timestampadd(YEAR, intval, thisDate)
end );

关于mysql - 获取错误到 : MySQL stored procedure with parameters in Access frontend via VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45859783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com