Solved

Stored Procedure Using LAST_INSERT_ID()

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

19 Experts available now in Live!

Get 1:1 Help Now