Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

@@Identity trouble

0 views
Skip to first unread message

Kev

unread,
Nov 15, 2009, 1:44:57 PM11/15/09
to
Hi all,

I am new tomySQL (no to SQL though) and am having trouble with
something. After I insert a new record I want to get the ID of that
record into a variable (so I can use it in subsequent inserts where it
is the foreign key). However this does not work:-

INSERT ......... INTO orders .....

DECLARE @new_id INT;
SET @new_id = 7;

SET @new_id = SELECT @@IDENTITY FROM orders;
or
SELECT @@IDENTITY INTO @new_id FROM orders;

SELECT @new_id;

Can someone give me some guidance please.

Thanks

ushastry

unread,
Nov 19, 2009, 4:19:19 AM11/19/09
to
Try this way..

INSERT ......... INTO orders .....

SET @new_id=LAST_INSERT_ID();

select @new_id;

0 new messages