mysql 存储过程 out 调用_使用带有OUT参数的VB6调用MySQL存储过程

bd96500e110b49cbb3cd949968f18be7.png

I have written this procedure in MySQL (Server 5.5)

DELIMITER $$

DROP PROCEDURE IF EXISTS `InsertList` $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `InsertList`(IN fName VARCHAR(20), IN fType VARCHAR(3), IN fFood varchar(20), Out fResult int)

BEGIN

insert into tblguest (firstname, confirm, food) values (fName, fType, fFood);

select count(id) from tblguest into fResult;

END $$

DELIMITER ;

When I call this procedure from MySQL Query Browser it returns as expected

Call InsertList ('V1', 'No', 'F1', @result);

Select @result;

--> It successfully returns the count of the ids in the table

I wrote the following code in VB6

Dim res As Integer

On Error GoTo chkErr

Set cmd = New ADODB.Command

cmd.ActiveConnection = cn

cmd.CommandType = adCmdStoredProc

cmd.CommandText = "InsertList"

cmd.Parameters.Append cmd.CreateParameter("fName", adVarChar, adParamInput, 20, Text3.Text)

cmd.Parameters.Append cmd.CreateParameter("fType", adVarChar, adParamInput, 3, Text2.Text)

cmd.Parameters.Append cmd.CreateParameter("fFood", adVarChar, adParamInput, 20, Text1.Text)

cmd.Parameters.Append cmd.CreateParameter("fResult", adInteger,adParamOutput)

cmd.Execute

res = cmd("fResult")

MsgBox res

Exit Sub

chkErr:

Select Case Err.Number

Case Else

Text4.Text = Err.Number & " - " & Err.Description

End Select

But the moment it tries to run the statement cmd.execute it throws the following error :

-2147467259 - [MySQL][ODBC 5.1 Driver][mysqld-5.5.34]OUT or INOUT argument 4 for routine dbtest.InsertList is not a variable or NEW

pseudo-variable in BEFORE trigger

I have seen codes working for SQL, so is it that MySQL itself has an issue using Stored Procedure with an OUT Parameter?

解决方案

It seems a unsolved bug from MySQL ODBC and C/API

One solution is to execute that using a SQL command with prepared variables:

Dim rs As ADODB.Recordset

Set cmd = New ADODB.Command

cmd.ActiveConnection = cn

cmd.CommandType = adCmdText

cmd.CommandText = "call InsertList(?,?,?,@fResult)"

cmd.Parameters.Append cmd.CreateParameter("fName", adVarChar, adParamInput, 20, Text3.Text)

cmd.Parameters.Append cmd.CreateParameter("fType", adVarChar, adParamInput, 3, Text2.Text)

cmd.Parameters.Append cmd.CreateParameter("fFood", adVarChar, adParamInput, 20, Text1.Text)

cmd.Execute

'And after that, using the same connection, get the value of

'@fResult from a single query:

Set rs = cn.Execute("select @fResult as fResult")

MsgBox rs!fResult

You will get the expected value.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值