HANDLER FOR NOT FOUND 如何工作以及它的用途是什么? [英] How does HANDLER FOR NOT FOUND work and what is its usage?

查看:66
本文介绍了HANDLER FOR NOT FOUND 如何工作以及它的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对 MySQL 还是有点陌生​​.我想知道什么是 HANDLER FOR NOT FOUND,它是如何工作的,最重要的是:它的用途是什么?

Still kinda new to MySQL. I was wondering what is HANDLER FOR NOT FOUND, how does it work and most importantly: what is its usage?

推荐答案

HANDLER 用于捕获异常.

从游标读取时,越过游标的末尾会抛出一个NOT FOUND异常,而不是返回一个无穷无尽的NULL流,所以你必须抓住这个例外.

When reading from a cursor, reading past the end of the cursor throws a NOT FOUND exception, rather than returning an endless stream of NULL, so you have to catch this exception.

DECLARE val1 INT DEFAULT NULL;
DECLARE done TINYINT DEFAULT FALSE;

DECLARE c1 CURSOR FOR SELECT id FROM t1;

-- when the NOT FOUND condition fires, "done" -- which defaults to FALSE -- will be set to true,
-- and since this is a CONTINUE handler, execution continues with the next statement.   

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN c1;

my_loop: 
LOOP

  FETCH NEXT FROM c1 INTO val1; 
  IF done THEN -- this will be true when we are out of rows to read, so we go to the statement after END LOOP.
    LEAVE my_loop; 
  ELSE
    -- maybe do more stuff here
  END IF;
END LOOP;

-- procedure continues here...

部分复制自我的示例此处.

这篇关于HANDLER FOR NOT FOUND 如何工作以及它的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆