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

  • 相关阅读:
    JS中数组的排序
    JS中输入身份证号码,subString截取出生日,并判断性别
    JS中for循环输出三角形
    JS中for循环实现s=x^y。
    JS中用for实现n的阶乘
    JS实现:for循环输出1000以内水仙花数
    JS中用if..else 查询成绩
    JS——do...while循环输出斐波拉契数前20项
    JS中while循环 ,二分法,产生随机数,计算机猜几次能猜中
    2018年5月9日JAVA-servlet01
  • 原文地址:https://www.cnblogs.com/super-yu/p/9018512.html
Copyright © 2011-2022 走看看