数据库教程:Mysql中调试存储过程最简单的方法

以前同事告诉我用临时表插入变量数据来查看,但是这种方法过于麻烦,而且mysql没有比较好的调试存储过程的工具。今天google了下发现可以用select + 变量名的方法来调试具体方法:在你的存储过程

以前同事告诉我用临时表插入变量数据来查看,但是这种方法过于麻烦,而且mysql没有比较好的调试存储过程的工具。今天google了下发现可以用select + 变量名的方法来调试

具体方法:

在你的存储过程中加入如下语句:
select 变量1,变量2;

然后用mysql自带的cmd程序进入mysql> 下。
call 你的存储过程名(输入参数1,@输出参数);(注:这里帮助下新同学,如果你的存储过程有输出变量,那么在这里只需要加 @ 然后跟任意变量名即可);
即可发现你的变量值被打印到了cmd下,简单吧?呵呵 希望能帮到诸位。

有如下一个存储过程

  create procedure `p_next_id`(kind_name varchar(30), i_length int,currentseqno varchar(3),out o_result int)  begin        set @a= null;       set @b= null;       select id into @a from t_seq where number= currentseqno and length= i_length ;          if (@a is null ) then              select min(id) into @a from t_seq where length = i_length;              select number  into @b from t_seq where id = @a;else          select number  into @b from t_seq where id = @a+1;               end if;               select @b into o_result;       end

在navicat中调用存储过程 

写语句调用
call p_next_id(‘t_factory’,2,’0′,@result); — 上面的存储过程含有四个参数,所以这里调用的时候,也需要传递4个参数:输入参数填写值,输出参数用变量表示@result
select @result; — 这句话是在控制台显示变量值
2. 窗口点击

直接点击运行时,在弹出输入框输入:’t_factory’,2,’0′,@result

追踪存储过程执行步骤

mysql不像oracle有plsqldevelper工具用来调试存储过程,所以有两简单的方式追踪执行过程:

用一张临时表,记录调试过程
直接在存储过程中,增加select @xxx,在控制台查看结果:
例如我把上面的存储过程中加一些查询语句(注意下面的红色语句)

  create procedure `p_next_id`(kind_name varchar(30), i_length int,currentseqno varchar(3),out o_result int)  begin        set @a= null;       set @b= null;       select id into @a from t_seq where number= currentseqno and length= i_length ;   select @a;            if (@a is null ) then              select min(id) into @a from t_seq where length = i_length;              select number  into @b from t_seq where id = @a;       select @b;       else          select number  into @b from t_seq where id = @a+1;               end if;               select @b into o_result;       end

到此这篇关于mysql中调试存储过程最简单的方法的文章就介绍到这了,更多相关mysql调试存储过程内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多数据库技术:Mysql中调试存储过程最简单的方法,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/807833.html

(0)
上一篇 2021年9月10日
下一篇 2021年9月10日

精彩推荐