mysql 操作同样有循环语句操作,网上说有3中标准的循环方式: while 循环 、 loop 循环和repeat循环。还有一种非标准的循环: goto。 鉴于goto 语句的跳跃性会造成使用的的思维混乱,所以不建议使用。
这几个循环语句的格式如下:
WHILE……DO……END WHILE
REPEAT……UNTIL END REPEAT
LOOP……END LOOP
GOTO。
目前我只测试了 while 循环:
?
| 12345678910111213141516 | delimiter $$    // 定义结束符为 $$ dropprocedureif exists wk; // 删除 已有的 存储过程 createprocedurewk()      //  创建新的存储过程 begindeclarei int;          // 变量声明 seti = 1;      while i < 11 do           // 循环体 insertintouser_profile (uid) values(i); seti = i +1; endwhile; end$$               // 结束定义语句 // 调用 delimiter ;          // 先把结束符 回复为; call wk(); | 
delimter : mysql 默认的 delimiter是; 告诉mysql解释器,该段命令是否已经结束了,mysql是否可以执行了。
这里使用 delimiter 重定义结束符的作用是: 不让存储过程中的语句在定义的时候输出。
创建 MySQL 存储过程的简单语法为:
?
| 1234567 | CREATEPROCEDURE存储过程名称( [in| out| inout] 参数 ) BEGINMysql 语句 END | 
调用存储过程:
?
| 1 | call 存储过程名称() // 名称后面要加() | 
?
| 1 | <spanstyle="color: rgb(57, 57, 57); font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; background-color: rgb(250, 247, 239);">二 、 REPEAT 循环</span> | 
?
| 12345678910111213141516171819 | <pre name="code"class="html">delimiter // dropprocedureif exists looppc; createprocedurelooppc() begindeclarei int; seti = 1; repeat insertintouser_profile_company (uid) values(i+1); seti = i + 1; until i >= 20 endrepeat; end// ---- 调用 call looppc() | 
三、 LOOP 循环
?
| 123456789101112131415 | delimiter $$ dropprocedureif exists lopp; createprocedurelopp() begindeclarei int; seti = 1; lp1 : LOOP              // lp1 为循环体名称 LOOP 为关键字insertintouser_profile (uid) values(i); seti = i+1; if i > 30 thenleave lp1;              // 离开循环体 endif; endLOOP;              // 结束循环 end$$ | 
点击查看更多内容
					为 TA 点赞
										
				 评论
				共同学习,写下你的评论
评论加载中...
作者其他优质文章
					正在加载中
				
			感谢您的支持,我会继续努力的~
		扫码打赏,你说多少就多少
		赞赏金额会直接到老师账户
		支付方式
		打开微信扫一扫,即可进行扫码打赏哦
	 
                 
             
			 
					