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的值返回给调用者。

  • 相关阅读:
    P1144 最短路计数 题解 最短路应用题
    C++高精度加减乘除模板
    HDU3746 Teacher YYF 题解 KMP算法
    POJ3080 Blue Jeans 题解 KMP算法
    POJ2185 Milking Grid 题解 KMP算法
    POJ2752 Seek the Name, Seek the Fame 题解 KMP算法
    POJ2406 Power Strings 题解 KMP算法
    HDU2087 剪花布条 题解 KMP算法
    eclipse创建maven项目(详细)
    maven的作用及优势
  • 原文地址:https://www.cnblogs.com/super-yu/p/9018512.html
Copyright © 2011-2022 走看看