Solved

Stored Procedure Using LAST_INSERT_ID()

Posted on 2008-10-08
2
1,309 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now