Jump to content

Using last_insert_id() in Trigger


clarkdej

Recommended Posts

I need to take the autoincrement value and add it to the first two letters of the last name, frist two letters of the last name, to create an 8 character barcode value.  Here is my Trigger;

BEFORE INSERT ON patron
FOR EACH ROW
begin
set @namestring = left(new.namel,2);
set @namestring2 = left(new.namef,2);
set @namestring3= last_insert_id();
set new.barcode = concat(@namestring,@namestring2,@namestring3);
end

Problem:  @namestring3 always returns 0.

What is the proper way to do this?

Using mySQL 5.0 with third party app generating insert.  Connector is 3.51.12
Link to comment
Share on other sites

I've never used triggers, but I've also never seen auto-increment used this way.  Don't you need to run "SELECT LAST_INSERT_ID()"?  Also, if you're doing this _before_ the INSERT, doesn't this mean that you can never get the new UID since you haven't actually run an insert yet?  Maybe "AFTER INSERT" is more appropriate?  Again, I'm not a trigger expert, just a hunch.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.