http://www.2cto.com/database/201408/327315.html http://blog.csdn.net/pleasurehappy/article/details/51087995 http://www.jb51.net/article/42906.htm http://www.jb51.net/article/52251.htm http://www.cnblogs.com/lxs1314/p/5945428.html delimiter $$ drop procedure if exists lopp; create procedure lopp() begin declare i int ; set i = 1; lp1 : LOOP              // lp1 为循环体名称 LOOP 为关键字insert into user_profile (uid) values (i); set i = i+1; if i > 30 then leave lp1;              // 离开循环体 end if; end LOOP;               // 结束循环 end $$ delimiter $$ drop procedure if exists `cx` $$ create procedure cx() begin declare i int; set i=1; while i<10000 DO insert into t(title)values(i); set i=i+1; end while; end $$ delimiter $$ drop procedure if exists `cx` $$ create procedure cx() begin declare flag int default 0; declare i int; set i=10000; REPEAT insert into t(title)values(i); set i=i+1; if(i>=10010) then set flag=1; end if; UNTIL flag=1 END REPEAT; end $$ ------------------------------------------ delimiter $$ use newcimie $$ drop procedure if exists `cx` $$ create procedure cx() begin declare i int; set i=1; select i; end $$ delimiter $$ drop procedure if exists `addAge` $$ create procedure addAge(age int,fun varchar(10)) begin if fun='add' then select 'add'; else select 'j'; end if; end $$ drop procedure if exists `addAge` $$ create procedure addAge(age int,fun varchar(10)) begin select age; select fun; end $$ ------------------------- declare i int; #声明变量 declare s int; #声明变量 set s = 0; #变量赋值 set i = 1; #变量赋值 //循环 while i<=n do set s =s+i; set i=i+1; end while; //输出 select s; --------------------- create procedure p4(n smallint) #含参 begin end $ --------------------- drop procedure if exists `p3` $$ create procedure p3(n int) #含参 begin if j='stu' then #与其他语言不同 必须加then 注意符号= 不是== select * from web_student where cid > n; else select * from web_student where cid n; end $$ delimiter $$ drop procedure if exists `song` $$ create procedure song() begin -- 声明一个标志done, 用来判断游标是否遍历完成
DECLARE done INT DEFAULT 0; DECLARE id int(8); DECLARE title varchar(200) DEFAULT ""; DECLARE article CURSOR FOR select id,title from web_article order by id desc limit 10; -- 在游标循环到最后会将 done 设置为 1
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; OPEN article; -- 遍历游标每一行
REPEAT -- 把一行的信息存放在对应的变量中
FETCH article INTO id,title; if not done then select id,title; end if; UNTIL done END REPEAT; CLOSE article; end $$ delimiter $$ drop procedure if exists `song` $$ create procedure song() begin -- 声明一个标志done, 用来判断游标是否遍历完成
DECLARE done INT DEFAULT 0; DECLARE id bigint(8); DECLARE title varchar(80) DEFAULT ""; DECLARE article CURSOR FOR select id,title from web_article order by id desc limit 10; -- 在游标循环到最后会将 done 设置为 1
DECLARE CONTINUE HANDLER FOR sqlstate '02000' SET done = 1; OPEN article; FETCH article INTO id,title; while done <> 1 do select id,title; FETCH article INTO id,title; end while; CLOSE article; end $$ 遍历表时候,需要注意的是定义的变量名称不能和数据库中定义的字段名一样,否则循环出来全部是null delimiter $$ drop procedure if exists `test` $$ create procedure test() begin DECLARE tmpflat INT DEFAULT 0; DECLARE tmpName varchar(20); DECLARE cur1 CURSOR FOR select id from web_article limit 10; DECLARE CONTINUE HANDLER FOR NOT FOUND SET tmpflat = null; OPEN cur1; FETCH cur1 INTO tmpName; WHILE(tmpflat is not null)DO select tmpName; FETCH cur1 INTO tmpName; END WHILE; CLOSE cur1; end $$ delimiter ; delimiter $$ drop procedure if exists `c42` $$ create procedure c42() begin DECLARE tmpflat INT DEFAULT 0; DECLARE sBrands varchar(30) default "0"; DECLARE tmpId int(4); DECLARE tmpTitle varchar(80); DECLARE tmpBrands varchar(80); DECLARE cur1 CURSOR FOR select id,title,brands from web_article where nav=42; DECLARE CONTINUE HANDLER FOR NOT FOUND SET tmpflat = null; OPEN cur1; FETCH cur1 INTO tmpId,tmpTitle,tmpBrands; WHILE(tmpflat is not null)DO set sBrands=0; if(locate("米其林",tmpTitle)>0) then set sBrands=15; elseif(locate("普利司通",tmpTitle)>0) then set sBrands=16; elseif(locate("风驰通",tmpTitle)>0) then set sBrands=16; elseif(locate("韩泰",tmpTitle)>0) then set sBrands=18; elseif(locate("奥拉力",tmpTitle)>0) then set sBrands=18; elseif(locate("正新",tmpTitle)>0) then set sBrands=39; elseif(locate("樱花",tmpTitle)>0) then set sBrands=39; elseif(locate("樱玛",tmpTitle)>0) then set sBrands=39; elseif(locate("益新",tmpTitle)>0) then set sBrands=39; elseif(locate("金系",tmpTitle)>0) then set sBrands=23; elseif(locate("欧耐特",tmpTitle)>0) then set sBrands=23; elseif(locate("凯达",tmpTitle)>0) then set sBrands=23; elseif(locate("雅度",tmpTitle)>0) then set sBrands=23; elseif(locate("安达",tmpTitle)>0) then set sBrands=23; elseif(locate("欧系",tmpTitle)>0) then set sBrands=23; elseif(locate("全诺",tmpTitle)>0) then set sBrands=23; elseif(locate("好运",tmpTitle)>0) then set sBrands=23; elseif(locate("威狮",tmpTitle)>0) then set sBrands=23; elseif(locate("雅度",tmpTitle)>0) then set sBrands=23; elseif(locate("朝阳",tmpTitle)>0) then set sBrands=23; elseif(locate("力奔",tmpTitle)>0) then set sBrands=23; elseif(locate("日升",tmpTitle)>0) then set sBrands=23; elseif(locate("金冠",tmpTitle)>0) then set sBrands=23; elseif(locate("超级舰队",tmpTitle)>0) then set sBrands=30; elseif(locate("飞跃",tmpTitle)>0) then set sBrands=30; elseif(locate("双钱",tmpTitle)>0) then set sBrands=30; elseif(locate("回力",tmpTitle)>0) then set sBrands=30; elseif(locate("万世达",tmpTitle)>0) then set sBrands=30; elseif(locate("万家达",tmpTitle)>0) then set sBrands=30; elseif(locate("佳安",tmpTitle)>0) then set sBrands=26; elseif(locate("佳通",tmpTitle)>0) then set sBrands=26; elseif(locate("邓禄普",tmpTitle)>0) then set sBrands=33; elseif(locate("玛吉斯",tmpTitle)>0) then set sBrands=36; elseif(locate("前进",tmpTitle)>0) then set sBrands=27; elseif(locate("大力士",tmpTitle)>0) then set sBrands=27; elseif(locate("多力通",tmpTitle)>0) then set sBrands=27; elseif(locate("风神",tmpTitle)>0) then set sBrands=28; elseif(locate("风力",tmpTitle)>0) then set sBrands=28; elseif(locate("风达",tmpTitle)>0) then set sBrands=28; elseif(locate("追风",tmpTitle)>0) then set sBrands=28; elseif(locate("玲珑",tmpTitle)>0) then set sBrands=31; elseif(locate("利奥",tmpTitle)>0) then set sBrands=31; elseif(locate("成山",tmpTitle)>0) then set sBrands=40; elseif(locate("富神",tmpTitle)>0) then set sBrands=40; elseif(locate("奥通 ",tmpTitle)>0) then set sBrands=40; elseif(locate("固特异",tmpTitle)>0) then set sBrands=17; elseif(locate("优途 ",tmpTitle)>0) then set sBrands=17; elseif(locate("金宇",tmpTitle)>0) then set sBrands=38; elseif(locate("赛轮 ",tmpTitle)>0) then set sBrands=38; elseif(locate("玉麒麟",tmpTitle)>0) then set sBrands=38; elseif(locate("盛凯 ",tmpTitle)>0) then set sBrands=38; elseif(locate("千里马 ",tmpTitle)>0) then set sBrands=41; elseif(locate("赤兔马",tmpTitle)>0) then set sBrands=41; elseif(locate("驰威福 ",tmpTitle)>0) then set sBrands=41; elseif(locate("喜达通",tmpTitle)>0) then set sBrands=41; elseif(locate("奥珀 ",tmpTitle)>0) then set sBrands=41; elseif(locate("骐马",tmpTitle)>0) then set sBrands=41; elseif(locate("瑞途 ",tmpTitle)>0) then set sBrands=41; elseif(locate("通运",tmpTitle)>0) then set sBrands=41; elseif(locate("劲步 ",tmpTitle)>0) then set sBrands=41; elseif(locate("三角",tmpTitle)>0) then set sBrands=25; elseif(locate("威力斯通 ",tmpTitle)>0) then set sBrands=25; elseif(locate("双星",tmpTitle)>0) then set sBrands=42; elseif(locate("达堡斯达 ",tmpTitle)>0) then set sBrands=42; elseif(locate("伊路顺",tmpTitle)>0) then set sBrands=42; elseif(locate("路路顺 ",tmpTitle)>0) then set sBrands=42; elseif(locate("兴源 ",tmpTitle)>0) then set sBrands=32; elseif(locate("恒丰",tmpTitle)>0) then set sBrands=32; elseif(locate("三泰 ",tmpTitle)>0) then set sBrands=32; elseif(locate("盛泰 ",tmpTitle)>0) then set sBrands=32; elseif(locate("固铂",tmpTitle)>0) then set sBrands=34; elseif(locate("优科豪马",tmpTitle)>0) then set sBrands=20; elseif(locate("马牌",tmpTitle)>0) then set sBrands=37; elseif(locate("倍耐力",tmpTitle)>0) then set sBrands=22; elseif(locate("朝阳",tmpTitle)>0) then set sBrands=23; elseif(locate("佳通",tmpTitle)>0) then set sBrands=26; elseif(locate("耐克森",tmpTitle)>0) then set sBrands=35; elseif(locate("锦湖",tmpTitle)>0) then set sBrands=19; elseif(locate("万力",tmpTitle)>0) then set sBrands=24; elseif(locate("金宇",tmpTitle)>0) then set sBrands=38; elseif(locate("东洋",tmpTitle)>0) then set sBrands=21; end if; update web_article set brands=sBrands where id=tmpId; if(locate("轿车",tmpTitle)>0) then update web_article set tyrenet_type=1 where id=tmpId; elseif(locate("卡客车",tmpTitle)>0) then update web_article set brands=3 where id=tmpId; end if; FETCH cur1 INTO tmpId,tmpTitle,tmpBrands; END WHILE; CLOSE cur1; end $$ delimiter ; delimiter $$ drop procedure if exists `c15` $$ create procedure c15() begin DECLARE tmpflat INT DEFAULT 0; DECLARE sBrands varchar(30) default "0"; DECLARE tmpId int(4); DECLARE tmpTitle varchar(80); DECLARE tmpBrands varchar(80); DECLARE cur1 CURSOR FOR select id,title,brands from web_article where nav=15; DECLARE CONTINUE HANDLER FOR NOT FOUND SET tmpflat = null; OPEN cur1; FETCH cur1 INTO tmpId,tmpTitle,tmpBrands; WHILE(tmpflat is not null)DO set sBrands=0; if(locate("米其林",tmpTitle)>0) then set sBrands=15; elseif(locate("普利司通",tmpTitle)>0) then set sBrands=16; elseif(locate("风驰通",tmpTitle)>0) then set sBrands=16; elseif(locate("韩泰",tmpTitle)>0) then set sBrands=18; elseif(locate("奥拉力",tmpTitle)>0) then set sBrands=18; elseif(locate("正新",tmpTitle)>0) then set sBrands=39; elseif(locate("樱花",tmpTitle)>0) then set sBrands=39; elseif(locate("樱玛",tmpTitle)>0) then set sBrands=39; elseif(locate("益新",tmpTitle)>0) then set sBrands=39; elseif(locate("金系",tmpTitle)>0) then set sBrands=23; elseif(locate("欧耐特",tmpTitle)>0) then set sBrands=23; elseif(locate("凯达",tmpTitle)>0) then set sBrands=23; elseif(locate("雅度",tmpTitle)>0) then set sBrands=23; elseif(locate("安达",tmpTitle)>0) then set sBrands=23; elseif(locate("欧系",tmpTitle)>0) then set sBrands=23; elseif(locate("全诺",tmpTitle)>0) then set sBrands=23; elseif(locate("好运",tmpTitle)>0) then set sBrands=23; elseif(locate("威狮",tmpTitle)>0) then set sBrands=23; elseif(locate("雅度",tmpTitle)>0) then set sBrands=23; elseif(locate("朝阳",tmpTitle)>0) then set sBrands=23; elseif(locate("力奔",tmpTitle)>0) then set sBrands=23; elseif(locate("日升",tmpTitle)>0) then set sBrands=23; elseif(locate("金冠",tmpTitle)>0) then set sBrands=23; elseif(locate("超级舰队",tmpTitle)>0) then set sBrands=30; elseif(locate("飞跃",tmpTitle)>0) then set sBrands=30; elseif(locate("双钱",tmpTitle)>0) then set sBrands=30; elseif(locate("回力",tmpTitle)>0) then set sBrands=30; elseif(locate("万世达",tmpTitle)>0) then set sBrands=30; elseif(locate("万家达",tmpTitle)>0) then set sBrands=30; elseif(locate("佳安",tmpTitle)>0) then set sBrands=26; elseif(locate("佳通",tmpTitle)>0) then set sBrands=26; elseif(locate("邓禄普",tmpTitle)>0) then set sBrands=33; elseif(locate("玛吉斯",tmpTitle)>0) then set sBrands=36; elseif(locate("前进",tmpTitle)>0) then set sBrands=27; elseif(locate("大力士",tmpTitle)>0) then set sBrands=27; elseif(locate("多力通",tmpTitle)>0) then set sBrands=27; elseif(locate("风神",tmpTitle)>0) then set sBrands=28; elseif(locate("风力",tmpTitle)>0) then set sBrands=28; elseif(locate("风达",tmpTitle)>0) then set sBrands=28; elseif(locate("追风",tmpTitle)>0) then set sBrands=28; elseif(locate("玲珑",tmpTitle)>0) then set sBrands=31; elseif(locate("利奥",tmpTitle)>0) then set sBrands=31; elseif(locate("成山",tmpTitle)>0) then set sBrands=40; elseif(locate("富神",tmpTitle)>0) then set sBrands=40; elseif(locate("奥通 ",tmpTitle)>0) then set sBrands=40; elseif(locate("固特异",tmpTitle)>0) then set sBrands=17; elseif(locate("优途 ",tmpTitle)>0) then set sBrands=17; elseif(locate("金宇",tmpTitle)>0) then set sBrands=38; elseif(locate("赛轮 ",tmpTitle)>0) then set sBrands=38; elseif(locate("玉麒麟",tmpTitle)>0) then set sBrands=38; elseif(locate("盛凯 ",tmpTitle)>0) then set sBrands=38; elseif(locate("千里马 ",tmpTitle)>0) then set sBrands=41; elseif(locate("赤兔马",tmpTitle)>0) then set sBrands=41; elseif(locate("驰威福 ",tmpTitle)>0) then set sBrands=41; elseif(locate("喜达通",tmpTitle)>0) then set sBrands=41; elseif(locate("奥珀 ",tmpTitle)>0) then set sBrands=41; elseif(locate("骐马",tmpTitle)>0) then set sBrands=41; elseif(locate("瑞途 ",tmpTitle)>0) then set sBrands=41; elseif(locate("通运",tmpTitle)>0) then set sBrands=41; elseif(locate("劲步 ",tmpTitle)>0) then set sBrands=41; elseif(locate("三角",tmpTitle)>0) then set sBrands=25; elseif(locate("威力斯通 ",tmpTitle)>0) then set sBrands=25; elseif(locate("双星",tmpTitle)>0) then set sBrands=42; elseif(locate("达堡斯达 ",tmpTitle)>0) then set sBrands=42; elseif(locate("伊路顺",tmpTitle)>0) then set sBrands=42; elseif(locate("路路顺 ",tmpTitle)>0) then set sBrands=42; elseif(locate("兴源 ",tmpTitle)>0) then set sBrands=32; elseif(locate("恒丰",tmpTitle)>0) then set sBrands=32; elseif(locate("三泰 ",tmpTitle)>0) then set sBrands=32; elseif(locate("盛泰 ",tmpTitle)>0) then set sBrands=32; elseif(locate("固铂",tmpTitle)>0) then set sBrands=34; elseif(locate("优科豪马",tmpTitle)>0) then set sBrands=20; elseif(locate("马牌",tmpTitle)>0) then set sBrands=37; elseif(locate("倍耐力",tmpTitle)>0) then set sBrands=22; elseif(locate("朝阳",tmpTitle)>0) then set sBrands=23; elseif(locate("佳通",tmpTitle)>0) then set sBrands=26; elseif(locate("耐克森",tmpTitle)>0) then set sBrands=35; elseif(locate("锦湖",tmpTitle)>0) then set sBrands=19; elseif(locate("万力",tmpTitle)>0) then set sBrands=24; elseif(locate("金宇",tmpTitle)>0) then set sBrands=38; elseif(locate("东洋",tmpTitle)>0) then set sBrands=21; end if; update web_article set brands=sBrands where id=tmpId; FETCH cur1 INTO tmpId,tmpTitle,tmpBrands; END WHILE; CLOSE cur1; end $$ delimiter ; delimiter $$ drop procedure if exists `c16` $$ create procedure c16() begin DECLARE tmpflat INT DEFAULT 0; DECLARE sBrands varchar(30) default "0"; DECLARE tmpId int(4); DECLARE tmpTitle varchar(80); DECLARE tmpBrands varchar(80); DECLARE cur1 CURSOR FOR select id,title,brands from web_article where nav=16; DECLARE CONTINUE HANDLER FOR NOT FOUND SET tmpflat = null; OPEN cur1; FETCH cur1 INTO tmpId,tmpTitle,tmpBrands; WHILE(tmpflat is not null)DO set sBrands=0; if(locate("米其林",tmpTitle)>0) then set sBrands=15; elseif(locate("普利司通",tmpTitle)>0) then set sBrands=16; elseif(locate("风驰通",tmpTitle)>0) then set sBrands=16; elseif(locate("韩泰",tmpTitle)>0) then set sBrands=18; elseif(locate("奥拉力",tmpTitle)>0) then set sBrands=18; elseif(locate("正新",tmpTitle)>0) then set sBrands=39; elseif(locate("樱花",tmpTitle)>0) then set sBrands=39; elseif(locate("樱玛",tmpTitle)>0) then set sBrands=39; elseif(locate("益新",tmpTitle)>0) then set sBrands=39; elseif(locate("金系",tmpTitle)>0) then set sBrands=23; elseif(locate("欧耐特",tmpTitle)>0) then set sBrands=23; elseif(locate("凯达",tmpTitle)>0) then set sBrands=23; elseif(locate("雅度",tmpTitle)>0) then set sBrands=23; elseif(locate("安达",tmpTitle)>0) then set sBrands=23; elseif(locate("欧系",tmpTitle)>0) then set sBrands=23; elseif(locate("全诺",tmpTitle)>0) then set sBrands=23; elseif(locate("好运",tmpTitle)>0) then set sBrands=23; elseif(locate("威狮",tmpTitle)>0) then set sBrands=23; elseif(locate("雅度",tmpTitle)>0) then set sBrands=23; elseif(locate("朝阳",tmpTitle)>0) then set sBrands=23; elseif(locate("力奔",tmpTitle)>0) then set sBrands=23; elseif(locate("日升",tmpTitle)>0) then set sBrands=23; elseif(locate("金冠",tmpTitle)>0) then set sBrands=23; elseif(locate("超级舰队",tmpTitle)>0) then set sBrands=30; elseif(locate("飞跃",tmpTitle)>0) then set sBrands=30; elseif(locate("双钱",tmpTitle)>0) then set sBrands=30; elseif(locate("回力",tmpTitle)>0) then set sBrands=30; elseif(locate("万世达",tmpTitle)>0) then set sBrands=30; elseif(locate("万家达",tmpTitle)>0) then set sBrands=30; elseif(locate("佳安",tmpTitle)>0) then set sBrands=26; elseif(locate("佳通",tmpTitle)>0) then set sBrands=26; elseif(locate("邓禄普",tmpTitle)>0) then set sBrands=33; elseif(locate("玛吉斯",tmpTitle)>0) then set sBrands=36; elseif(locate("前进",tmpTitle)>0) then set sBrands=27; elseif(locate("大力士",tmpTitle)>0) then set sBrands=27; elseif(locate("多力通",tmpTitle)>0) then set sBrands=27; elseif(locate("风神",tmpTitle)>0) then set sBrands=28; elseif(locate("风力",tmpTitle)>0) then set sBrands=28; elseif(locate("风达",tmpTitle)>0) then set sBrands=28; elseif(locate("追风",tmpTitle)>0) then set sBrands=28; elseif(locate("玲珑",tmpTitle)>0) then set sBrands=31; elseif(locate("利奥",tmpTitle)>0) then set sBrands=31; elseif(locate("成山",tmpTitle)>0) then set sBrands=40; elseif(locate("富神",tmpTitle)>0) then set sBrands=40; elseif(locate("奥通 ",tmpTitle)>0) then set sBrands=40; elseif(locate("固特异",tmpTitle)>0) then set sBrands=17; elseif(locate("优途 ",tmpTitle)>0) then set sBrands=17; elseif(locate("金宇",tmpTitle)>0) then set sBrands=38; elseif(locate("赛轮 ",tmpTitle)>0) then set sBrands=38; elseif(locate("玉麒麟",tmpTitle)>0) then set sBrands=38; elseif(locate("盛凯 ",tmpTitle)>0) then set sBrands=38; elseif(locate("千里马 ",tmpTitle)>0) then set sBrands=41; elseif(locate("赤兔马",tmpTitle)>0) then set sBrands=41; elseif(locate("驰威福 ",tmpTitle)>0) then set sBrands=41; elseif(locate("喜达通",tmpTitle)>0) then set sBrands=41; elseif(locate("奥珀 ",tmpTitle)>0) then set sBrands=41; elseif(locate("骐马",tmpTitle)>0) then set sBrands=41; elseif(locate("瑞途 ",tmpTitle)>0) then set sBrands=41; elseif(locate("通运",tmpTitle)>0) then set sBrands=41; elseif(locate("劲步 ",tmpTitle)>0) then set sBrands=41; elseif(locate("三角",tmpTitle)>0) then set sBrands=25; elseif(locate("威力斯通 ",tmpTitle)>0) then set sBrands=25; elseif(locate("双星",tmpTitle)>0) then set sBrands=42; elseif(locate("达堡斯达 ",tmpTitle)>0) then set sBrands=42; elseif(locate("伊路顺",tmpTitle)>0) then set sBrands=42; elseif(locate("路路顺 ",tmpTitle)>0) then set sBrands=42; elseif(locate("兴源 ",tmpTitle)>0) then set sBrands=32; elseif(locate("恒丰",tmpTitle)>0) then set sBrands=32; elseif(locate("三泰 ",tmpTitle)>0) then set sBrands=32; elseif(locate("盛泰 ",tmpTitle)>0) then set sBrands=32; elseif(locate("固铂",tmpTitle)>0) then set sBrands=34; elseif(locate("优科豪马",tmpTitle)>0) then set sBrands=20; elseif(locate("马牌",tmpTitle)>0) then set sBrands=37; elseif(locate("倍耐力",tmpTitle)>0) then set sBrands=22; elseif(locate("朝阳",tmpTitle)>0) then set sBrands=23; elseif(locate("佳通",tmpTitle)>0) then set sBrands=26; elseif(locate("耐克森",tmpTitle)>0) then set sBrands=35; elseif(locate("锦湖",tmpTitle)>0) then set sBrands=19; elseif(locate("万力",tmpTitle)>0) then set sBrands=24; elseif(locate("金宇",tmpTitle)>0) then set sBrands=38; elseif(locate("东洋",tmpTitle)>0) then set sBrands=21; end if; update web_article set brands=sBrands where id=tmpId; FETCH cur1 INTO tmpId,tmpTitle,tmpBrands; END WHILE; CLOSE cur1; end $$ delimiter ; delimiter $$ drop procedure if exists `c17` $$ create procedure c17() begin DECLARE tmpflat INT DEFAULT 0; DECLARE sBrands varchar(30) default "0"; DECLARE tmpId int(4); DECLARE tmpTitle varchar(80); DECLARE tmpBrands varchar(80); DECLARE cur1 CURSOR FOR select id,title,brands from web_article where nav=17; DECLARE CONTINUE HANDLER FOR NOT FOUND SET tmpflat = null; OPEN cur1; FETCH cur1 INTO tmpId,tmpTitle,tmpBrands; WHILE(tmpflat is not null)DO set sBrands=0; if(locate("米其林",tmpTitle)>0) then set sBrands=15; elseif(locate("普利司通",tmpTitle)>0) then set sBrands=16; elseif(locate("风驰通",tmpTitle)>0) then set sBrands=16; elseif(locate("韩泰",tmpTitle)>0) then set sBrands=18; elseif(locate("奥拉力",tmpTitle)>0) then set sBrands=18; elseif(locate("正新",tmpTitle)>0) then set sBrands=39; elseif(locate("樱花",tmpTitle)>0) then set sBrands=39; elseif(locate("樱玛",tmpTitle)>0) then set sBrands=39; elseif(locate("益新",tmpTitle)>0) then set sBrands=39; elseif(locate("金系",tmpTitle)>0) then set sBrands=23; elseif(locate("欧耐特",tmpTitle)>0) then set sBrands=23; elseif(locate("凯达",tmpTitle)>0) then set sBrands=23; elseif(locate("雅度",tmpTitle)>0) then set sBrands=23; elseif(locate("安达",tmpTitle)>0) then set sBrands=23; elseif(locate("欧系",tmpTitle)>0) then set sBrands=23; elseif(locate("全诺",tmpTitle)>0) then set sBrands=23; elseif(locate("好运",tmpTitle)>0) then set sBrands=23; elseif(locate("威狮",tmpTitle)>0) then set sBrands=23; elseif(locate("雅度",tmpTitle)>0) then set sBrands=23; elseif(locate("朝阳",tmpTitle)>0) then set sBrands=23; elseif(locate("力奔",tmpTitle)>0) then set sBrands=23; elseif(locate("日升",tmpTitle)>0) then set sBrands=23; elseif(locate("金冠",tmpTitle)>0) then set sBrands=23; elseif(locate("超级舰队",tmpTitle)>0) then set sBrands=30; elseif(locate("飞跃",tmpTitle)>0) then set sBrands=30; elseif(locate("双钱",tmpTitle)>0) then set sBrands=30; elseif(locate("回力",tmpTitle)>0) then set sBrands=30; elseif(locate("万世达",tmpTitle)>0) then set sBrands=30; elseif(locate("万家达",tmpTitle)>0) then set sBrands=30; elseif(locate("佳安",tmpTitle)>0) then set sBrands=26; elseif(locate("佳通",tmpTitle)>0) then set sBrands=26; elseif(locate("邓禄普",tmpTitle)>0) then set sBrands=33; elseif(locate("玛吉斯",tmpTitle)>0) then set sBrands=36; elseif(locate("前进",tmpTitle)>0) then set sBrands=27; elseif(locate("大力士",tmpTitle)>0) then set sBrands=27; elseif(locate("多力通",tmpTitle)>0) then set sBrands=27; elseif(locate("风神",tmpTitle)>0) then set sBrands=28; elseif(locate("风力",tmpTitle)>0) then set sBrands=28; elseif(locate("风达",tmpTitle)>0) then set sBrands=28; elseif(locate("追风",tmpTitle)>0) then set sBrands=28; elseif(locate("玲珑",tmpTitle)>0) then set sBrands=31; elseif(locate("利奥",tmpTitle)>0) then set sBrands=31; elseif(locate("成山",tmpTitle)>0) then set sBrands=40; elseif(locate("富神",tmpTitle)>0) then set sBrands=40; elseif(locate("奥通 ",tmpTitle)>0) then set sBrands=40; elseif(locate("固特异",tmpTitle)>0) then set sBrands=17; elseif(locate("优途 ",tmpTitle)>0) then set sBrands=17; elseif(locate("金宇",tmpTitle)>0) then set sBrands=38; elseif(locate("赛轮 ",tmpTitle)>0) then set sBrands=38; elseif(locate("玉麒麟",tmpTitle)>0) then set sBrands=38; elseif(locate("盛凯 ",tmpTitle)>0) then set sBrands=38; elseif(locate("千里马 ",tmpTitle)>0) then set sBrands=41; elseif(locate("赤兔马",tmpTitle)>0) then set sBrands=41; elseif(locate("驰威福 ",tmpTitle)>0) then set sBrands=41; elseif(locate("喜达通",tmpTitle)>0) then set sBrands=41; elseif(locate("奥珀 ",tmpTitle)>0) then set sBrands=41; elseif(locate("骐马",tmpTitle)>0) then set sBrands=41; elseif(locate("瑞途 ",tmpTitle)>0) then set sBrands=41; elseif(locate("通运",tmpTitle)>0) then set sBrands=41; elseif(locate("劲步 ",tmpTitle)>0) then set sBrands=41; elseif(locate("三角",tmpTitle)>0) then set sBrands=25; elseif(locate("威力斯通 ",tmpTitle)>0) then set sBrands=25; elseif(locate("双星",tmpTitle)>0) then set sBrands=42; elseif(locate("达堡斯达 ",tmpTitle)>0) then set sBrands=42; elseif(locate("伊路顺",tmpTitle)>0) then set sBrands=42; elseif(locate("路路顺 ",tmpTitle)>0) then set sBrands=42; elseif(locate("兴源 ",tmpTitle)>0) then set sBrands=32; elseif(locate("恒丰",tmpTitle)>0) then set sBrands=32; elseif(locate("三泰 ",tmpTitle)>0) then set sBrands=32; elseif(locate("盛泰 ",tmpTitle)>0) then set sBrands=32; elseif(locate("固铂",tmpTitle)>0) then set sBrands=34; elseif(locate("优科豪马",tmpTitle)>0) then set sBrands=20; elseif(locate("马牌",tmpTitle)>0) then set sBrands=37; elseif(locate("倍耐力",tmpTitle)>0) then set sBrands=22; elseif(locate("朝阳",tmpTitle)>0) then set sBrands=23; elseif(locate("佳通",tmpTitle)>0) then set sBrands=26; elseif(locate("耐克森",tmpTitle)>0) then set sBrands=35; elseif(locate("锦湖",tmpTitle)>0) then set sBrands=19; elseif(locate("万力",tmpTitle)>0) then set sBrands=24; elseif(locate("金宇",tmpTitle)>0) then set sBrands=38; elseif(locate("东洋",tmpTitle)>0) then set sBrands=21; end if; update web_article set brands=sBrands where id=tmpId; FETCH cur1 INTO tmpId,tmpTitle,tmpBrands; END WHILE; CLOSE cur1; end $$ delimiter ; delimiter $$ drop procedure if exists `c19` $$ create procedure c19() begin DECLARE tmpflat INT DEFAULT 0; DECLARE sBrands varchar(30) default "0"; DECLARE tmpId int(4); DECLARE tmpTitle varchar(80); DECLARE tmpBrands varchar(80); DECLARE cur1 CURSOR FOR select id,title,brands from web_article where nav=19; DECLARE CONTINUE HANDLER FOR NOT FOUND SET tmpflat = null; OPEN cur1; FETCH cur1 INTO tmpId,tmpTitle,tmpBrands; WHILE(tmpflat is not null)DO set sBrands=0; if(locate("米其林",tmpTitle)>0) then set sBrands=15; elseif(locate("普利司通",tmpTitle)>0) then set sBrands=16; elseif(locate("风驰通",tmpTitle)>0) then set sBrands=16; elseif(locate("韩泰",tmpTitle)>0) then set sBrands=18; elseif(locate("奥拉力",tmpTitle)>0) then set sBrands=18; elseif(locate("正新",tmpTitle)>0) then set sBrands=39; elseif(locate("樱花",tmpTitle)>0) then set sBrands=39; elseif(locate("樱玛",tmpTitle)>0) then set sBrands=39; elseif(locate("益新",tmpTitle)>0) then set sBrands=39; elseif(locate("金系",tmpTitle)>0) then set sBrands=23; elseif(locate("欧耐特",tmpTitle)>0) then set sBrands=23; elseif(locate("凯达",tmpTitle)>0) then set sBrands=23; elseif(locate("雅度",tmpTitle)>0) then set sBrands=23; elseif(locate("安达",tmpTitle)>0) then set sBrands=23; elseif(locate("欧系",tmpTitle)>0) then set sBrands=23; elseif(locate("全诺",tmpTitle)>0) then set sBrands=23; elseif(locate("好运",tmpTitle)>0) then set sBrands=23; elseif(locate("威狮",tmpTitle)>0) then set sBrands=23; elseif(locate("雅度",tmpTitle)>0) then set sBrands=23; elseif(locate("朝阳",tmpTitle)>0) then set sBrands=23; elseif(locate("力奔",tmpTitle)>0) then set sBrands=23; elseif(locate("日升",tmpTitle)>0) then set sBrands=23; elseif(locate("金冠",tmpTitle)>0) then set sBrands=23; elseif(locate("超级舰队",tmpTitle)>0) then set sBrands=30; elseif(locate("飞跃",tmpTitle)>0) then set sBrands=30; elseif(locate("双钱",tmpTitle)>0) then set sBrands=30; elseif(locate("回力",tmpTitle)>0) then set sBrands=30; elseif(locate("万世达",tmpTitle)>0) then set sBrands=30; elseif(locate("万家达",tmpTitle)>0) then set sBrands=30; elseif(locate("佳安",tmpTitle)>0) then set sBrands=26; elseif(locate("佳通",tmpTitle)>0) then set sBrands=26; elseif(locate("邓禄普",tmpTitle)>0) then set sBrands=33; elseif(locate("玛吉斯",tmpTitle)>0) then set sBrands=36; elseif(locate("前进",tmpTitle)>0) then set sBrands=27; elseif(locate("大力士",tmpTitle)>0) then set sBrands=27; elseif(locate("多力通",tmpTitle)>0) then set sBrands=27; elseif(locate("风神",tmpTitle)>0) then set sBrands=28; elseif(locate("风力",tmpTitle)>0) then set sBrands=28; elseif(locate("风达",tmpTitle)>0) then set sBrands=28; elseif(locate("追风",tmpTitle)>0) then set sBrands=28; elseif(locate("玲珑",tmpTitle)>0) then set sBrands=31; elseif(locate("利奥",tmpTitle)>0) then set sBrands=31; elseif(locate("成山",tmpTitle)>0) then set sBrands=40; elseif(locate("富神",tmpTitle)>0) then set sBrands=40; elseif(locate("奥通 ",tmpTitle)>0) then set sBrands=40; elseif(locate("固特异",tmpTitle)>0) then set sBrands=17; elseif(locate("优途 ",tmpTitle)>0) then set sBrands=17; elseif(locate("金宇",tmpTitle)>0) then set sBrands=38; elseif(locate("赛轮 ",tmpTitle)>0) then set sBrands=38; elseif(locate("玉麒麟",tmpTitle)>0) then set sBrands=38; elseif(locate("盛凯 ",tmpTitle)>0) then set sBrands=38; elseif(locate("千里马 ",tmpTitle)>0) then set sBrands=41; elseif(locate("赤兔马",tmpTitle)>0) then set sBrands=41; elseif(locate("驰威福 ",tmpTitle)>0) then set sBrands=41; elseif(locate("喜达通",tmpTitle)>0) then set sBrands=41; elseif(locate("奥珀 ",tmpTitle)>0) then set sBrands=41; elseif(locate("骐马",tmpTitle)>0) then set sBrands=41; elseif(locate("瑞途 ",tmpTitle)>0) then set sBrands=41; elseif(locate("通运",tmpTitle)>0) then set sBrands=41; elseif(locate("劲步 ",tmpTitle)>0) then set sBrands=41; elseif(locate("三角",tmpTitle)>0) then set sBrands=25; elseif(locate("威力斯通 ",tmpTitle)>0) then set sBrands=25; elseif(locate("双星",tmpTitle)>0) then set sBrands=42; elseif(locate("达堡斯达 ",tmpTitle)>0) then set sBrands=42; elseif(locate("伊路顺",tmpTitle)>0) then set sBrands=42; elseif(locate("路路顺 ",tmpTitle)>0) then set sBrands=42; elseif(locate("兴源 ",tmpTitle)>0) then set sBrands=32; elseif(locate("恒丰",tmpTitle)>0) then set sBrands=32; elseif(locate("三泰 ",tmpTitle)>0) then set sBrands=32; elseif(locate("盛泰 ",tmpTitle)>0) then set sBrands=32; elseif(locate("固铂",tmpTitle)>0) then set sBrands=34; elseif(locate("优科豪马",tmpTitle)>0) then set sBrands=20; elseif(locate("马牌",tmpTitle)>0) then set sBrands=37; elseif(locate("倍耐力",tmpTitle)>0) then set sBrands=22; elseif(locate("朝阳",tmpTitle)>0) then set sBrands=23; elseif(locate("佳通",tmpTitle)>0) then set sBrands=26; elseif(locate("耐克森",tmpTitle)>0) then set sBrands=35; elseif(locate("锦湖",tmpTitle)>0) then set sBrands=19; elseif(locate("万力",tmpTitle)>0) then set sBrands=24; elseif(locate("金宇",tmpTitle)>0) then set sBrands=38; elseif(locate("东洋",tmpTitle)>0) then set sBrands=21; end if; update web_article set brands=sBrands where id=tmpId; FETCH cur1 INTO tmpId,tmpTitle,tmpBrands; END WHILE; CLOSE cur1; end $$ delimiter ; LOOP.....END LOOP delimiter $$ drop procedure if exists `test` $$ create procedure test() begin DECLARE tmpflat INT DEFAULT 0; DECLARE tmpId int(4); DECLARE tmpTitle varchar(80); DECLARE cur1 CURSOR FOR select id,title from web_article order by id desc limit 10; DECLARE CONTINUE HANDLER FOR NOT FOUND SET tmpflat = 1; OPEN cur1; FETCH cur1 INTO tmpId,tmpTitle; loop_label:LOOP select tmpId,tmpTitle; FETCH cur1 INTO tmpId,tmpTitle; if(tmpflat=1)then LEAVE loop_label; end if; END LOOP; CLOSE cur1; end $$ delimiter ; REPEAT ....END REPEAT delimiter $$ drop procedure if exists `test` $$ create procedure test() begin DECLARE tmpflat INT DEFAULT 0; DECLARE tmpId int(4); DECLARE tmpTitle varchar(80); DECLARE cur1 CURSOR FOR select id,title from web_article order by id desc limit 3; DECLARE CONTINUE HANDLER FOR NOT FOUND SET tmpflat = 1; OPEN cur1; FETCH cur1 INTO tmpId,tmpTitle; REPEAT select tmpId,tmpTitle; FETCH cur1 INTO tmpId,tmpTitle; UNTIL tmpflat=1 END REPEAT; CLOSE cur1; end $$ delimiter ; IF NOT _done THEN #IF total > 0 THEN SELECT ordUID into uid FROM fightorder WHERE fightorder.alipayNo=parentId limit 1;#查询用户UID #INSERT INTO test values(total,uid); UPDATE sysusers SET integral=integral+total WHERE sysusers.UID=uid;#新增积分 #END IF; END IF; CREATE PROCEDURE myProc(customer_status VARCHAR(10)) -> BEGIN //创建mysql存储过程 -> -> CASE customer_status //case when语句用法 -> WHEN 'PLATINUM' THEN -> select 'PLATINUM'; -> -> WHEN 'GOLD' THEN -> select 'GOLD'; -> -> WHEN 'SILVER' THEN -> select 'SILVER'; -> -> WHEN 'BRONZE' THEN -> select 'BRONZE'; -> END CASE; -> END$$ 存储过程和函数的区别: 名称不同 :存储过程:procedure 函数function 存储过程没有返回值。
实例二
一个简单存储过程游标实例
DELIMITER $$
DROP PROCEDURE IF EXISTS getUserInfo $$
CREATE PROCEDURE getUserInfo(in date_day datetime)
--
-- 实例
-- 存储过程名为:getUserInfo
-- 参数为:date_day日期格式:2008-03-08
--
BEGIN
declare _userName varchar(12); -- 用户名
declare _chinese int ; -- 语文
declare _math int ; -- 数学
declare done int;
-- 定义游标
DECLARE rs_cursor CURSOR FOR SELECT username,chinese,math from userInfo where datediff(createDate, date_day)=0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
-- 获取昨天的日期
if date_day is null then
set date_day = date_add(now(),interval -1 day);
end if;
open rs_cursor;
cursor_loop:loop
FETCH rs_cursor into _userName, _chinese, _math; -- 取数据

if done=1 then
leave cursor_loop;
end if;
-- 更新表
update infoSum set total=_chinese+_math where UserName=_userName;
end loop cursor_loop;
close rs_cursor; END$$
DELIMITER ;
实例三
存储过程游标循环跳出现
在MySQL的存储过程中,游标操作时,需要执行一个conitnue的操作.众所周知,MySQL中的游标循环操作常用的有三种,LOOP,REPEAT,WHILE.三种循环,方式大同小异.以前从没用过,所以记下来,方便以后查阅.
1.REPEAT
REPEAT
Statements;
UNTIL expression
END REPEAT
demo
DECLARE num INT;
DECLARE my_string VARCHAR(255);
REPEAT
SET my_string =CONCAT(my_string,num,',');
SET num = num +1;
UNTIL num <5
END REPEAT;
2.WHILE
WHILE expression DO
Statements;
END WHILE
demo
DECLARE num INT;
DECLARE my_string VARCHAR(255);
SET num =1;
SET str ='';
WHILE num < span>10DO
SET my_string =CONCAT(my_string,num,',');
SET num = num +1;
END WHILE;
3.LOOP(这里面有非常重要的ITERATE,LEAVE)
代码如下 复制代码
DECLARE num INT;
DECLARE str VARCHAR(255);
SET num =1;
SET my_string ='';
loop_label: LOOP
IF num <10THEN
LEAVE loop_label;
ENDIF;
SET num = num +1;
IF(num mod3)THEN
ITERATE loop_label;
ELSE
SET my_string =CONCAT(my_string,num,',');
ENDIF;
END LOOP;
PS:可以这样理解ITERATE就是我们程序中常用的contiune,而ITERATE就是break.当然在MySQL存储过程,需要循环结构有个名称,其他都是一样的.