且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

mysql存储过程入门

更新时间:2022-04-28 19:00:04

in类型的参数:

mysql> delimiter //

mysql> create procedure deom_in(in p_in int)

    -> begin

    -> select p_in;

    -> set p_in=3;

    -> select p_in;

    -> end;

    -> //

Query OK, 0 rows affected

 

mysql> set @p_in=1//

Query OK, 0 rows affected

mysql> call deom_in(@p_in)//

+------+

| p_in |

+------+

|    1 |

+------+

1 row in set

 

+------+

| p_in |

+------+

|    3 |

+------+

1 row in set

 

Query OK, 0 rows affected

 

传入参数未受影响

mysql> select @p_in//

+-------+

| @p_in |

+-------+

| 1     |

+-------+

1 row in set

 

 

 

 

 

 

 

out类型的参数:

mysql> create procedure demo_out(out p_out int)

    -> begin

    -> select p_out;

    -> set p_out=3;

    -> select p_out;

    -> end;

    -> //

Query OK, 0 rows affected

 

 

 

mysql> set @p_out=1//

Query OK, 0 rows affected

 

mysql> call demo_out(@p_out)//

+-------+

| p_out |

+-------+

| NULL  |

+-------+

1 row in set

 

+-------+

| p_out |

+-------+

|     3 |

+-------+

1 row in set

 

Query OK, 0 rows affected

 

传入参数被改变

mysql> select @p_out//

+--------+

| @p_out |

+--------+

| 3      |

+--------+

1 row in set

 

 

inout类型的参数:

 

mysql> create procedure demo_inout(inout p_inout int)

    -> begin

    -> select p_inout;

    -> set p_inout=4;

    -> select p_inout;

    -> end;

    -> //

Query OK, 0 rows affected

 

mysql> set @p_inout=1//

Query OK, 0 rows affected

 

mysql> call demo_inout(@p_inout)//

+---------+

| p_inout |

+---------+

|       1 |

+---------+

1 row in set

 

+---------+

| p_inout |

+---------+

|       4 |

+---------+

1 row in set

 

Query OK, 0 rows affected

 

传入参数被改变

mysql> select @p_inout//

+----------+

| @p_inout |

+----------+

| 4        |

+----------+

1 row in set

 

 

查看存储过程:

mysql> show procedure status where db='test';//


 mysql存储过程入门

 

删除存储过程:

mysql> drop procedure pro//

Query OK, 0 rows affected

mysql存储过程入门

注意:在图形化工具的命令模式下,写存储过程不需要delimiter //

他会自动识别。用”  ;  ”就行了。否则会报错。

 

mysql存储过程入门



mysql存储过程入门

If else:

 

mysql存储过程入门



mysql存储过程入门

其中:结构

 

Begin

If ... Then

 

Elseif ...then

 

Else ...

 

End if

 

 

End

 

 


Case:


mysql存储过程入门




mysql存储过程入门

While  do


mysql存储过程入门





mysql存储过程入门


repeat···· end repeat

它在执行操作后检查结果,而while则是执行前进行检查。


相当于do while   先执行再判断

 

mysql存储过程入门


mysql存储过程入门

如果输入一个小于7的数:

mysql存储过程入门