CREATE PROCEDURE genid ()
BEGIN
DECLARE st VARCHAR(100);
START TRANSACTION;
IF NOT EXISTS (SELECT 1 FROM order_key) THEN
SET @sqlstr=CONCAT('insert into order_key select substring(`key`,8) from order', DATE_FORMAT(CURDATE(), '%Y%m'), ' order by id desc limit 1');
PREPARE st FROM @sqlstr;
EXECUTE st;
ELSE
UPDATE order_key SET id=id+1;
END IF;
SELECT id FROM order_key;
COMMIT;
END
order201208 id key data 1 x2012081 ds 2 x2012082 24 3 x2012083 14
也就是说现在取到的是3 3会被插入到order_key的id字段中。然后被跟新为4
上面的存储过程我执行的时候有问题,苦于找不到问题来求助!
------解决方案--------------------
delimiter // CREATE PROCEDURE genid () BEGIN
DECLARE st VARCHAR(100); START TRANSACTION;
IF NOT EXISTS (SELECT 1 FROM order_key) THEN SET @sqlstr=CONCAT('insert into order_key select substring(`key`,8) from order', DATE_FORMAT(CURDATE(), '%Y%m'), ' order by id desc limit 1'); PREPARE st FROM @sqlstr; EXECUTE st; ELSE UPDATE order_key SET id=id+1; END IF; SELECT id FROM order_key; COMMIT; END //
------解决方案--------------------