首页 > 数据库技术 > 详细

MySQL存储过程把执行结果保存在变量中

时间:2021-08-25 23:36:00      阅读:22      评论:0      收藏:0      [点我收藏+]

在MySQL存储过程中若需要把执行的结果保存在变量中,可以使用into关键字。但使用普通语句和预处理语句的保存方式不一样。

1)普通语句

create procedure proc_var02()
begin
    declare create_time datetime;
    select now() into create_time;
    select create_time;
end;

普通的语句使用这种方式是没有问题的,可以直接赋值成功。

2)预处理语句

错误写法:

create procedure proc_var02()
begin
    declare create_time datetime;
    set @stmt = concat(select now() into, create_time);
        prepare stmt from @stmt;
        execute stmt;
        deallocate prepare stmt;
    select create_time;
end;

创建后调用此存储过程,会出现错误

call proc_var02()
> 1064 - 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 ‘NULL‘ at line 1
> 时间: 0s

 

正确写法:

create procedure proc_var02()
begin
    set @stmt = concat(select now() into @create_time);
        prepare stmt from @stmt;
        execute stmt;
        deallocate prepare stmt;
    select @create_time;
end;

需使用用户变量进行接收,不能使用局部变量。

MySQL存储过程把执行结果保存在变量中

原文:https://www.cnblogs.com/zys2019/p/15171766.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!