DELIMITER $ drop procedure if exists insert_f_arenarank$ CREATE PROCEDURE `insert_f_arenarank`(in uid varchar(100)) BEGIN declare _prep int; declare _id int; DECLARE t_error INTEGER DEFAULT 0; DECLARE EXIT HANDLER FOR SQLEXCEPTION SET t_error=1; start transaction; set _prep = (select id from t_arenarank where next is null); insert into t_arenarank (userid,prep) values(uid,_prep); set _id = @@IDENTITY; update t_arenarank set next = _id where id=_prep; if t_error=1 then rollback; else commit; end if; END$ DELIMITER ;
1.varchar(100) 括号及其内容是不可以省略的;
2.# 是不能注释的;
3.判断是否有错误时:DECLARE EXIT HANDLER FOR SQLEXCEPTION SET t_error=1;
而不是DECLARE continue。
附:3种错误:
DECLARE EXIT HANDLER FOR 1062 Select '键值重复'; DECLARE EXIT HANDLER FOR SQLEXCEPTION Select 'SQLException异常'; DECLARE EXIT HANDLER FOR SQLSTATE '23000' Select 'SQLSTATE 23000';