过程:若干语句,调用时执行封装的体。没有返回值的函数。
函数:是一个有返回值的过程 存储过程:把若干条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. 如何根据实体动态生成sql语句

    该文章同时解决了,如何向数据库中添加Null值,以及如何处理“参数化查询未提供参数”的错误.解决方案请看第二段折叠的代码. 背景: 在项目开发的过程中,往往需要根据实体的值来修改sql语句,比如说,有 ...

  2. 在JavaScript中获取Map集合中的key和value值(前提是:既不知道key为什么值,也不知道value有哪些值)

  3. Triangular Pastures (二维01背包)

    描述Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectang ...

  4. PTA 04-树4 是否同一棵二叉搜索树 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/712 5-4 是否同一棵二叉搜索树   (25分) 给定一个插入序列就可以唯一确定一棵二 ...

  5. rsync 同步文件重复拷贝问题

    rsync 是同步文件的利器,一般用于多个机器之间的文件同步与备份,同时也支持在本地的不同目录之间互相同步文件.在这种场景下,rsync 远比 cp 命令更加合适,它只会同步需要更新的文件,默认情况下 ...

  6. python 快排,堆排,归并

    #归并排序 def mergeSort(a,L,R) :     if(L>=R) :         return     mid=((L+R)>>1)     mergeSort ...

  7. iOS时钟,秒针扫秒样式

    昨天做一个时钟小demo,发现了一些问题. 描述能力有限,我封装好了一个时钟框架,朋友们可以参考      https://github.com/qianlishun/ClockView 点击这里可以 ...

  8. 【HDOJ6318】Swaps and Inversions(树状数组)

    题意: 给定一串数组,其中含有一个逆序对则需要花费x,交换相邻两个数需要花费y,输出最小花费. n<=1e5,-1e9<=a[i]<=1e9 思路: #include<cstd ...

  9. Python 列表的复制操作

    2013-10-18 10:07:03|   import copy a = [1,2,3,['a','b']] b = a c = a[:] d = copy.copy(a) e = copy.de ...

  10. 编译.net .net Core程序 代码,仅做备份

    //创建一个ProcessStartInfo对象 使用系统shell 指定命令和参数 设置标准输出 //编译.net core项目 var psi = new ProcessStartInfo(&qu ...