Solved

Stored Procedure Using LAST_INSERT_ID()

Posted on 2008-10-08
2
1,316 Views
Last Modified: 2010-07-27
Im trying to use LAST_INSERT_ID() as variable in a stored procedure
after the first insert statment is run i want to return the id of the last auto inc record and then use that value in the second insert statement.
attached is my current procedure it returns the following error:
Unknown column 'NewLockID' in 'field list'




CREATE DEFINER=`root`@`localhost` PROCEDURE `SP_LockAdd`(
IN LockSerial VarChar(50),
IN LockDescription VarChar(50),
IN LockAssetID VarChar(50),
IN LockName VarChar(50),
IN LockLabelID VarChar(45),
IN LockAdded VarChar(45),
IN LockAddedBy VarChar(45),
IN LockStreet VarChar(45),
IN LockCity VarChar(45),
IN LockState VarChar(45),
IN LockZip VarChar(45),
IN LockCountry VarChar(45),
IN RouteID INT (11))
BEGIN
INSERT INTO esc.tbllocks (fldIDElectronic,fldDescription,fldAssetID,fldName,fldIDLabel,fldadded,fldaddedby,fldaddress1,fldcity,fldstate,fldzip,fldcountry) VALUES
(LockSerial,LockDescription,LockAssetID,LockName,LockLabelID,LockAdded,LockAddedBy,LockStreet,LockCity,LockState,LockZip,LockCountry);
SELECT LAST_INSERT_ID() as NewLockID;
INSERT INTO tblroutestolocks (fldLockID,fldRouteID) VALUES
(NewLockID,RouteID);
END

Open in new window

0
Comment
Question by:philosullivan
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22673768
what about this:;
CREATE DEFINER=`root`@`localhost` PROCEDURE `SP_LockAdd`(
IN p_LockSerial VarChar(50),
IN p_LockDescription VarChar(50),
IN p_LockAssetID VarChar(50),
IN p_LockName VarChar(50),
IN p_LockLabelID VarChar(45),
IN p_LockAdded VarChar(45),
IN p_LockAddedBy VarChar(45),
IN p_LockStreet VarChar(45),
IN p_LockCity VarChar(45),
IN p_LockState VarChar(45),
IN p_LockZip VarChar(45),
IN p_LockCountry VarChar(45),
IN p_RouteID INT (11))
BEGIN
INSERT INTO esc.tbllocks (fldIDElectronic,fldDescription,fldAssetID,fldName,fldIDLabel,fldadded,fldaddedby,fldaddress1,fldcity,fldstate,fldzip,fldcountry) VALUES
(p_LockSerial,p_LockDescription,p_LockAssetID,p_LockName,p_LockLabelID,p_LockAdded,p_LockAddedBy,p_LockStreet,p_LockCity,p_LockState,p_LockZip,p_LockCountry);
INSERT INTO tblroutestolocks (fldLockID,fldRouteID) SELECT LAST_INSERT_ID() ,p_RouteID;
END

Open in new window

0
 

Author Closing Comment

by:philosullivan
ID: 31504465
awsome, thanks! i see what i was doing wrong.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question