zoukankan      html  css  js  c++  java
  • MySQL存储过程把执行结果保存在变量中

    在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;

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

    就是这么简单,你学废了吗?感觉有用的话,给笔者点个赞吧 !
  • 相关阅读:
    左耳听风-ARTS-第4周(2019/4/21-2019/4/27)
    Java集合总结
    Zuul网关总结
    左耳听风-ARTS-第3周(2019/4/7-2019/4/13)
    左耳听风-ARTS-第2周(2019/3/31-2019/4/6)
    Java泛型相关总结(下)
    左耳听风-ARTS-第1周
    去长江边走走,看看
    第1记
    c#发送邮件
  • 原文地址:https://www.cnblogs.com/zys2019/p/15171766.html
Copyright © 2011-2022 走看看