views:

141

answers:

1

i use a trigger to insert a row and want to use the last created id for using in the subsequent query.

how could i do this?

the code looks like:

BEGIN
IF (NEW.counter >= 100) THEN
INSERT INTO tagCategories (name, counter) VALUES ('unnamed', NEW.counter);
// here i want to have access to the above inserted id
UPDATE tagCategories2tagPairs SET tagCategoryId = <<ID_HERE>> WHERE tagPairId = OLD.id
END IF;
END
+3  A: 

Have you looked at LAST_INSERTED_ID()? But be aware:

If you insert multiple rows using a single INSERT statement, LAST_INSERT_ID() returns the value generated for the first inserted row only.

Mitch Wheat