存储过程是SQL语句和控制语句的预编译集合,以一个名称存储并作为一个单元处理
存储过程存储在数据库内,可以由应用程序调用执行,而且允许用户声明变量以及进行流程控制,可以接收输入或输出类型的参数,而且可以由多个返回值
存储过程的优点:
- 增强SQL语句的功能和灵活性
- 实现较快的执行速度
- 减少网络流量
创建存储过程:
CREATE
[DEFINER = {user | CURRENT_USER }] #若省略这句话,会默认使用当前用户
PROCEDURE sp_name([proc_parameter[,...]])
[characteristic ...] routine_body
注释:
sp_name指的是存储过程的名字
proc_parameter是存储过程的参数,形式为:[ IN | OUT | INOUT ] param_name type
IN,表示该参数的值必须在调用存储过程时指定,在存储过程中,这个值不能被返回
OUT,表示该参数的值可以被存储过程中的程序改变,并且可以返回
INOUT,表示该参数的在调用时指定,并且可以被改变和返回