• MySQL循环语句之while循环测试


    转自:http://www.nuoweb.com/database/7614.html
    MySQL有循环语句操作,while 循环、loop循环和repeat循环,目前我只测试了 while 循环,下面与大家分享下

    mysql 操作同样有循环语句操作,网上说有3中标准的循环方式: while 循环 、 loop 循环和repeat循环。还有一种非标准的循环: goto。 鉴于goto 语句的跳跃性会造成使用的的思维混乱,所以不建议使用。

    这几个循环语句的格式如下:
    WHILE……DO……END WHILE
    REPEAT……UNTIL END REPEAT
    LOOP……END LOOP
    GOTO。

    目前我只测试了 while 循环:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    delimiter $$    // 定义结束符为 $$
    drop procedure if exists wk; // 删除 已有的 存储过程
    create procedure wk()      //  创建新的存储过程
    begin
    declare i int;          // 变量声明
    set i = 1;     
    while i < 11 do           // 循环体
    insert into user_profile (uid) values (i);
    set i = i +1;
    end while;
    end $$               // 结束定义语句
     
    // 调用
     
    delimiter ;          // 先把结束符 回复为;
    call wk();

    delimter : mysql 默认的 delimiter是; 告诉mysql解释器,该段命令是否已经结束了,mysql是否可以执行了。

    这里使用 delimiter 重定义结束符的作用是: 不让存储过程中的语句在定义的时候输出。

    创建 MySQL 存储过程的简单语法为:

    1
    2
    3
    4
    5
    6
    7
    CREATE PROCEDURE 存储过程名称( [in | out | inout] 参数 )
     
    BEGIN
     
    Mysql 语句
     
    END

    调用存储过程:

    1
    call 存储过程名称() // 名称后面要加()
    1
    <span style="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>
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <pre name="code" class="html">delimiter //
    drop procedure if exists looppc;
    create procedure looppc()
    begin
    declare i int;
    set i = 1;
     
    repeat
    insert into user_profile_company (uid) values (i+1);
    set i = i + 1;
    until i >= 20
     
    end repeat;
     
     
    end //
     
    ---- 调用
    call looppc()

    三、 LOOP 循环

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    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 $$
  • 相关阅读:
    ES7/ES8 语法学习
    JavaScript中this对象原理简洁说明
    浅谈http协议
    各个浏览器之间常见的兼容性问题
    npm -v 报错:cannot find module 'core-util-is'
    对象遍历的几种方法
    Vue项目如何关闭Eslint检测
    axios 基本运用
    如何去掉vue路由中的#
    vue 父子组件、兄弟组件传值
  • 原文地址:https://www.cnblogs.com/sharpest/p/7770247.html
Copyright © 2020-2023  润新知