过程:若干语句,调用时执行封装的体。没有返回值的函数。
函数:是一个有返回值的过程 存储过程:把若干条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. 按Esc按钮关闭layer弹窗

    //按Esc关闭弹出框 $(document).ready(function () { }).keydown( function (e) { if (e.which === 27) {  layer. ...

  2. bzoj 1500 [NOI 2005] 维修数列

    题目大意不多说了 貌似每个苦逼的acmer都要做一下这个splay树的模版题目吧 还是有很多操作的,估计够以后当模版了.... #include <cstdio> #include < ...

  3. Codevs 2666 2666 Accept Ratio

    时间限制: 1 s  空间限制: 32000 KB   题目等级 : 钻石 Diamond 题目描述 Description 某陈痴迷于pku的ACM题库,常常彻夜奋斗刷题.他最近的目标是在NOIP0 ...

  4. BZOJ1696: [Usaco2007 Feb]Building A New Barn新牛舍

    n<=10000个点(xi,yi),找到一个不同于给出的所有点的点,使得该点到所有点的曼哈顿距离最小并找出这样的点的个数. 第一眼看上去这不是中位数嘛,奇数一个点偶数一片,然后找一下这篇区域有几 ...

  5. 显示倒计时,为零时自动点击按钮提交【JavaScript实现】

    原文发布时间为:2008-10-17 -- 来源于本人的百度文章 [由搬家工具导入] <html> <head> <title>显示倒计时,完毕提交</tit ...

  6. Fibonacci--poj3070(矩阵快速幂)

    http://poj.org/problem?id=3070 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn ...

  7. react 使用 eslint 的三种代码检查方案总结,多了解点--让代码更完美....

    1.介绍 ESLint 是一个可扩展,每条规则独立,被设计为完全可配置的lint工具. 可以用来检测代码,避免低级错误 可以用来规范代码的开发风格,统一代码习惯. 2.为什么使用 ESLint ? 统 ...

  8. LINUX下安装和配置WEBLOGIC10.0.3

    weblogic for linux安装 首先声明,我参考了某位原创者的笔记,加以整理的.安装1. 安装前的准备工作1.1 首先请确认您要安装的Weblogic版本所在的平台已通过了BEA的认证,完整 ...

  9. GAN Generative Adversarial Network 生成式对抗网络-相关内容

    参考: https://baijiahao.baidu.com/s?id=1568663805038898&wfr=spider&for=pc Generative Adversari ...

  10. 条款一:尽量使用const、inline而不是#define

    #define ASPECT_RATIO 1.653 编译器会永远也看不到ASPECT_RATIO这个符号名,因为在源码进入编译器之前,它会被预处理程序去掉,于是ASPECT_RATIO不会加入到符号 ...