• MySQL中的流程控制


    使用 if、case、loop、leave、iterate、repeat、while  语句来控制流程。

    1、if 语句

                IF i_staff_id =2 THEN
                     SET @x1 = @x1 + d_amont;
                ELSE
                    set @x2 = @x2 +d_amount;
                end if;

    2、case 语句

    CASE 
        WHEN i_staff_id = 2 THEN
            set @x1 = @x1 + d_amount
        ELSE
            set @x2 = @x2 + d_amount
    END CASE;

    或者

    CASE i_staff_id 
        WHEN 2 THEN
            set @x1 = @x1 + d_amount
        ELSE
            set @x2 = @x2 + d_amount
    END CASE;

    3、loop 语句(通常和 leave 语句一起使用)

    4、leave 语句

    CREATE PROCEDURE actor_insert ( ) BEGIN
        
        SET @x = 0;
        ins :LOOP   
            SET @x = @x + 1
            IF
                @x = 100 THEN
            LEAVE ins;
                
            END IF;
            INSERT INTO actor ( first_name, last_name )
            VALUES( 'Test', '201' );
            
        END LOOP label;
    
    END;
    call actor_insert();
    select count(*) from actor where first_name='Test'

    5、iterate 语句

    CREATE PROCEDURE actor_insert() BEGIN
        
        SET @x = 0;
        ins :LOOP
                
            SET @x = @x + 1;
            IF @x = 10 THEN
            LEAVE ins;
                
            END IF;
            INSERT INTO actor ( actor_id, first_name, last_name )
            VALUES( @x + 200, 'Test', @x );
            
        END LOOP ins;
    
    END;
    call actor_insert();
    select actor_id,first_name,last_name from actor where first_name='Test';

    6、REPATE 语句

        REPEAT
        FETCH cur_payment INTO i_staff_id, d_amount;
          if i_staff_id =2 THEN
                    SET @x1 = @x1 + d_amont;
                ELSE
                    set @x2 = @x2 +d_amount;
                end if;
        
        UNTIL 0 END REPEAT;

    7、while 语句

    CREATE PROCEDURE loop_demo() BEGIN
        
        SET @x = 1,@x1 = 1;
        REPEAT
                
                SET @x = @x + 1;
        UNTIL @x > 0 END REPEAT;
        WHILE @x < 0 DO
                
                SET @x1 = @x1 + 1;
            
        END WHILE;
    
    END;
    call loop_demo();
  • 相关阅读:
    使用STM32驱动双通道12位DAC(TLV5618)
    CentOS 7挂载离线yum源
    有关于Git的使用的一点心得和说明
    STM32单片机学习心得——MDK使用技巧
    小米手机连接ADB
    我看操作系统的发展
    centos7下cups + samba共打印服务
    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤
    centos7 更新yum源
    CentOS7 安装Odoo9.0
  • 原文地址:https://www.cnblogs.com/kate7/p/13346442.html
Copyright © 2020-2023  润新知