使用带有OUT参数的

时间:2017-04-21 06:46:17

标签: mysql stored-procedures vb6

我在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 ;

当我从MySQL Query Browser调用此过程时,它会按预期返回

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

Select @result; 

- >它成功返回表中的ID计数

我在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

但是当它试图运行语句cmd.execute时,它会抛出以下错误:

  

-2147467259 - [MySQL] [ODBC 5.1驱动程序] [mysqld-5.5.34]例程dbtest.InsertList的OUT或INOUT参数4不是变量或NEW   BEFORE触发器中的伪变量

我见过用于SQL的代码,那么MySQL本身是否存在使用带有OUT参数的存储过程的问题?

1 个答案:

答案 0 :(得分:1)

这似乎是MySQL ODBC和C / API

中未解决的错误

一种解决方案是使用带有准备变量的SQL命令执行该操作:

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

您将获得预期的价值。