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,但需要检查后执行,个人推荐第二种做法. ...
随机推荐
- DEV Express中Bar Manager的使用
未排版 在barManager中可以添加多种元素,如皮肤按钮,复选框等,但是下拉菜单却给出了多个冗余的控件. 遗留问题:怎么设置Bar为大图标,查找是否存在Ribbon控件. Bar 1, ...
- 【MySQL】性能优化之 Index Condition Pushdown
一 概念介绍 Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一种在存储引擎层使用索引过滤数据的一种优化方式.a 当关闭ICP时,index ...
- 大数据学习——安装zooleeper
1 alt+p,上传zookeeper-3.4.5.tar.gz 2 解压安装包 ,安装在apps目录下 tar -zxvf zookeeper-3.4.5.tar.gz -C apps 3 删除zo ...
- C#静态构造函数和非静态构造函数
// 使用静态构造函数时,需要注意几点 //1. 一个类中,最多只能有一个静态构造函数,不允许静态构造函数的重载: //2. 不能加任何访问修饰符(public/private/internale等) ...
- Codeforces Round #387 (Div. 2) A+B+C+D!
A. Display Size 水题,暴力(数据都是水题).0:04 int main() { int n; while(~scanf("%d",&n)) { int mi ...
- Codeforces936D. World of Tank
$n \leq 1e9$,$n*2$的网格里有$m_1+m_2 \leq 1e6$个障碍物,现有一坦克从$(0,1)$出发要到$(n+1,1/2)$,他每秒可以换行(纵坐标1变2或2变1)也可以发炮弹 ...
- iOS - 系统方法中弃用的关键字的了解 NS_AVAILABLE和NS_DEPRECATED
NS_AVAILABLE_IOS(5_0) 这个方法可以在iOS5.0及以后的版本中使用,如果在比5.0更老的版本中调用这个方法,就会引起崩溃. NS_AVAILABLE(_mac, _ios) ...
- Django学习之 - 基础模板语言
模板语言if/else/endif {% if today_is_weekend %} <p>Welcome to the weekend!</p> {% else %} &l ...
- 从零开始写STL—模板元编程之tuple
tuple Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generali ...
- Python高级进阶(一)Python框架之Django入门
传说中的Django Django由来 Django是一个开放源代码的Web应用框架,由Python写成.采用了MVC的框架模式,即模型M,视图V和控制器C.它最初是被开发来用于管理劳伦斯出版集团旗下 ...