zoukankan      html  css  js  c++  java
  • MySQL中的IN、OUT、INOUT类型

    MySQL中,存储过程的参数类型IN、OUT、INOUT,但是函数的参数只能是IN类型的。

    1、IN类型

    使用IN类型来传递信息,存储过程内部可以对参数的值进行修改,但是修改后的值调用者不可见。

    create procedure pr_demo_in(in id int)
    begin
        if (id is not null)then set id = id + 1;
        end if;
        select id as output_id;
    end
    
    /** 测试 **/
    set @id = 1;
    call pr_demo_in(@id);  /** 运行结果output_id = 2  **/
    select @id as output_id;  /** 运行结果output_id = 1  **/

    可以看出虽然设置了变量id的值为1,但是在存储过程内部修改了id的值为2,id的值并未返回给调用者。

    2、OUT类型

    使用OUT类型来传递信息,在存储过程内部,该值的默认值为NULL,无论调用者是否传值给存储过程。

    create procedure pr_demo_out(out id int)
    begin
        if (id is not null)then set id = id + 1;
        end if;
        select id as output_id;
    end
    
    /** 测试 **/
    set @id = 10;
    call pr_demo_out(@id); /** 运行结果output_id为null  **/
    select @id as output_id; /** 运行结果output_id为null **/

    可以看出虽然设置了变量id的值为10,但是在存储过程内部id的值为null,最后id的值在存储过程内修改后返回调用者。

    2、INOUT类型

    使用IN类型来传递信息,存储过程内部可以对参数的值进行修改,并将最终值返回给调用者。

    create procedure pr_demo_inout(inout id int)
    begin
        if (id is not null)then set id = id + 1;
        end if;
        select id as output_id;
    end
    
    /** 测试 **/
    set @id = 1;
    call pr_demo_inout(@id); /** 运行结果output_id = 2  **/
    select @id as output_id; /** 运行结果output_id = 2  **/

    以看出设置了变量id的值为2,在存储内部将id的值修改为2,最后id的值返回给调用者。

  • 相关阅读:
    ASP.NET 防盗链的实现[HttpHandler]
    html打印表格每页都有的表头和打印分页
    spring是怎样管理mybatis的及注入mybatis mapper bean的
    浅谈Log4j和Log4j2的区别
    git tag — 标签相关操作
    java cocurrent包
    线程实现异步
    使用Shell脚本查找程序对应的进程ID,并杀死进程
    shell脚本监测文件变化
    spring boot的几种配置类型
  • 原文地址:https://www.cnblogs.com/super-yu/p/9018512.html
Copyright © 2011-2022 走看看