以下的文章主要是向大家描述的是MySQL 存储过程的实际应用,我前两天在相关网站看见的资料,觉得实用性很大,今天拿出来以供大家分享。

我们大家都知道MySQL 存储过程是从 MySQL 5.0 开始逐渐增加新的功能。存储过程在实际应用中也是优点大于缺点。不过最主要的还是执行效率和SQL 代码封装。特别是 SQL 代码封装功能,如果没有存储过程。

MySQL 存储过程的基本用法(mysql存储过程使用)  存储过程 第1张

在外部程序访问数据库时(例如 PHP),要组织很多 SQL 语句。

特别是业务逻辑复杂的时候,一大堆的 SQL 和条件夹杂在 PHP 代码中,让人不寒而栗。现在有了 MySQL 存储过程,业务逻辑可以封装存储过程中,这样不仅容易维护,而且执行效率也高。

一、MySQL 创建存储过程

“pr_add” 是个简单的 MySQL 存储过程,这个MySQL 存储过程有两个 int 类型的输入参数 “a”、“b”,返回这两个参数的和。

  1. dropprocedureifexistspr_add;

计算两个数之和

  1. createprocedurepr_add
  2. (
  3. aint,
  4. bint
  5. )
  6. begin
  7. declarecint;
  8. ifaisnullthen
  9. seta=0;
  10. endif;
  11. ifbisnullthen
  12. setb=0;
  13. endif;
  14. setc=a+b;
  15. selectcassum;
  16. /*
  17. returnc;

不能在 MySQL 存储过程中使用。return 只能出现在函数中。

  1. */
  2. end;

二、调用 MySQL 存储过程

  1. callpr_add(10,20);

执行 MySQL 存储过程,存储过程参数为 MySQL 用户变量。

  1. set@a=10;
  2. set@b=20;
  3. callpr_add(@a,@b);

三、MySQL 存储过程特点

创建 MySQL 存储过程的简单语法为:

  1. createprocedure存储过程名字()
  2. (
  3. [in|out|inout]参数datatype
  4. )
  5. begin
  6. MySQL语句;
  7. end;

MySQL 存储过程参数如果不显式指定“in”、“out”、“inout”,则默认为“in”。习惯上,对于是“in” 的参数,我们都不会显式指定。

1. MySQL 存储过程名字后面的“()”是必须的,即使没有一个参数,也需要“()”

2. MySQL 存储过程参数,不能在参数名称前加“@”,如:“@a int”。下面的创建存储过程语法在 MySQL 中是错误的(在 SQL Server 中是正确的)。 MySQL 存储过程中的变量,不需要在变量名字前加“@”,虽然 MySQL 客户端用户变量要加个“@”。

  1. createprocedurepr_add
  2. (
  3. @aint,--错误
  4. bint--正确
  5. )

3. MySQL 存储过程的参数不能指定默认值。

4. MySQL 存储过程不需要在 procedure body 前面加 “as”。而 SQL Server 存储过程必须加 “as” 关键字。

  1. createprocedurepr_add
  2. (
  3. aint,
  4. bint
  5. )
  6. as--错误,MySQL不需要“as”
  7. begin
  8. mysqlstatement...;
  9. end;

5. 如果 MySQL 存储过程中包含多条 MySQL 语句,则需要 begin end 关键字。

  1. createprocedurepr_add
  2. (
  3. aint,
  4. bint
  5. )
  6. begin
  7. mysqlstatement1...;
  8. mysqlstatement2...;
  9. end;

6. MySQL 存储过程中的每条语句的末尾,都要加上分号 “;”

  1. ...
  2. declarecint;
  3. ifaisnullthen
  4. seta=0;
  5. endif;
  6. ...
  7. end;

7. MySQL 存储过程中的注释。

  1. createprocedurepr_add
  2. (
  3. aint,
  4. bint
  5. )
  6. begin
  7. declarecint;
  8. ifaisnullthen
  9. seta=0;
  10. endif;
  11. ifbisnullthen
  12. setb=0;
  13. endif;
  14. setc=a+b;
  15. selectcassum;
  16. /*
  17. returnc;
0

8. 不能在 MySQL 存储过程中使用 “return” 关键字。

  1. createprocedurepr_add
  2. (
  3. aint,
  4. bint
  5. )
  6. begin
  7. declarecint;
  8. ifaisnullthen
  9. seta=0;
  10. endif;
  11. ifbisnullthen
  12. setb=0;
  13. endif;
  14. setc=a+b;
  15. selectcassum;
  16. /*
  17. returnc;
1

9. 调用 MySQL 存储过程时候,需要在过程名字后面加“()”,即使没有一个参数,也需要“()”

  1. createprocedurepr_add
  2. (
  3. aint,
  4. bint
  5. )
  6. begin
  7. declarecint;
  8. ifaisnullthen
  9. seta=0;
  10. endif;
  11. ifbisnullthen
  12. setb=0;
  13. endif;
  14. setc=a+b;
  15. selectcassum;
  16. /*
  17. returnc;
2

10. 因为 MySQL 存储过程参数没有默认值,所以在调用 MySQL 存储过程时候,不能省略参数。可以用 null 来替代。

【编辑推荐】

  1. 对MySQL行锁的深入研究
  2. MySQL游标的使用笔记大全
  3. MySQL使用rand 随机查询记录效率测试
  4. MySQL数据库性能优化的实际操作方案
  5. MySQL备份之根据表备份概述
转载请说明出处
知优网 » MySQL 存储过程的基本用法(mysql存储过程使用)

发表评论

您需要后才能发表评论