zoukankan      html  css  js  c++  java
  • Mysql储存过程6: in / out / inout

    in 为向函数传送进去的值

    out 为函数向外返回的值

    intout 传送进去的值, 并且还返回这个值

        create procedure q1(in number int,out name varchar(100))
          begin
            if number > 1 then
            select 'true';
            else
            select 'false';
            end if;
          end$

    调用时:

    call q1(1, @value);

    注意, 第二个参数要为变量定义的型式。

    这个函数并没有向外发送改变后的name值, 所以调用后 select @value 为null。

    再看看out:

    mysql>     create procedure qq(number int,inout name varchar(100))
        ->       begin
        ->         if number > 1 then
        ->         select 'true';
        ->         else
        ->         select 'false';
        ->         end if;
        ->         set name=user();
        ->       end$
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> call qq(1,@as)$
    +-------+
    | false |
    +-------+
    | false |
    +-------+
    1 row in set (0.00 sec)
    
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> select @as$
    +----------------+
    | @as            |
    +----------------+
    | root@localhost |
    +----------------+
    1 row in set (0.00 sec)
    
    mysql>

    inout例子:

    mysql> create procedure qqq(inout name varchar(100))
        ->       begin
        ->         set name=database();
        ->       end$
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> call qqq(1)$
    ERROR 1414 (42000): OUT or INOUT argument 1 for routine test.qqq is not a variable or NEW pseudo-var
    iable in BEFORE trigger
    mysql> call qqq(@abc)$
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select @abc$
    +------+
    | @abc |
    +------+
    | test |
    +------+
    1 row in set (0.00 sec)
    
    mysql>

    注意参数型式, 因为他要发送回来, 这个inout的参数型式要跟out类似, 也就是要变量定义型式: @变量名。

  • 相关阅读:
    88. Merge Sorted Array
    87. Scramble String
    86. Partition List
    85. Maximal Rectangle
    84. Largest Rectangle in Histogram
    83. Remove Duplicates from Sorted List
    82. Remove Duplicates from Sorted List II
    81. Search in Rotated Sorted Array II
    80. Remove Duplicates from Sorted Array II
    计算几何——点线关系(叉积)poj2318
  • 原文地址:https://www.cnblogs.com/perl6/p/7114726.html
Copyright © 2011-2022 走看看