mysql procedure存储过程循环,条件判断范例

WBOY
풀어 주다: 2016-06-07 16:24:07
원래의
1110명이 탐색했습니다.

mysql procedure存储过程循环,条件判断实例 本文转载自:???? http://www.111cn.net/database/mysql/38878.htm ? ? mysql教程 procedure存储过程循环,条件判断实例 ? mysql delimiter $$ mysql CREATE PROCEDURE myProc() ??? - DETERMINISTIC ??? - BEGIN ?

mysql procedure存储过程循环,条件判断实例

本文转载自:???? http://www.111cn.net/database/mysql/38878.htm

?

?

mysql教程 procedure存储过程循环,条件判断实例

?

mysql> delimiter $$
mysql> CREATE PROCEDURE myProc()
??? -> DETERMINISTIC
??? -> BEGIN
??? ->?? DECLARE counter INT DEFAULT 0;
??? ->
??? ->?? simple_loop: LOOP
??? ->???? SET counter=counter+1;
??? ->???? select counter;
??? ->???? IF counter=10 THEN
??? ->??????? LEAVE simple_loop;
??? ->???? END IF;
??? ->?? END LOOP simple_loop;
??? ->?? SELECT 'I can count to 10';
??? -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql>
mysql> call myProc();
+---------+
| counter |
+---------+
|?????? 1 |
+---------+
1 row in set (0.00 sec)

+---------+
| counter |
+---------+
|?????? 2 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|?????? 3 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|?????? 4 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|?????? 5 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|?????? 6 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|?????? 7 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|?????? 8 |
+---------+
1 row in set (0.02 sec)

+---------+
| counter |
+---------+
|?????? 9 |
+---------+
1 row in set (0.33 sec)

+---------+
| counter |
+---------+
|????? 10 |
+---------+
1 row in set (0.33 sec)

+-------------------+
| I can count to 10 |
+-------------------+
| I can count to 10 |
+-------------------+
1 row in set (0.33 sec)

Query OK, 0 rows affected (0.33 sec)


实例二

mysql> CREATE PROCEDURE myProc()
??? -> BEGIN
??? ->???? DECLARE i int;
??? ->???? SET i=1;
??? ->???? myloop: LOOP
??? ->????????? SET i=i+1;
??? ->????????? IF i=10 THEN
??? ->?????????????????? LEAVE myloop;
??? ->????????? END IF;
??? ->???? END LOOP myloop;
??? ->???? SELECT 'I can count to 10';
??? ->
??? -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql>
mysql> call myProc();
+-------------------+
| I can count to 10 |
+-------------------+
| I can count to 10 |
+-------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec

带有条件

mysql> create procedure increment (IN in_count INT)
??? -> BEGIN
??? -> declare count INT default 0;
??? ->
??? ->???? increment: loop
??? ->???????? set count = count + 1;
??? ->???????? if count ??? ->???????????? iterate increment;
??? ->???????? end if;
??? ->???????? if count > in_count then
??? ->???????????? leave increment;
??? ->???????? end if;
??? ->???? end loop increment;
??? ->???? select count;
??? -> END
??? -> //
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql> call increment(3);
+-------+
| count |
+-------+
|??? 20 |
+-------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

?

?

?

?

?

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿