如何在MySQL存储函数中使用goto标签 [英] How to use goto label in MySQL stored function

查看:1069
本文介绍了如何在MySQL存储函数中使用goto标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MySQL存储函数中使用goto. 我该如何使用? 示例代码为:

I would like to use goto in MySQL stored function. How can I use? Sample code is:

if (action = 'D') then
    if (rowcount > 0) then
        DELETE FROM datatable WHERE id = 2;      
    else
       SET p=CONCAT('Can not delete',@b);
       goto ret_label;
    end if;
end if;

Label: ret_label;
return 0;

推荐答案

有一些无法在MySQL中实现的GOTO情况,例如向后跳转代码(也是一件好事).

There are GOTO cases which can't be implemented in MySQL, like jumping backwards in code (and a good thing, too).

但是对于您想要从所有内容跳至最后一系列语句的示例而言,您可以在代码周围创建一个BEGIN/END块以跳出以下代码:

But for something like your example where you want to jump out of everything to a final series of statements, you can create a BEGIN / END block surrounding the code to jump out of:

aBlock:BEGIN
    if (action = 'D') then
        if (rowcount > 0) then
            DELETE FROM datatable WHERE id = 2;      
        else
           SET p=CONCAT('Can not delete',@b);
           LEAVE aBlock;
        end if;
    end if;
END aBlock;
return 0;

由于您的代码只是一些嵌套的IF,因此在给定的代码中不需要构造.但是,对于LOOP/WHILE/REPEAT来说,避免在循环内避免多个RETURN语句并合并最终处理(类似于TRY/FINALLY)更有意义.

Since your code is just some nested IFs, the construct is unnecessary in the given code. But it makes more sense for LOOP/WHILE/REPEAT to avoid multiple RETURN statements from inside a loop and to consolidate final processing (a little like TRY / FINALLY).

这篇关于如何在MySQL存储函数中使用goto标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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