MYSQL INSERT ID

Im writing stored procedure, MYSQL version 5.1

how can i get the last added or updated ID.
The table has a primary key called `stock_id`

INSERT INTO stocks 
		SET 	ctns_or_loose = usp_remaining_loose ,
			inventory_id = usp_inventory_id ,
			pcs_in_ctn = 0 ,
			stock_location_id  = usp_stock_id
		ON DUPLICATE KEY UPDATE ctns_or_loose = ctns_or_loose + usp_remaining_loose ;

Open in new window

LVL 1
CMDAIAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

theGhost_k8Database ConsultantCommented:
may be u can use last_insert_id stored in a variable:

SELECT LAST_INSERT_ID() INTO lstInsId;
0
thejaswipadmanabhaCommented:
ideally SELECT LAST_INSERT_ID(); would work a sample PHP code is as below
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
?>
0
tdotCommented:
AFTER SQL  QUERY
$id = mysql_insert_id();
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

CMDAIAuthor Commented:
Not using php here....

Thank this works
SELECT LAST_INSERT_ID()

how about if im using ON DUPLICATE KEY UPDATE,
is there a way to find out which which  key was updated?
0
CMDAIAuthor Commented:
I found this guy doing it but cant get his example to work....
http://www.sreeju-on-web.co.cc/?p=10

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CMDAIAuthor Commented:
oh ok i got it

I thought id was a variable , id is the index column.
0
CMDAIAuthor Commented:
Thank for your help, this is my result
INSERT INTO stocks 
SET 	ctns_or_loose = usp_remaining_loose ,
        inventory_id = usp_inventory_id ,
	pcs_in_ctn = 0 ,
	stock_location_id = usp_stock_id
ON DUPLICATE KEY UPDATE stock_id = LAST_INSERT_ID(stock_id) , ctns_or_loose = ctns_or_loose + usp_remaining_loose ;

Open in new window

0
Ioannis AnifantakisProgramming InstructorCommented:
what I use to get last inserted id is the following, but you have to be inside a transaction to do that.

1) start transaction;
2) insert blah blah blah
3) select @@identity as id;
4) commit;

@@identity always returns last id number of your insert.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.