当前位置:Gxlcms > mysql > MySql官方手册学习笔记3—MySql中的存储过程简介_MySQL

MySql官方手册学习笔记3—MySql中的存储过程简介_MySQL

时间:2021-07-01 10:21:17 帮助过:24人阅读

bitsCN.com

SELECT INTO

CREATE PROCEDURE sp1 (x VARCHAR(5)) //创建储存过程

BEGIN

DECLARE xname VARCHAR(5) DEFAULT 'bob'; //声明局部变量

DECLARE newname VARCHAR(5);

DECLARE xid INT;

SELECT xname,id INTO newname,xid //将查到的属性值存入变量中

FROM table1 WHERE xname = xname;

SELECT newname;

END;

变量名不能与列名一样,当这个程序被调用的时候,无论table.xname列的值是什么,变量newname将返回值‘bob’。

HANDLER

mysql> CREATE TABLE test.t (s1 int,primary key (s1));

Query OK, 0 rows affected (0.00 sec)

mysql> delimiter // //将定界符“;”改为“//”,因为程序中需要用到“;”。

mysql> CREATE PROCEDURE handlerdemo ()

-> BEGIN

-> DECLARE insertErr CONDITION FOR SQLSTATE '23000'; //声明CONDITION

-> DECLARE CONTINUE HANDLER FOR insertErr SET @x2 = 1;//声明HANDLER

-> SET @x = 1; //为局部变量赋值

-> INSERT INTO test.t VALUES (1);

-> SET @x = 2;

-> INSERT INTO test.t VALUES (1);

-> SET @x = 3;

-> END;

-> //

Query OK, 0 rows affected (0.00 sec)

mysql> CALL handlerdemo()//

Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @x//

+------+

| @x |

+------+

| 3 |

+------+

1 row in set (0.00 sec)

注意到,@x是3,这表明MySQL被执行到程序的末尾。如果DECLARE CONTINUE HANDLER FOR insertErr SET @x2 = 1; 这一行不在,第二个INSERT因PRIMARY KEY强制而失败之后,MySQL可能已经采取 默认(EXIT)路径,并且SELECT @x可能已经返回2。

CURSOR

CREATE PROCEDURE curdemo()

BEGIN

DECLARE done INT DEFAULT 0;

DECLARE a CHAR(16);

DECLARE b,c INT;

DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;

DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

OPEN cur1; //使用前先打开游标

OPEN cur2;

REPEAT

FETCH cur1 INTO a, b;

FETCH cur2 INTO c;

IF NOT done THEN

IF b < c THEN

INSERT INTO test.t3 VALUES (a,b);

ELSE

INSERT INTO test.t3 VALUES (a,c);

END IF;

END IF;

UNTIL done END REPEAT;

CLOSE cur1;

CLOSE cur2;

END

CASE

CASE case_value

WHEN when_value THEN statement_list

[WHEN when_value THEN statement_list] ...

[ELSE statement_list]

END CASE

Or:

CASE

WHEN search_condition THEN statement_list

[WHEN search_condition THEN statement_list] ...

[ELSE statement_list]

END CASE

存储程序的CASE语句实现一个复杂的条件构造。如果search_condition 求值为真,相应的SQL被执行。如果没有搜索条件匹配,在ELSE子句里的语句被执行。

LEAVE

LEAVE label

这个语句被用来退出任何被标注的流程控制构造。它和BEGIN ... END或循环一起被使用。

ITERATE

ITERATE label

ITERATE只可以出现在LOOP, REPEAT, 和WHILE语句内。ITERATE意思为:“再次循环。”

例如:

CREATE PROCEDURE doiterate(p1 INT)

BEGIN

label1: LOOP

SET p1 = p1 + 1;

IF p1 < 10 THEN ITERATE label1; END IF;

LEAVE label1;

END LOOP label1;

SET @x = p1;

END

REPEAT

mysql> delimiter //

mysql> CREATE PROCEDURE dorepeat(p1 INT)

-> BEGIN

-> SET @x = 0;

-> REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;

-> END

-> //

Query OK, 0 rows affected (0.00 sec)

mysql> CALL dorepeat(1000)//

Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @x//

+------+

| @x |

+------+

| 1001 |

+------+

1 row in set (0.00 sec)

WHILE

CREATE PROCEDURE dowhile()

BEGIN

DECLARE v1 INT DEFAULT 5;

WHILE v1 > 0 DO

...

SET v1 = v1 - 1;

END WHILE;

EN

bitsCN.com

人气教程排行