时间:2021-07-01 10:21:17 帮助过:64人阅读
用来声明一个数据集
游标的声明必须在变量和条件声明之后,在handler声明之前
游标特性:
实例:
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT FALSE;
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 NOT FOUND SET done= TRUE;
OPEN cur1;
OPEN cur2;read_loop:LOOP
FETCH cur1 INTO a,b;
FETCH cur2 INTO c;
IF done THEN
LEAVE read_loop;
END IF;
IF b<c THEN
INSERT INTO test.t3 VALUES (a,b);
ELSE
INSERT INTO test.t3 VALUES (a,c);
END IF;
END LOOP;
CLOSE cur1;
CLOSE cur2;
END;
说明:
cursor declare用来声明一个游标和指定游标对应的数据集合,通常数据集合是一个select语句。
DECLARE cursor_name CURSOR FOR select_statement
cursor fetch用来获取游标指定数据集的下一行数据并将各个字段值赋予后面的变量
FETCH [[NEXT] FROM] cursor_name INTO var_name [, var_name] ...
命名特定的错误条件,而该特定错误可以在declare...handler中指定处理方法
语法:
DECLARE condition_name CONDITION FOR condition_value
condition_value:mysql_error_code
| SQLSTATE [VALUE] sqlstate_value
说明:
condition_value指定特定的错误条件,有以下两种形式:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘v1 int default 5;
DECLARE CONTINUE HANDLER FOR 1051
BEGIN
-- body of handler
END;
declare no_such_table condition for 1051;
declare continue handler for no_such_table
begin
--body of handler
end;
declare no_such_table condition for sqlstate ‘42S02‘
declare continue handler for no_such_table
begin
--body of handler
end;
语法:
DECLARE handler_action HANDLER
FOR condition_value [, condition_value] ...
statement
handler_action:CONTINUE
| EXIT
| UNDO
condition_value:mysql_error_code
| SQLSTATE [VALUE] sqlstate_value
| condition_name
| SQLWARNING
| NOT FOUND
| SQLEXCEPTION
handler_action声明当执行完SQL语句之后应该做什么:
condition_value的值有以下几种:
实例:
DECLARE CONTINUE HANDLER FOR 1051
BEGIN
-- body of handler
END;
DECLARE CONTINUE HANDLER FOR SQLSTATE ‘42S02‘
BEGIN
-- body of handler
END;
DECLARE CONTINUE HANDLER FOR SQLWARNING
BEGIN
-- body of handler
END;
DECLARE CONTINUE HANDLER FOR NOT FOUND
BEGIN
-- body of handler
END;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
-- body of handler
END;
当conditon发生但没有声明handler时,则存储过程和函数依照如下规则处理:
实例:
mysql> CREATE TABLE test.t(s1 INT, PRIMARY KEY (s1));
Query OK, 0 rows affected (0.35 sec)
SQLSTATE ‘23000‘表示主键冲突
mysql> delimiter //
mysql> CREATE PROCEDURE handlerdemo()
-> BEGIN
-> DECLARE CONTINUE HANDLER FOR SQLSTATE ‘23000‘ SET @x2 = 1;
-> 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.03 sec)
mysql> delimiter ;
mysql> select @x;
+------+
| @x |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
实例:
mysql> delimiter //
mysql> create procedure curdemo()
-> begin
-> declare done int default false;
-> declare a char(16);
-> declare b,c int;
-> declare cur1 cursor for select id,data from t1;
-> declare cur2 cursor for select i from test.t2;
-> declare continue handler for not found set done = true;
-> open cur1;
-> open cur2;
-> read_loop:loop
-> fetch cur1 into a,b;
-> fetch cur2 into c;
-> if done then
-> leave read_loop;
-> end if;
-> if b<c then
-> insert into test.t3 values(a,b);
-> else
-> insert into test.t3 values(a,c);
-> end if;
-> end loop;
-> close cur1;
-> close cur2;
-> end//
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
MySQL-5.7 游标及DECLARE
标签:错误 leave next tab statement creat 执行 ... 使用