欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MYSQL简单控制流程

程序员文章站 2022-05-31 20:15:00
...

DELIMITER CREATEPROCEDUREproc(iINT)BEGINDECLAREjINTDEFAULT100;IFi=10THENSELECTj;SELECT10;ELSESETj=i+i;SELECTj;SELECT10;ENDIF;END CREATE PROCEDURE proc(i INT) BEGIN DECLARE j INT DEFAULT 100 ; IF i=10 THEN SELECT j ; SELECT '等于10' ; ELSE SET j = i + i ; SELECT j ; SELECT '不等于10' ; END IF ; END

CALL proc(11)

DROP PROCEDURE IF EXISTS proc

– 等额判断
DELIMITER CREATEPROCEDUREproc(wINT)BEGINCASEwWHEN1THENSELECT;WHEN2THENSELECT;WHEN3THENSELECT;WHEN4THENSELECT;WHEN5THENSELECT;WHEN6THENSELECT;WHEN7THENSELECT;ELSESELECT;ENDCASE;END CREATE PROCEDURE proc( w INT) BEGIN CASE w WHEN 1 THEN SELECT '星期一' ; WHEN 2 THEN SELECT '星期二' ; WHEN 3 THEN SELECT '星期三' ; WHEN 4 THEN SELECT '星期四' ; WHEN 5 THEN SELECT '星期五' ; WHEN 6 THEN SELECT '星期六' ; WHEN 7 THEN SELECT '星期七' ; ELSE SELECT '输入有误' ; END CASE ; END

CALL proc(10)

DROP PROCEDURE IF EXISTS proc
– 范围判断
DELIMITER CREATEPROCEDUREproc(scoreINT)BEGINCASEWHENscore>90THENSELECT;WHENscore>60ANDscore<=90THENSELECT;ELSESELECT;ENDCASE;END CREATE PROCEDURE proc( score INT) BEGIN CASE WHEN score>90 THEN SELECT '优' ; WHEN score>60 AND score<=90 THEN SELECT '及格' ; ELSE SELECT '不及格' ; END CASE ; END

CALL proc(80)

DROP PROCEDURE IF EXISTS proc
– 实现循环累加等功能

DELIMITER $$
CREATE PROCEDURE proc( m INT )
BEGIN
DECLARE i INT DEFAULT 0 ;
DECLARE result INT DEFAULT 0 ;

WHILE  i < m  DO
	SET result = result + i ;
	SET i = i + 1 ;
END WHILE ;
SELECT result ;

END $$

CALL proc(100)

DROP PROCEDURE IF EXISTS proc
DELIMITER $$
CREATE PROCEDURE proc( m INT)
BEGIN
DECLARE result INT DEFAULT 0 ;
DECLARE i INT DEFAULT 1 ;
calc : LOOP
SET result = result + i ;
SET i = i + 1 ;
IF i > m THEN
LEAVE calc ;
END IF ;
END LOOP ;
SELECT result ;
END

CALL proc(10)

DROP PROCEDURE IF EXISTS proc
– repeat until
DELIMITER CREATEPROCEDUREproc(mINT)BEGINDECLAREiINTDEFAULT0;DECLAREresultINTDEFAULT0;REPEATSETresult=result+i;SETi=i+1;UNTILi&gt;mENDREPEAT;SELECTresult;END CREATE PROCEDURE proc(m INT) BEGIN DECLARE i INT DEFAULT 0 ; DECLARE result INT DEFAULT 0 ; REPEAT SET result = result + i ; SET i = i + 1 ; UNTIL i &gt; m END REPEAT ; SELECT result ; END

CALL proc(10)