...
DELIMITER CREATEPROCEDUREproc(iINT)BEGINDECLAREjINTDEFAULT100;IFi=10THENSELECTj;SELECT′等于10′;ELSESETj=i+i;SELECTj;SELECT′不等于10′;ENDIF;END
CALL proc(11)
DROP PROCEDURE IF EXISTS proc
– 等额判断
DELIMITER CREATEPROCEDUREproc(wINT)BEGINCASEwWHEN1THENSELECT′星期一′;WHEN2THENSELECT′星期二′;WHEN3THENSELECT′星期三′;WHEN4THENSELECT′星期四′;WHEN5THENSELECT′星期五′;WHEN6THENSELECT′星期六′;WHEN7THENSELECT′星期七′;ELSESELECT′输入有误′;ENDCASE;END
CALL proc(10)
DROP PROCEDURE IF EXISTS proc
– 范围判断
DELIMITER CREATEPROCEDUREproc(scoreINT)BEGINCASEWHENscore>90THENSELECT′优′;WHENscore>60ANDscore<=90THENSELECT′及格′;ELSESELECT′不及格′;ENDCASE;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>mENDREPEAT;SELECTresult;END
CALL proc(10)