0

我在 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。请帮助我。提前致谢

4

1 回答 1

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 ;
于 2012-10-16T08:02:58.817 回答