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.创建存储过程

  1. create procedure p1()
  2. begin
  3. select * from table;
  4. end;
  5.  
  6. call p1();
  7.  
  8. create procedure p2(n int)
  9. begin
  10. select * from table where num>n;
  11. end;
  12.  
  13. call p2(2);
  14.  
  15. create procedure p3(n int,j char(1))
  16. begin
  17. if j='a' then
  18. select * from table where num>n;
  19. else
  20. select * from table where num<n;
  21. end if;
  22. end;
  23.  
  24. call p3(2,'a');
  25.  
  26. #计算1->n的和
  27. create procedure p4(n smallint)
  28. begin
  29. declare i int;
  30. declare sum int;
  31. set i = 1;
  32. set sum = 0;
  33. while i <= n do
  34. set sum = sum+i;
  35. set i = i+1;
  36. end while;
  37. select sum;
  38. end;
  39. 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. 240. Search a 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  2. c 函数及指针学习 7

    1.结构的存储分配 1 2 printf("%d \n",sizeof(char)); printf("%d \n",sizeof(int)); int 类型为 ...

  3. Chrome A标签的迁移错误:【Error loading page】

    在IE中经常使用A标签用来迁移,正确的写法是 <a href="001.html"></a>即可,不过在chrome上面可能会引发错误无法迁移. 比如用下面 ...

  4. leetcode 96 Unique Binary Search Trees ----- java

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  5. php随笔杂记(一)

    1.在function updatepwd($postData=array())   如果参数是一个数组, 在使用时,如果给他赋值则只返回数组名$postData即可  ,如果里面已有值 ,这返回的可 ...

  6. 论文笔记之:Active Object Localization with Deep Reinforcement Learning

    Active Object Localization with Deep Reinforcement Learning ICCV 2015 最近Deep Reinforcement Learning算 ...

  7. RandomAccessFile

    RandomAccessFile是用来访问那些保存数据记录的文件的,你就可以用seek( )方法来访问记录,并进行读写了.这些记录的大小不必相同:但是其大小和位置必须是可知的.但是该类仅限于操作文件

  8. PHP-关于$_SERVER

    类似于Nginx中的请求头,所有header,都可以使用 $http_xxx来使用,比如$http_accept,甚至包括自定义的,比如,$http_x_forwarded_host proxy_se ...

  9. unity3d用鼠标拖动物体的一段代码

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 这是一段拖动物体的代码,比较简洁明了,对了解uni ...

  10. Ubuntu 安装和配置redis数据库

    Ubuntu 14.04下安装和配置redis数据库 小编现在在写一个分布式爬虫,要用到这个数据库,所以分享一下小编是如何安装和配置的,希望对大家有帮助. 工具/原料   Ubuntu 系统电脑一台 ...