MySQL Forums
Forum List  »  German

trigger + last_insert_id
Posted by: uwe resch
Date: May 27, 2011 06:01AM

Hi zusammen,

hab schon überal gesucht und im trigger forum antwortet keiner...
ich hoffe hier kann mir jemand helfen.

Gibt es eine möglichkeit bei einem Before Insert trigger der die ID ändert also die die Autoincrement funktion nicht ausführt die Last_insert_id auf diese Zeile zu setzen?

hier mal ein beispiel was ich meine:

CREATE TABLE `t1` (
`a` bigint(20) NOT NULL AUTO_INCREMENT,
`b` varchar(255) DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

CREATE TABLE `t2` (
`a` bigint(20) NOT NULL AUTO_INCREMENT,
`b` varchar(255) DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

CREATE TRIGGER `t1_BI` BEFORE INSERT ON `t1`
FOR EACH ROW BEGIN
INSERT INTO t2(b) VALUES(new.b);
SET new.a=(SELECT a FROM t2 WHERE a=LAST_INSERT_ID());
END;


INSERT INTO t2(b)VALUES('xxx');

Select Last_Insert_ID(); ---->>> 1

INSERT INTO t1(b)VALUES('yyy');

Select Last_Insert_ID(); ---->>> 0

hier brauch ich aber die 2 .... kann mir jemand einen schups in die richtige richtung geben wie man das problem lösen kann?

Options: ReplyQuote


Subject
Views
Written By
Posted
trigger + last_insert_id
4466
May 27, 2011 06:01AM
2099
May 27, 2011 07:02AM
2123
May 27, 2011 10:34AM
1852
May 28, 2011 12:55AM
1906
May 28, 2011 09:45AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.