简体   繁体   English

Mysql 存储过程返回 LAST_INSERT_ID 为零

[英]Mysql stored procedure returns LAST_INSERT_ID as zero

I have a problem with return output parameter from mysql stored procedure to VBA macro.我在将 output 参数从 mysql 存储过程返回到 VBA 宏时遇到问题。 I wrote this mysql stored procedure InsertProduct to insert new product into database:我写了这个 mysql 存储过程 InsertProduct 将新产品插入数据库:


DELIMITER $$
CREATE PROCEDURE `InsertProduct`(IN `p_modelno` VARCHAR(40), IN `p_name` VARCHAR(120), OUT `p_last_id` BIGINT)
BEGIN
    INSERT INTO product (product_id, name, model_no) VALUES (null, p_name, p_modelno);
    SET p_last_id =  LAST_INSERT_ID(); 
END$$
DELIMITER ;

When I test this procedure in phpmyadmin, it returns Last_Insert_ID correctly (for example - new record got the ID 4454, it display p_last_id=4454), see this image: phpmyadmin testing procedure returns correct ID当我在 phpmyadmin 中测试此程序时,它正确返回 Last_Insert_ID(例如 - 新记录的 ID 为 4454,它显示 p_last_id=4454),请参见此图像: phpmyadmin 测试程序返回正确的 ID

Then I have an userform in excel with two text boxes (txtModelNo, txtName) as a front-end to save new product into database.然后我在 excel 中有一个用户表单,其中两个文本框(txtModelNo,txtName)作为前端将新产品保存到数据库中。 I've written VBA macro to save the product into database and then I want to dipslay msgbox with this last insert record ID.我已经编写了 VBA 宏来将产品保存到数据库中,然后我想使用最后一个插入记录 ID 来显示 msgbox。 Here is the problem, p_last_id in macro always returns 0...这是问题所在,宏中的 p_last_id 总是返回 0 ...

Code in VBA for running this stored procedure and use this output parameter: VBA 中的代码用于运行此存储过程并使用此 output 参数:



Sub InsertProduct()

  
    Dim cnSqlConn As New ADODB.Connection
    cnSqlConn.Connectionstring = "DSN=XXX"
    cnSqlConn.Open
    
    Dim cmd As New ADODB.Command
    
    With cmd
        .ActiveConnection = cnSqlConn
        .CommandType = adCmdStoredProc
        .CommandText = "InsertProduct"
        .CommandTimeout = 15
    End With
    

    cmd.Parameters.Append cmd.CreateParameter("p_modelno", adVarChar, adParamInput, 40, Me.txtModelNo) 
    cmd.Parameters.Append cmd.CreateParameter("p_name", adVarChar, adParamInput, 120, Me.txtName)
    cmd.Parameters.Append cmd.CreateParameter("p_last_id", adInteger, adParamOutput, 11)
    cmd.Execute lngResult

     Msgbox cmd.Parameters("p_last_id") /****** HERE COMES THE PROBLEM - IT ALWAYS RETURNS 0 !! *******/

    Set cmd = Nothing
    cnSqlConn.Close
    Set cnSqlConn = Nothing

End Sub()

I've read many articles about this topic, but nothing helps.我已经阅读了很多关于这个主题的文章,但没有任何帮助。 Thanks in advance for any advice.提前感谢您的任何建议。

OK, I rewrite the procedure without output parameter:好的,我重写了没有output参数的程序:


DELIMITER $$
CREATE PROCEDURE `InsertProduct`(IN `p_modelno` VARCHAR(40), IN `p_name` VARCHAR(120))
BEGIN
    INSERT INTO product (product_id, name, model_no) VALUES (null, p_name, p_modelno);
    SELECT LAST_INSERT_ID() as last_id FROM product; 
END$$
DELIMITER ;

VBA procedure code: VBA程序代码:


Sub InsertProduct()

  
    Dim cnSqlConn As New ADODB.Connection
    cnSqlConn.Connectionstring = "DSN=XXX"
    cnSqlConn.Open
    
    Dim cmd As New ADODB.Command
    
    With cmd
        .ActiveConnection = cnSqlConn
        .CommandType = adCmdStoredProc
        .CommandText = "InsertProduct"
        .CommandTimeout = 15
    End With
    

    cmd.Parameters.Append cmd.CreateParameter("p_modelno", adVarChar, adParamInput, 40, 
    Me.txtModelNo) 
    cmd.Parameters.Append cmd.CreateParameter("p_name", adVarChar, adParamInput, 120, 
    Me.txtName)
    
     dim rstProduct as new ADODB.Recordset set rstProduct = cmd.Execute Msgbox rstProduct.Fields("last_id").Value /* displays the correct value */ 
    rstProduct.Close
    set rstProduct = nothing
    Set cmd = Nothing
    cnSqlConn.Close
    Set cnSqlConn = Nothing

End Sub()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM