过程:若干语句,调用时执行封装的体。没有返回值的函数。
函数:是一个有返回值的过程 存储过程:把若干条sql封装起来,起个名字(过程),并存储在数据库中。 也有不存储的过程,匿名过程,用完就扔(mysql不支持匿名过程) create procedure p1()
begin
select 2+3;
end$ show procedure status;//查看现有的存储过程: mysql> call p1();//调用存储过程,Call 存储过程名字(); //存储过程中,使用declare声明变量,declare n int [default 默认值] create procedure p2(age int,hei int)
begin
declare age smallint default 3;
declare height int default 180;
select concat('年龄是:', age, '身高是:' ,height);
end$ mysql> call p2(1,2)$
+---------------------------------------------+
| concat('年龄是:', age, '身高是:' ,height) |
+---------------------------------------------+
| 年龄是:3身高是:180 |
+---------------------------------------------+ create procedure p3()
begin
declare age smallint default 3;
set age := (age +20);
select concat('年龄是:', age);
end$ mysql> call p3()$
+-------------------------+
| concat('年龄是:', age) |
+-------------------------+
| 年龄是:23 |
+-------------------------+ create procedure p4(n int)
begin
select * from orde where gid=n;
end$ mysql> call p4(3)$
+-----+-----+-----+
| oid | gid | num |
+-----+-----+-----+
| 1 | 3 | 100 |
| 1 | 3 | 100 |
| 1 | 3 | 100 |
| 1 | 3 | 100 |
+-----+-----+-----+ create procedure p5(n int)
begin
declare age int default 18;
if age>18 then
select '已成年';
else
select '未成年';
end if;
end$ create procedure p7(n int,m char(1))
begin
if m='t' then
select * from orde where gid=3;
else
select * from orde where gid=2;
end if;
end$ delimiter $
create procedure p8(width int,height int)
begin
if width > height then
select '胖';
elseif width < height then
select '瘦';
else
select '方'
end if;
end$ //编程:顺序、选择、循环。语法格式不一样。
create procedure t8()
begin
declare total int default 0;
declare num int default 0; while num <= 100 do
set total := total + num;
set num = num +1;
end while; select total;
end$ mysql> call t8()$
+-------+
| total |
+-------+
| 5050 |
+-------+ create procedure t8(in n int)//in表示传进去的参数,
begin
declare total int default 0;
declare num int default 0; while num <= n do
set total := total + num;
set num = num +1;
end while; select total;
end$ mysql> create procedure t8(in n int,out total int)//in表示传进去的参数,out是传出去的参数,
begin
declare num int default 0; set total=0;
while num <= n do
set num = num +1;
set total := total + num;
end while; end$ mysql> call t8(100,@tt)$ //输出的值给@tt
Query OK, 0 rows affected mysql> select @tt$
+------+
| @tt |
+------+
| 5151 |
+------+ mysql> create procedure t12(inout io1 int)//inout既可以传进去也可以传出来
begin
declare num int default 0; while num <= io1 do
set num = num +1;
set io1 := io1 + num;
end while; end$ mysql> set @total = 100$
Query OK, 0 rows affected mysql> call t12(@total)$
1264 - Out of range value for column 'io' at row 1 mysql> select @total$ //case用法:
create procedure t13()
begin
declare pos int default 0; set pos := floor(4*rand()); //不能用position是关键字 case pos
when 1 then select "在飞";
when 2 then select "在海里";
when 3 then select "在地上";
else select "不知道";
end case; end$ mysql> call t13()$
+--------+
| 不知道 |
+--------+
| 不知道 |
+--------+ //repeat
create procedure t14()
begin declare total int default 0;
declare i int default 0; repeat
set i:=i+1;
set total:=total+i;
until i>=100
end repeat; select total;
end$ mysql> call t14()$
+-------+
| total |
+-------+
| 5151 |
+-------+

mysql04--存储过程的更多相关文章

  1. 将表里的数据批量生成INSERT语句的存储过程 增强版

    将表里的数据批量生成INSERT语句的存储过程 增强版 有时候,我们需要将某个表里的数据全部或者根据查询条件导出来,迁移到另一个相同结构的库中 目前SQL Server里面是没有相关的工具根据查询条件 ...

  2. Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示

    Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...

  3. MySQL主从环境下存储过程,函数,触发器,事件的复制情况

    下面,主要是验证在MySQL主从复制环境下,存储过程,函数,触发器,事件的复制情况,这些确实会让人混淆. 首先,创建一张测试表 mysql),age int); Query OK, rows affe ...

  4. mysql进阶之存储过程

    往往看别人的代码会有这样的感慨: 看不懂 理还乱 是离愁 别是一番滋味在心头 为什么要使用存储过程? 在mysql开发中使用存储过程的理由: 当希望在不同的应用程序或平台上执行相同的函数,或者封装特定 ...

  5. MySQL 系列(三)你不知道的 视图、触发器、存储过程、函数、事务、索引、语句

    第一篇:MySQL 系列(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:MySQL 系列(二) 你不知道的数据库操作 第三篇:MySQL 系列(三)你不知道的 视图.触发器.存储过程.函数 ...

  6. 参数探测(Parameter Sniffing)影响存储过程执行效率解决方案

    如果SQL query中有参数,SQL Server 会创建一个参数嗅探进程以提高执行性能.该计划通常是最好的并被保存以重复利用.只是偶尔,不会选择最优的执行计划而影响执行效率. SQL Server ...

  7. MSSQL 事务,视图,索引,存储过程,触发器

    事务 事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行. 在数据库系统上执行并发操作时事务是作为最小的控制单元来使用的.这特别适用于多用户同时操作的数据 ...

  8. Mysql - 存储过程/自定义函数

    在数据库操作中, 尤其是碰到一些复杂一些的系统, 不可避免的, 会用到函数/自定义函数, 或者存储过程. 实际项目中, 自定义函数和存储过程是越少越好, 因为这个东西多了, 也是一个非常难以维护的地方 ...

  9. SQL Server存储过程

    创建于2016-12-24 16:12:19 存储过程 概念: 1.存储过程是在数据库管理系统中保存的.预先编译的.能实现某种功能的SQL程序,它是数据库应用中运用比较广泛的 一种数据对象. 2.存储 ...

  10. SQL Server 批量删除存储过程

    原理很简单的'drop proc xxx'即可,下面有提供了两种方式来删除存储过程,其实本质是相同的,方法一是生成删除的sql后直接执行了,方法二会生成SQL,但需要检查后执行,个人推荐第二种做法. ...

随机推荐

  1. POJ 1252 Euro Efficiency(最短路 完全背包)

    题意: 给定6个硬币的币值, 问组成1~100这些数最少要几个硬币, 比如给定1 2 5 10 20 50, 组成40 可以是 20 + 20, 也可以是 50 -10, 最少硬币是2个. 分析: 这 ...

  2. PS学习笔记(02)

    书籍推荐: <设计之下>这本APP设计书字里行间都透露出了真实,作者能将其工作流程和方法分享出来,实在值得尊敬.通过这本书全面了解了真实的设计工作是怎么做的,今后可以用到自己的工作中.赞! ...

  3. sqlserver复制表数据到另一个表

    SQL Server中,如果目标表存在: insert into 目标表 select * from 原表; SQL Server中,,如果目标表不存在: select * into 目标表 from ...

  4. Linux 下Python2.7解决list打印中文字符问题

    在写一个爬取智联招聘数据的爬虫中,将所需内容匹配到后打印出现了utf-8字符,并没有出现中文字符. 例如: >>>listnine = ['梨', '橘子', '苹果', '香蕉'] ...

  5. Leetcode 229.求众数II

    求众数II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...

  6. 【Ajax 3】JavaScript封装Ajax

    导读:上一篇博客简单介绍了一下对Ajax中的核心对象XMLHttpRequest的封装.那么为了方便对Ajax的应用,我们还需要进一步的对Ajax的基本功能进行下一步的封装,不得不说的是早就有人专门写 ...

  7. 如何解决安装istio后istioctl命令每次使用都需要重新配置路径

    Kubernetes在安装istio后初次使用istioctl命令时会提示istioctl command not found 这时候如果在istio文件夹的根目录下配置 export PATH=$P ...

  8. hust 1017 dancing links 精确覆盖模板题

    最基础的dancing links的精确覆盖题目 #include <iostream> #include <cstring> #include <cstdio> ...

  9. Codeforces917C. Pollywog

    $n \leq 1e8$个石头,$x \leq 8$个蝌蚪一开始在最左边$x$个石子,要跳到最右的$x$个,每次只能最左边的蝌蚪跳一次,一个石头不能站两个蝌蚪,跳可以跳$1到k,x \leq k \l ...

  10. POJ3233:Matrix Power Series

    对n<=30(其实可以100)大小的矩阵A求A^1+A^2+……+A^K,K<=1e9,A中的数%m. 从K的二进制位入手.K分解二进制,比如10110,令F[i]=A^1+A^2+……+ ...