If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Temporary Variable

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-20-07, 04:24
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Temporary Variable

Hi

Is there a way to store LAST_INSERT_ID() in a temporary variable and use that variable in the succeeding queries ?

Code:
INSERT INTO `tbl1` VALUES ('', 5, 'data 1', 'data 2');

INSERT INTO `tbl2` VALUES ('', LAST_INSERT_ID(), 'x', 'y');
INSERT INTO `tbl2` VALUES ('', LAST_INSERT_ID(), 'a', 'b');
INSERT INTO `tbl2` VALUES ('', LAST_INSERT_ID(), '1', '2');
Obviously, in the last 2 inserts, this will keep inserting in tbl2, the prev auto-increment values that it just INSERTed in tbl2. I want it to refer to the LAST_INSERT_ID() of tbl1 in all 3 queries (last 3).

Is there a way ?

Thanks
__________________
MySQL 5.1
Reply With Quote
  #2 (permalink)  
Old 06-20-07, 04:39
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
If you can use a stored procedure, then yes, otherwise you will have to do it programatically
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 06-20-07, 04:43
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
I have MySQL 5.0.41. So I can use stored procedures, but is there a way to just store this in a variable without actually creating a function ?

EDIT : solved using SET @lid := LAST_INSERT_ID(); and then
INSERT INTO `tbl2` VALUES ('', @lid, 'x', 'y');
__________________
MySQL 5.1

Last edited by anjanesh; 06-20-07 at 05:21.
Reply With Quote
  #4 (permalink)  
Old 06-20-07, 05:52
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Do it all in a transaction set just to be safe...
Reply With Quote
  #5 (permalink)  
Old 06-20-07, 07:35
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Code:
SET @lid:= (SELECT `id` FROM `tbl1` WHERE `uniqueKEY` = '12345');
IF @lid == NULL THEN
   INSERT INTO `tbl1` VALUES ('', '12345', data 1', 'data 2');
   SET @lid := LAST_INSERT_ID();
END IF;

INSERT INTO `tbl2` VALUES ('', @lid, 'x', 'y');
.
.
I get this error message :
Code:
ERROR: Unknown Punctuation String @ 146
STR: ==
SQL: SET @lid := (SELECT `id` FROM `tbl1` WHERE `uniqueKEY` = '12345'); # MySQL returned an empty result set (i.e. zero rows).
Any clue to why @lid == NULL isnt right ?
__________________
MySQL 5.1
Reply With Quote
  #6 (permalink)  
Old 06-20-07, 07:45
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
The syntax in SQL is = NOT ==.
That latter is a programming language construct.
i.e.
IF @lid = NULL THEN...

Edit:
In fact come to think of it you're testing NULL, thus
IF @lid IS NULL THEN...
Reply With Quote
  #7 (permalink)  
Old 06-20-07, 07:54
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Code:
IF @lid IS NULL THEN
   INSERT INTO `tbl1` VALUES ('', '12345', data 1', 'data 2');
   SET @lid := LAST_INSERT_ID();
END IF;
Now I get a different error
Code:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF @lid IS NULL THEN
   INSERT INTO `tbl1` VALUES ('', '12345', 'data 1' at line 1
__________________
MySQL 5.1
Reply With Quote
  #8 (permalink)  
Old 06-21-07, 04:28
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
What is the DDL of your tbl1 ?

Looks like you might be missing a quote
INSERT INTO `tbl1` VALUES ('', '12345', data 1', 'data 2');
but I can't be certain given your error message it appears to have been quoted correctly.
Reply With Quote
  #9 (permalink)  
Old 06-21-07, 04:44
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Apparently, I was trying to do something that cannot be done without it being inside a PROCEDURE. It has been solved. Thanks.
__________________
MySQL 5.1
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On