Solved

Stored Procedure Using LAST_INSERT_ID()

Posted on 2008-10-08
2
1,319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql disables rename 4 103
MYSQL database problem within Coldfusion 2016 environment 12 58
MySql Recovery 2 35
Unwanted output from my query 5 42
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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