存储过程

认识

在一些编程语言中, 如pascal, 有一个概念叫"过程" procedure, 和"函数" function, 如VB中的sub. Java, Python, PHP, 没有过程, 只有function.

过程(procedure) : 封装了若干条语句, 调用时, 这些封装体执行.

函数(function): 是一个有返回值的 "过程" (ps: Python函数即可是过程, 也是是函数)

存储过程(sql_procedure): 将若干条sql封装起来, 取一个名字, 即为过程, 把此过程存储在数据库中, 即存储过程.

存储过程-创建语法

-- 创建
create procedure procedureName()
begin
SQL语句1;
SQL语句2;.....
end -- 调用
call procedureName();

第一存储过程

helloWorld

-- 第一个存储过程: 打印hello world
delimiter //
drop procedure if exists p1;
create procedure p1()
begin
select "hello world!";
select 1 + 1;
end //
delimiter ; -- CALL 调用
call p1;
-- 查看: show procedure status;
show show procedure status;

效果

mysql> -- 第一个存储过程: 打印hello world
delimiter //
drop procedure if exists p1;
create procedure p1()
begin
select "hello world!";
select 1 + 1;
end //
delimiter ;
Query OK, 0 rows affected (0.16 sec) Query OK, 0 rows affected (0.16 sec) mysql> call p1();
+--------------+
| hello world! |
+--------------+
| hello world! |
+--------------+
1 row in set (0.06 sec) +-------+
| 1 + 1 |
+-------+
| 2 |
+-------+
1 row in set (0.21 sec) Query OK, 0 rows affected (0.00 sec)

引入变量-declare 局部

存储过程是可以编程的, 意味着可以用变量, 表达式, 控制结构来完成各种复杂的功能.

在存储过程中, 用 declare 变量名 变量类型 [default 默认值].

-- 变量引入
drop procedure if exists p2;
delimiter //
create procedure p2()
begin
declare age int default 18;
declare height int default 180;
-- concat 拼接输出
select concat("油哥的年龄是:", age, "身高是:", height);
end //
delimiter ; call p2();

效果:

call p2();
Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) +-------------------------------------------------+
| concat("油哥的年龄是:", age, "身高是:", height) |
+-------------------------------------------------+
| 油哥的年龄是:18身高是:180 |
+-------------------------------------------------+
1 row in set (0.10 sec) Query OK, 0 rows affected (0.04 sec)

引入运算

在储存过程中, 变量可以引入sql语句中合法的运算, 如 + - * /, 值的注意的是, 运算的结果,如何赋值给变量?

**set 变量名 := expression ** 在存储过程中的变量是一个局部变量.

set @变量名 := expression 用户(会话)变量, 在存储过程外面也能用, 类似"全局变量".

**declare 变量名 变量类型 [default value] **用在过程中的局部变量, 声明类型.

关于赋值 = 与 := 的区别

:=

  • 标准的赋值符号, 在任何场景都是赋值.

=

  • 只在 set 和 update 是和 := 一样是赋值, 其他都是等于的作用.
drop procedure if exists p3;
delimiter //
create procedure p3()
begin
declare age int default 18; select concat("现在的年龄是:", age);
-- 变量运算,赋值
set age := age + 10;
select concat("10年后, 年龄变成了:", age);
end //
delimiter ;
-- out
mysql> call p3();
+------------------------------+
| concat("现在的年龄是:", age) |
+------------------------------+
| 现在的年龄是:18 |
+------------------------------+
1 row in set (0.11 sec) +------------------------------------+
| concat("10年后, 年龄变成了:", age) |
+------------------------------------+
| 10年后, 年龄变成了:28 |
+------------------------------------+
1 row in set (0.25 sec)

控制结构 if - then - else - end if;

-- 语法
if condition then
statement_01
else
statement_02
end if;
drop procedure if exists p4;
delimiter //
create procedure p4()
begin
declare age int default 18; if age >= 18 then
select "已成年";
else
select "未成年";
end if;
end //
delimiter ; -- test
call p4(); -- out
+--------+
| 已成年 |
+--------+
| 已成年 |
+--------+
1 row in set (0.09 sec) Query OK, 0 rows affected (0.00 sec)

存储过程传参

存储过程的括号里, 可以声明参数, 语法是 [in / out / inout] 参数名 参数类型

in 表示往procedure里面传参数; out 表示其往外发射参数

-- 输入矩形的 width, height 求矩形的面积
drop procedure if exists p5;
delimiter //
create procedure p5(width int, height int)
begin
select concat("面积是:", width * height);
if width > height then
select "比较胖";
elseif width < height then
select "比较瘦";
else
select "方的一痞";
end if; end //
delimiter ; call p5(12, 13); -- out
call p5(12, 13);
Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.05 sec) +-----------------------------------+
| concat("面积是:", width * height) |
+-----------------------------------+
| 面积是:156 |
+-----------------------------------+
1 row in set (0.11 sec) +--------+
| 比较瘦 |
+--------+
| 比较瘦 |
+--------+
1 row in set (0.22 sec) Query OK, 0 rows affected (0.01 sec)

流程控制 while , repeat, loop

任何编程语言, 只要具备控制结构顺序, 选择, 循环就足够了.

感觉就是, 编程其实思路都是一样的, 只是不同语言的应用场景, 语法特性有差别而已, 思路都是一样的.

-- while 循环 语法
WHILE search_condition DO
statement_list
END WHILE [end_label]
-- 求 1+2+3+...100
drop procedure if exists p6;
delimiter //
create procedure p6()
begin
declare total int default 0;
declare num int default 0;
-- while 循环
while num <= 100 do
set total := total + num;
set num := num + 1;
end while;
-- 最后输出结果
select concat("1+2+...100的值是: ", total) as 'sum';
end //
delimiter ; call p6(); -- out
call p6();
Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.05 sec) +------------------------+
| sum |
+------------------------+
| 1+2+...100的值是: 5050 |
+------------------------+
1 row in set (0.09 sec)

改进: 求 1+2+....N 的和, 这里引入参数 IN

-- 求 1+2+3+...N
drop procedure if exists p7;
delimiter //
-- 传入参数 in类型
create procedure p7(in n int)
begin
declare total int default 0;
declare num int default 0;
-- while 循环
while num <= n do
set total := total + num;
set num := num + 1;
end while;
-- 最后输出结果
select concat("1+2+.. 的值是: ", total) as 'sum';
end //
delimiter ; call p7(10000); -- out
call p7(10000);
Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) +-------------------------+
| sum |
+-------------------------+
| 1+2+.. 的值是: 50005000 |
+-------------------------+
1 row in set (0.14 sec)

out 型参数

drop procedure if exists p8;
delimiter //
create procedure p8(in n int, out total int)
begin
-- 声明一个局部(临时)变量num来存储 1..n
declare num int default 0;
-- while
while num <= n do
set total := total + num;
set num := num + 1;
end while;
-- select concat("the sum is:", total)
end //
delimiter ; -- 区别: 没有在 begin ..end 中声明 total变量, 而是在 OUT类型的参数中.
-- out: 传入一个变量去接收输出
call p8(100, @cj); mysql> select @cj;
+------+
| @cj |
+------+
| NULL |
+------+
1 row in set (0.10 sec)

NULL 的特殊性, 导致没有正确输出 , 解决: 给total 一个默认值即可

mysql> select null = null;
+-------------+
| null = null |
+-------------+
| NULL |
+-------------+
1 row in set (0.09 sec) mysql> select 1 + null;
+----------+
| 1 + null |
+----------+
| NULL |
+----------+
1 row in set (0.05 sec)
-- 解决null的特殊性
drop procedure if exists p8;
delimiter //
create procedure p8(in n int, out total int)
begin -- 先声明一个局部(临时)变量num来存储 1..n
declare num int default 0;
-- 再给out变量一个默认值即可(顺序是先declare哦)
set total := 0;
-- while
while num <= n do
set total := total + num;
set num := num + 1;
end while;
end //
delimiter ; -- 区别: 没有在 begin ..end 中声明 total变量, 而是在 OUT类型的参数中. -- out: 传入一个变量去接收输出的total变量值
call p8(100, @theSum);
select @theSum; -- out mysql> call p8(100, @theSum);
Query OK, 0 rows affected (0.00 sec) mysql> select @theSum;
+---------+
| @theSum |
+---------+
| 5050 |
+---------+
1 row in set (0.11 sec)

小结参数 in 和 out 和 inout

  • in 类型, 是要输入一个值进去, 传递给procedure的in类型变量(传值)
  • out类型, 是要输入一个变量进去, 接收procedure的out类型变量的值
  • inout类型, 传入值进入和传入变量接收值出来
-- inout 类型
drop procedure if exists p9;
delimiter //
create procedure p9(inout age int)
begin
set age := age + 20;
end //
delimiter ; -- call 的时候, inout, 首先要定义一个"全局(会话变量)", 然后再传入
-- out
mysql> set @age := 100;
Query OK, 0 rows affected (0.00 sec) mysql> call p9(@age);
Query OK, 0 rows affected (0.00 sec) mysql> select @age;
+------+
| @age |
+------+
| 120 |
+------+

Mysql 存储过程初识的更多相关文章

  1. MySQL存储过程(转)

    一.MySQL 创建存储过程 "pr_add" 是个简单的 MySQL 存储过程,这个存储过程有两个 int 类型的输入参数 "a"."b" ...

  2. MySql存储过程

    MySQL 存储过程 ```sql CREATE PROCEDURE myprocedure (IN para01 INTEGER) BEGIN DECLARE var01 CHAR(10); IF ...

  3. mysql存储过程和存储函数

    mysql存储过程和存储函数 存数函数代码示例: DROP PROCEDURE IF EXISTS calc_ci_day_suc_rate; delimiter // CREATE FUNCTION ...

  4. mysql存储过程编写-入门案例-遁地龙卷风

    (-1)写在前面 这篇文章只是简要的叙述了mysql存储过程编写的基本概念. 我使用的mysql版本是5.7.9-log. 参照<<深入浅出MySQL>>. (0) delim ...

  5. MySQL存储过程动态SQL语句的生成

    用Mysql存储过程来完成动态SQL语句,使用存储过程有很好的执行效率: 现在有要求如下:根据输入的年份.国家.节假日类型查询一个节假日,我们可以使用一般的SQL语句嵌入到Java代码中,但是执行效率 ...

  6. MySQL 存储过程

    MySQL 存储过程 存储过程是通过给定的语法格式编写自定义的数据库API,类似于给数据库编写可执行函数. 简介 存储过程是一组为了完成特定功能的SQL语句集合,是经过编译后存储在数据库中. 存储过程 ...

  7. mysql存储过程详解

    mysql存储过程详解 1.      存储过程简介   我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的S ...

  8. PHP调用MYSQL存储过程实例

    PHP调用MYSQL存储过程实例 标签: mysql存储phpsqlquerycmd 2010-09-26 11:10 11552人阅读 评论(3) 收藏 举报 实例一:无参的存储过程$conn = ...

  9. mysql存储过程语法及实例

    存储过程如同一门程序设计语言,同样包含了数据类型.流程控制.输入和输出和它自己的函数库. --------------------基本语法-------------------- 一.创建存储过程cr ...

随机推荐

  1. es6 中的模块导入与nodejs 中模块的导入的异同!

    我们知道es6 的模块导入导出是通过import 和 export 来实现,而nodejs的模块导入导出是通过require 和module.exports 来实现,那么它们有什么异同吗? 请看如下: ...

  2. selenium--页面元素是否可见和可操作

    判断元素是否可见 from selenium import webdriver import unittest class Test_Display(unittest.TestCase): def t ...

  3. Spring Boot 知识笔记(全局异常)

    通过ControllerAdvice和ExceptionHandler捕获异常和错误信息,向前端返回json格式的状态码及异常描述信息. 1.新建一个Controller,抛出一个异常. packag ...

  4. QFileInfo().created() 警告 created is deprecated 怎么改?

    有这样一行代码操作: QFileInfo(...).created().toString(...); QtCreator提示警告: 'created' is deprecated 'created' ...

  5. FreeSql 访问 Oracle 解决大小写问题

    方法一 new FreeSqlBuilder() .UseSyncStructureToUpper(true) .Build() 方法二 全局转换实体属性名方法,这种只能转属性. 其实是通过Aop方法 ...

  6. Docker&持续集成与容器管理--系列教程

    一 Docker简介 Docker介绍 Docker架构 二 Docker安装 Ubuntu Docker 安装 CentOS Docker 安装 Windows Docker 安装 MacOS Do ...

  7. javascript系列--认识并理解构造函数,原型和原型链

    一.前言 介绍构造函数,原型,原型链.比如说经常会被问道:symbol是不是构造函数:constructor属性是否只读:prototype.[[Prototype]]和__proto__的区别:什么 ...

  8. 阿里云配置DDoS高防

  9. AntDesign vue学习笔记(六)Table 显示图片

    AntDeign官网上没有table动态绑定显示图片的示例,baidu上搜索出来的大部分都是React语法,无法使用. 经过摸索,实现方法如下:以显示一个图片,一个按钮为例(picurl是返回的jso ...

  10. 大一0基础小白用最基础C写哥德巴赫猜想

    #include <stdio.h>int main (){ int a,b,c,k,count1,count2; for(a=4;a<=1200;a=a+2){ for(b=2;b ...