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存储过程简明使用的更多相关文章

  1. MySQL存储过程(转)

    一.MySQL 创建存储过程 "pr_add" 是个简单的 MySQL 存储过程,这个存储过程有两个 int 类型的输入参数 "a"."b" ...

  2. MySql存储过程

    MySQL 存储过程 ```sql CREATE PROCEDURE myprocedure (IN para01 INTEGER) BEGIN DECLARE var01 CHAR(10); IF ...

  3. mysql存储过程和存储函数

    mysql存储过程和存储函数 存数函数代码示例: DROP PROCEDURE IF EXISTS calc_ci_day_suc_rate; delimiter // CREATE FUNCTION ...

  4. mysql存储过程编写-入门案例-遁地龙卷风

    (-1)写在前面 这篇文章只是简要的叙述了mysql存储过程编写的基本概念. 我使用的mysql版本是5.7.9-log. 参照<<深入浅出MySQL>>. (0) delim ...

  5. MySQL存储过程动态SQL语句的生成

    用Mysql存储过程来完成动态SQL语句,使用存储过程有很好的执行效率: 现在有要求如下:根据输入的年份.国家.节假日类型查询一个节假日,我们可以使用一般的SQL语句嵌入到Java代码中,但是执行效率 ...

  6. MySQL 存储过程

    MySQL 存储过程 存储过程是通过给定的语法格式编写自定义的数据库API,类似于给数据库编写可执行函数. 简介 存储过程是一组为了完成特定功能的SQL语句集合,是经过编译后存储在数据库中. 存储过程 ...

  7. mysql存储过程详解

    mysql存储过程详解 1.      存储过程简介   我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的S ...

  8. PHP调用MYSQL存储过程实例

    PHP调用MYSQL存储过程实例 标签: mysql存储phpsqlquerycmd 2010-09-26 11:10 11552人阅读 评论(3) 收藏 举报 实例一:无参的存储过程$conn = ...

  9. mysql存储过程语法及实例

    存储过程如同一门程序设计语言,同样包含了数据类型.流程控制.输入和输出和它自己的函数库. --------------------基本语法-------------------- 一.创建存储过程cr ...

随机推荐

  1. 计算机中如何表示数字-07IEEE754浮点数标准

    由于不同机器所选用的基数.尾数位长度和阶码位长度不同,因此对浮点数的表示有较大差别,这不利于软件在不同计算机之间的移植.为此,美国IEEE(电器及电子工程师协会)提出了一个从系统角度支持浮点数的表示方 ...

  2. 《C标准库》——之<stddef.h>

    <stddef.h>,顾名思义,就是标准定义.C语言里这个标准库里定义了一些类型,和宏定义. <stddef.h>的内容: 类型: ptrdiff_t : 是两个指针相减的结果 ...

  3. gradle默认路径

    C:\Users\wango\.gradle\wrapper\dists\gradle-2.2.1-all\c64ydeuardnfqctvr1gm30w53

  4. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  5. URAL 1076 Trash Trash(最大权匹配)

    Trash Time limit: 1.0 secondMemory limit: 64 MB You were just hired as CEO of the local junkyard.One ...

  6. Linux系统编程@终端IO

    Linux系统中终端设备种类  终端是一种字符型设备,有多种类型,通常使用tty 来简称各种类型的终端设备.终端特殊设备文件一般有以下几种: 串行端口终端(/dev/ttySn ) ,伪终端(/dev ...

  7. javaweb在线预览

    需要工具: 1.openoffice 将word.excel.ppt.txt等文件转换为pdf文件 2.SWFTool 将pdf转换为swf文件 3.flexPaper是一个开源轻量级的在浏览器上显示 ...

  8. Unity Meshes

    1. Unity 没有自带建模工具 2. 导入 Mesh 时,Unity 会自动寻找所引用的纹理,查找文件夹名为 Textures 的.先在本目录下找 -> 上溯在parent查找 ==> ...

  9. c程序代码优化的一些方法

    我认为一个好的用于科学计算的程序代码应该:算法漂亮精妙,程序简洁易懂,运算快速,节省内存.这里有的地方是矛盾的,比如简洁vs易懂,时间vs空间,找个平衡吧.目前来看时间要比空间宝贵一些.写程序分几步: ...

  10. ID3算法 决策树的生成(2)

    # coding:utf-8 import matplotlib.pyplot as plt import numpy as np import pylab def createDataSet(): ...