简体   繁体   中英

MySQL LAST_INSERT_ID Returns 0 in Stored Procedure but not Manually Executed

UPDATE 2: If I manually replace the EXECUTE code sample below with the actual SQL code to be executed, it works perfectly fine, which tells me the issue is tired to PREPARE, EXECUTE and DEALLOCATE PREPARE.

UPDATE 1: Actually, it appears every single stored procedure has stopped working (eg 0 always returned on insert). What's very strange is when I MANUALLY execute the insert, I get the index back just fine. But when it's executed via Stored Procedure, it's always 0. The only thing that's changed is I rebooted my system. That's it. Note this is my local 127.0.0.1 MySQL installation. Any reason why a manual execute of INSERT would return a LAST_INSERT_ID() but a stored procedure of the exact same insert would fail? I am not using commit/rollback in the SPs and haven't changed the default system setting for commits

This is quite the head scratcher. I have a large and complex Stored Procedure that builds a SQL INSERT from dynamic JSON field data. It was working perfectly until I merged multiple updates into a single SP to support transaction rollback (but I haven't put any of that code in yet).

The code executing the SQL select statement is:

SET @mysql := var_sql_insert;
PREPARE SQLStatement1 FROM @mysql;
EXECUTE SQLStatement1;
DEALLOCATE PREPARE SQLStatement1;
SET var_company_main_id = LAST_INSERT_ID();

Below is the SQL code that's being executed:

INSERT INTO company_main  (company_owner_account_ref_id,company_name,company_industry_ref_id,company_size_c,company_type_c,company_date_founded) VALUES (1,'TestCompany',2,1,1,'2009/05/06')

The issue is LAST_INSERT_ID() is returning 0 each and every time I execute the SQL statement in the stored procedure, but if I manually execute it, the LAST_INSERT_ID() returns the index just fine. Note the table I'm doing the INSERT to has a properly defined PK Auto-Increment Index, so that's not the issue (which seems to be the #1 cause of the 0 result).

What's even more nuts is this exact same code worked perfectly fine before - it hasn't changed - the only thing I did was put it in a larger stored procedure and add a Label to the BEGIN (because I execute LEAVE on error).

When I walk the code, it executes perfectly; it's just LAST_INSERT_ID() always returns 0 now even when the record is inserted just fine (along with the incremented ID/index).

Any ideas?

So it seems this issue is related to dbForge Studio 2019 for MySQL. I believe it's creating instanced calling in the IDE that can throw off the LAST_INSERT_ID() in cascaded calls. Once I was able to track down and resolve an error that was returning a bad index (yet had no bearing on a completely un-associated INSERT) everything went back to normal. So it seems this issue was caused by the IDE. Took about 3 hours to figure out because the actual debugger built into dbForge was part of the issue. sigh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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