mysql高级 存储过程[1]

来源:互联网 发布:可以查询淘宝账号 编辑:程序博客网 时间:2024/06/16 09:20
在一起的语言,叫做'过程' 
php中没有过程,只有函数, 准确的说.函数能起到过程的作用


"过程"就是封装语句,调用的时候,这些封装体执行
  函数是有返回值的过程,而过程没有返回值的函数


过程 --- 把若干条sql封装起来, 起个名字就是过程
存储过程 --- 把此过程存在数据库中, 叫做存储过程
匿名过程  --- mysql 不支持



语法:

# 创建:create procedure procedureName()begin --  sql 语句end $# 查看:show procedure status \G$# 调用:call procedureName();



 ---  存储过程是可以编程的,可以用表达式 控制结构等 完成复杂的功能
 - 在存储过程中,用 declare 声明变量
 - 格式 declare 变量名 类型 [default默认值]

创建:

create procedure p2()begin declare age int default 18;declare height int default 180;select concat('年龄是',age,'身高是',height);end $----------------------------------------------mysql> call p2()$+--------------------------------------+| concat('年龄是',age,'身高是',height) |+--------------------------------------+| 年龄是18身高是180                    |+--------------------------------------+----------------------------------------------

运算:

 - 变量可以做sql语句中合法的运算,如 + - * /  - 怎么赋值给变量? -- set 变量名 := x+xcreate procedure p3()begin declare age int default 18;declare height int default 180;set age := age+20;select concat('年龄是',age,'身高是',height);select concat('20年后年龄是',age);end $----------------------------------------------mysql> call p3()$+--------------------------------------+| concat('年龄是',age,'身高是',height) |+--------------------------------------+| 年龄是38身高是180                    |+--------------------------------------+1 row in set (0.00 sec)+----------------------------+| concat('20年后年龄是',age) |+----------------------------+| 20年后年龄是38             |+----------------------------+1 row in set (0.01 sec)----------------------------------------------

控制:

-- if/else 控制结构/*if conditon then statement else xxooend ;*/create procedure p4()begin declare age int default 18;if age >= 18 then select '已成年';else select '未成年';end if;end $----------------------------------------------mysql> call p4()$+--------+| 已成年 |+--------+| 已成年 |+--------+----------------------------------------------

输送参数:

/*存储过程的括号里,可以声明参数,语法 [in/out/inout] 参数名 参数类型*/create procedure p5(width int ,height int)begin select concat('面积是:',width*height);if width > height then select '胖';elseif width < height thenselect '瘦';else select '方';end if;end $----------------------------------------------mysql> call p5(1,1)$+---------------------------------+| concat('面积是:',width*height) |+---------------------------------+| 面积是:1                       |+---------------------------------+1 row in set (0.00 sec)+----+| 方 |+----+| 方 |+----+1 row in set (0.01 sec)----------------------------------------------mysql> call p5(4,5)$+---------------------------------+| concat('面积是:',width*height) |+---------------------------------+| 面积是:20                      |+---------------------------------+1 row in set (0.00 sec)+----+| 瘦 |+----+| 瘦 |+----+1 row in set (0.01 sec)----------------------------------------------mysql> call p5(6,5)$+---------------------------------+| concat('面积是:',width*height) |+---------------------------------+| 面积是:30                      |+---------------------------------+1 row in set (0.00 sec)+----+| 胖 |+----+| 胖 |+----+1 row in set (0.01 sec)----------------------------------------------



更复杂的~......

更实用的~......

未完~



0 0