MYSQL最后插入的行ID没有自动增量列

时间:2012-10-16 08:01:50

标签: mysql

我在Mysql中有一个存储过程

DELIMITER $$
CREATE PROCEDURE REG_LOCATE(IN MDN VARCHAR(50), IN CALLID VARCHAR(50))
BEGIN
DECLARE cmd CHAR(255);
DECLARE result int(10);
DECLARE transactionId VARCHAR(255);
INSERT INTO  transactions(id, device_number, call_id, creation_time, method_name) VALUES( UUID(), MDN, CALLID, now(), 'MDN_REG_LOCATE');
SELECT id FROM transactions ORDER BY creation_time desc LIMIT 1 INTO transactionId;
END;
$$
DELIMITER ;

我在事务表中插入了一行,其id由mysql的UUID()插入,现在我怎样才能从同一程序中的事务表中获取最后一个插入的id。我用过

SELECT id FROM transactions ORDER BY creation_time desc LIMIT 1 INTO transactionId;

但它没有给我最后一个插入的ID。请帮助我。 在此先感谢

1 个答案:

答案 0 :(得分:0)

首先将对UUID()的调用结果分配给变量,然后在INSERT语句中使用该变量的值:

DELIMITER $$

CREATE PROCEDURE REG_LOCATE(IN MDN VARCHAR(50), IN CALLID VARCHAR(50))
BEGIN
  DECLARE cmd CHAR(255);
  DECLARE result int(10);
  DECLARE transactionId VARCHAR(255);

  SET transactionId := UUID();
  INSERT INTO transactions
    (id, device_number, call_id, creation_time, method_name)
  VALUES
    (transactionId, MDN, CALLID, now(), 'MDN_REG_LOCATE');
END$$

DELIMITER ;