Discussion:
@@Identity trouble
(too old to reply)
Kev
2009-11-15 18:44:57 UTC
Permalink
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
2009-11-19 09:19:19 UTC
Permalink
Try this way..
INSERT ......... INTO orders .....
Post by Kev
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 .....
         or
Can someone give me some guidance please.
Thanks
Continue reading on narkive:
Loading...