Mysql存储过程简明使用
mysql> \d // 改变命令行下的结束符标志
mysql> create procedure p3()
-> begin
-> set @i=1; # 这样也可以定义变量变给值
-> while @i<10000 do
-> insert into t3 values (@i);
-> set @i=@i+1;
-> end while;
-> end //
1.存储过程创建语法
create procedure 存储过程名称()
begin
-- sql语句
end;
2.查看已有的存储过程
show procedure status;
3.调用存储过程
call 存储过程名称();
4.在存储过程中申明变量 declare
--格式 declare 变量名 变量类型 [default 默认值]
create procedure p2()
begin
declare age smallint default 18;
declare hi int smallint default 180;
select concat('年龄是:',age,'身高为:',hi);
end;
--运算和赋值
-- set 变量名 := expression
create procedure p3()
begin
declare age smallint default 18;
declare hi int smallint default 180;
set age := age+20;
select concat('年龄是:',age,'身高为:',hi);
end;
-- if/else控制结构
/**
if condition then
执行语句
else
end if;
*/
create procedure p4()
begin
declare age smallint default 18;
declare hi int smallint default 180;
if age>=18 then
select '成年';
else
select '很小';
end if;
end;
-- 存储过程传参
/**
存储过程的括号里,可以声明参数
语法[in/out/inout] 参数名 参数类型
*/
create procedure p5(width int,height int)
begin
select width+height;
end;
call p5(10,20);
--循环
--求1到100之和
create procedure p6()
begin
declare sum smallint default 0;
declare i smallint default 0;
while i<=100 do
set sum := sum+i;
set i := i+1;
end while;
select sum;
end;
call p6();
create procedure p7(in n int)
begin
declare sum smallint default 0;
declare i smallint default 0;
while i<=n do
set sum := sum+i;
set i := i+1;
end while;
select sum;
end;
call p7(100);
-- out 类型参数
create procedure p8(in n int,out total int)
begin
declare num int default 0;
set total := 0;
while num<n do
set num := num+1;
set total := total+num;
end while;
end;
call p8(100,@sum);
select @sum;
--inout类型
create procedure p9(inout age int)
begin
set age := age+20;
end;
set @currAge = 18;
call p9(@currAge);
select @currAge;
-- case用法
create procedure p10()
begin
declare pos int default 0;
set pos := floor(5*rand());
case pos
when 1 then select '小明';
when 2 then select '小华';
when 3 then select '小张';
else select '未知';
end case;
end;
call p10();
-----repeat 循环
/*
repeat
sql语句;
.....
sqlN;
until condition end repeat;
*/
create procedure p11()
begin
declare i int default 0;
declare sum int default 0;
repeat
set i := i+1;
set sum := sum+i;
until i>=100 end repeat;
select sum;
end;
##########################################################
1.查看存储过程
show procedure status;
2.删除存储过程
drop procedure 存储过程名称
3.创建存储过程
create procedure p1()
begin
select * from table;
end; call p1(); create procedure p2(n int)
begin
select * from table where num>n;
end; call p2(2); create procedure p3(n int,j char(1))
begin
if j='a' then
select * from table where num>n;
else
select * from table where num<n;
end if;
end; call p3(2,'a'); #计算1->n的和
create procedure p4(n smallint)
begin
declare i int;
declare sum int;
set i = 1;
set sum = 0;
while i <= n do
set sum = sum+i;
set i = i+1;
end while;
select sum;
end;
call p4(100);
4.调用存储过程
call 存储过程名称();
Mysql存储过程简明使用的更多相关文章
- MySQL存储过程(转)
一.MySQL 创建存储过程 "pr_add" 是个简单的 MySQL 存储过程,这个存储过程有两个 int 类型的输入参数 "a"."b" ...
- MySql存储过程
MySQL 存储过程 ```sql CREATE PROCEDURE myprocedure (IN para01 INTEGER) BEGIN DECLARE var01 CHAR(10); IF ...
- mysql存储过程和存储函数
mysql存储过程和存储函数 存数函数代码示例: DROP PROCEDURE IF EXISTS calc_ci_day_suc_rate; delimiter // CREATE FUNCTION ...
- mysql存储过程编写-入门案例-遁地龙卷风
(-1)写在前面 这篇文章只是简要的叙述了mysql存储过程编写的基本概念. 我使用的mysql版本是5.7.9-log. 参照<<深入浅出MySQL>>. (0) delim ...
- MySQL存储过程动态SQL语句的生成
用Mysql存储过程来完成动态SQL语句,使用存储过程有很好的执行效率: 现在有要求如下:根据输入的年份.国家.节假日类型查询一个节假日,我们可以使用一般的SQL语句嵌入到Java代码中,但是执行效率 ...
- MySQL 存储过程
MySQL 存储过程 存储过程是通过给定的语法格式编写自定义的数据库API,类似于给数据库编写可执行函数. 简介 存储过程是一组为了完成特定功能的SQL语句集合,是经过编译后存储在数据库中. 存储过程 ...
- mysql存储过程详解
mysql存储过程详解 1. 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的S ...
- PHP调用MYSQL存储过程实例
PHP调用MYSQL存储过程实例 标签: mysql存储phpsqlquerycmd 2010-09-26 11:10 11552人阅读 评论(3) 收藏 举报 实例一:无参的存储过程$conn = ...
- mysql存储过程语法及实例
存储过程如同一门程序设计语言,同样包含了数据类型.流程控制.输入和输出和它自己的函数库. --------------------基本语法-------------------- 一.创建存储过程cr ...
随机推荐
- ehcache 缓存
一:详细配置步骤 1,添加ehcache.xml文件 将ehcache.xml文件添加到src路径下面.ehcache.xml文件内容如下 <ehcache> <diskStore ...
- spark新能优化之广播共享数据
如果你的算子函数中,使用到了特别大的数据,那么,这个时候,推荐将该数据进行广播.这样的话,就不至于将一个大数据拷贝到每一个task上去.而是给每个节点拷贝一份,然后节点上的task共享该数据. 这样的 ...
- spark优化之优化数据结构
概序: 要减少内存的消耗,除了使用高效的序列化类库以外,还有一个很重要的事情,就是优化数据结构.从而避免Java语法特性中所导致的额外内存的开销,比如基于指针的Java数据结构,以及包装类型. 有一个 ...
- P168 实战练习(构造方法)
尝试编写一个矩形类,将长宽做为矩形类的属性,在构造方法中将长宽初始化,定义一个成员方法求此矩形的面积. 编写代码如下: 创建Rectangular类,则相关代码为: package org.hanqi ...
- 越狱Season 1-Episode 19: The Key
Season 1, Episode 19: The Key -Kellerman: WeusedtohaveaGreatDane, Dane: 丹麦大狗 我们以前有一只大丹犬 bigandwild. ...
- 【P1813】8的倍数
容斥原理,居然没想到……要补一下数论了 原题: 小x最近对数字8很感兴趣,有8进制,2008奥运会之类的.现在小x想知道,在[x,y]区间里,有多少个数能被8整除.小y觉得题目太简单,于是给出n个其他 ...
- java的nio之:java的nio系列教程之DatagramChannel
Java NIO中的DatagramChannel是一个能收发UDP包的通道.因为UDP是无连接的网络协议,所以不能像其它通道那样读取和写入.它发送和接收的是数据包. 打开 DatagramChann ...
- DuiLib——xml配置项
DUILibxml配置项 根节点 子类 属性 类型 Window |--------Image 图片信息 | o------name string | o------restype string | ...
- GPU优化方法[转]
CUDA优化的最终目的是:在最短的时间内,在允许的误差范围内完成给定的计算任务.在这里,“最短的时间”是指整个程序运行的时间,更侧重于计算的吞吐量,而不是单个数据的延迟.在开始考虑使用GPU和CPU协 ...
- eclipse cdt代码悬停窗口背景颜色设置(转载)
在eclipse中编写C++代码时,有一个很方便的功能,是当鼠标停放在某一个函数或变量上不同时,会出现一个悬停框,显示该函数或变量的声明 体.但是, 从Ubuntu 10.04之后,这个悬停框便出现了 ...