mysql04--存储过程
过程:若干语句,调用时执行封装的体。没有返回值的函数。
函数:是一个有返回值的过程 存储过程:把若干条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--存储过程的更多相关文章
- 将表里的数据批量生成INSERT语句的存储过程 增强版
将表里的数据批量生成INSERT语句的存储过程 增强版 有时候,我们需要将某个表里的数据全部或者根据查询条件导出来,迁移到另一个相同结构的库中 目前SQL Server里面是没有相关的工具根据查询条件 ...
- Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示
Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...
- MySQL主从环境下存储过程,函数,触发器,事件的复制情况
下面,主要是验证在MySQL主从复制环境下,存储过程,函数,触发器,事件的复制情况,这些确实会让人混淆. 首先,创建一张测试表 mysql),age int); Query OK, rows affe ...
- mysql进阶之存储过程
往往看别人的代码会有这样的感慨: 看不懂 理还乱 是离愁 别是一番滋味在心头 为什么要使用存储过程? 在mysql开发中使用存储过程的理由: 当希望在不同的应用程序或平台上执行相同的函数,或者封装特定 ...
- MySQL 系列(三)你不知道的 视图、触发器、存储过程、函数、事务、索引、语句
第一篇:MySQL 系列(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:MySQL 系列(二) 你不知道的数据库操作 第三篇:MySQL 系列(三)你不知道的 视图.触发器.存储过程.函数 ...
- 参数探测(Parameter Sniffing)影响存储过程执行效率解决方案
如果SQL query中有参数,SQL Server 会创建一个参数嗅探进程以提高执行性能.该计划通常是最好的并被保存以重复利用.只是偶尔,不会选择最优的执行计划而影响执行效率. SQL Server ...
- MSSQL 事务,视图,索引,存储过程,触发器
事务 事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行. 在数据库系统上执行并发操作时事务是作为最小的控制单元来使用的.这特别适用于多用户同时操作的数据 ...
- Mysql - 存储过程/自定义函数
在数据库操作中, 尤其是碰到一些复杂一些的系统, 不可避免的, 会用到函数/自定义函数, 或者存储过程. 实际项目中, 自定义函数和存储过程是越少越好, 因为这个东西多了, 也是一个非常难以维护的地方 ...
- SQL Server存储过程
创建于2016-12-24 16:12:19 存储过程 概念: 1.存储过程是在数据库管理系统中保存的.预先编译的.能实现某种功能的SQL程序,它是数据库应用中运用比较广泛的 一种数据对象. 2.存储 ...
- SQL Server 批量删除存储过程
原理很简单的'drop proc xxx'即可,下面有提供了两种方式来删除存储过程,其实本质是相同的,方法一是生成删除的sql后直接执行了,方法二会生成SQL,但需要检查后执行,个人推荐第二种做法. ...
随机推荐
- python基础——1(简介与变量)
目录 一.编程语言介绍 1.1.机器语言: 1.2.汇编语言: 1.3.高级语言: 二.安装python解释器 2.1.验证python解释器的安装 2.2.设置环境变量 三.执行python程序的两 ...
- C# 禁止通过标题栏移动窗体
protected override void WndProc(ref Message m) { base.WndProc (ref m); if(m.Msg == 0x84) //不让拖动标题栏 { ...
- Fiddler抓包-会话框添加查看get与post请求类型选项
from:https://www.cnblogs.com/yoyoketang/p/7061990.html 在使用fiddler抓包的时候,查看请求类型get和post每次只有点开该请求,在Insp ...
- mq推送消息
场景:BDM(实名制系统)同步数据到CRNS(实名制系统) 一,首先建个队列,队列名字为 bdm_empolyeeinfo_crns 二,applicationContext-rabbitmq.xml ...
- HDU-1041-Computer Transformation,大数递推,水过~~
Computer Transformatio ...
- Codeforces396A - On Number of Decompositions into Multipliers
Portal Description 给出\(n(n\leq500)\)个\([1,10^9]\)的数,令\(m=\prod_{i=1}^n a_i\).求有多少个有序排列\(\{a_n\}\),使得 ...
- gcc,gdb基础学习1
gcc: (1)gcc -O1 -S code.c 这里的-S可以的到code.c的汇编(只进行了预处理和编译这:两个阶段形成 了汇编代码code·s) (2)gcc -c code.c 这里 ...
- .NET Core windows开发环境 + Git代码控管 + Docker 部署环境搭建
开发环境准备 下载vs code,.NET Core sdk: https://www.microsoft.com/net/core#windowscmd 目前最新版为code 1.8.1,.NET ...
- Codeforces 703C(计算几何)
C. Chris and Road time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- HDU 1896 【留个使用priority_queue容器的样例】
感谢<啊哈!算法>的讲解,水鸟弄懂了什么是优先队列. 题意是:在路上有很多石子,给出他们的初始位置和小明能够将他们扔出的距离,当小明遇到奇数个石子的时候就会把它扔出,遇到偶数个就会忽略他, ...