Home  >  Article  >  Database  >  MYSQL存储过程与函数_MySQL

MYSQL存储过程与函数_MySQL

WBOY
WBOYOriginal
2016-06-01 13:47:43869browse

bitsCN.com

  一、模式

  

/

 

  二、存储过程与存储函数

  语法如下:

  CREATE PROCEDURE sp_name ([proc_parameter[,...]])

  [characteristic ...] routine_body

  CREATE FUNCTION sp_name ([func_parameter[,...]])

  RETURNS type

  [characteristic ...] routine_body

  proc_parameter:

  [ IN | OUT | INOUT ] param_name type

  func_parameter:

  param_name type

  type:

  Any valid MySQL data type

  characteristic:

  LANGUAGE SQL

  | [NOT] DETERMINISTIC

  | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }

  | SQL SECURITY { DEFINER | INVOKER }

  | COMMENT string

  routine_body:

  Valid SQL procedure statement or statements

  例如:存储过程

  mysql> use test;

  mysql> delimiter // ###定义"//"为语句开始执行符号

  mysql> create procedure simpleproc(out param1 int)

  begin

  select count(*) into param1 from t;

  end

  //

  mysql>delimiter ; ####重新定义;为语句开始执行的符号

  mysql> call simpleproc(@a);

  mysql> select @a; ####a为一个变量

  +------+

  | @a |

  +------+

  | 1 |

  +------+

  区别:过程只处理一件事,并无返回值,但可以通过出口参数param1来返回处理后的值;而存储函数可以返回值

  例如:存储函数

  1.字符串拼接函数hello()

  mysql> use test;

  mysql> delimiter //

  mysql> create function hello (t char(20)) returns char(50)

  return concat(‘hello, ’ ,t, ‘!’);

  //

  mysql> delimiter ;

  mysql> select hello(‘world’);

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn