CREATE DEFINER=`root`@`%` PROCEDURE `test`(`num` int) BEGIN /*定义变量*/ DECLARE sex TINYINT(2) DEFAULT 0; #查询结果给变量赋值 SELECT gender INTO sex FROM gyy_user_t WHERE id=num; #if 语句 IF sex=1 THEN SET sex=2; ELSE SET sex=1; END IF; #使用变量 UPDATE gyy_user_t SET gender = sex WHERE id=num; #循环 DECLARE i INT(11) DEFAULT 0; lop:LOOP SELECT i; IF i=5 THEN LEAVE lop;#跳出循环 END IF; SET i = i + 1; END LOOP lop; /*查询结果放游标进行遍历*/ DECLARE idnew INT(11); declare done INT(2); DECLARE idList CURSOR FOR (SELECT id FROM gyy_user_t WHERE id IN(10267,10268,10269)); DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;#游标中的内容执行完后将done设置为1 OPEN idList; lop:LOOP #1. 取游标中的值 FETCH idList INTO idnew; #2. 跳出循环条件 IF done=1 THEN LEAVE lop; END IF; #3. 执行业务逻辑 SELECT idnew; END LOOP lop; CLOSE idList; END