mysql基础四 存储过程
admin
2023-05-02 14:04:33
0

一、存储过程:变量的声明和赋值。
delimiter $

create procedure p1()

begin

declare age int default 18;

    set age :=age+20;或者   set age =age+20

select age from dual;

end$

调用存储过程:call p1(); 其结果是如下:

mysql基础四 存储过程

二、存储过程:if 控制语句。
delimiter $
create procedure p2()
begin

declare age int default 18;
if age>=18 then
 select '已成年' ;
else 
 select '未成年';
end if;

end$
调用存储过程:call p2(); 其结果是如下:
mysql基础四 存储过程

三、存储过程:输入参数。
计算一个矩形的面积,并判断是胖fat? 瘦then? 还是方square?
delimiter $
create procedure p3(w int ,h int)
begin
select concat('area:',w*h);

if w > h then
  select 'fat';
elseif w < h then
  select 'then';    
else 
  select 'square';
end if;

end$

四、存储过程:while循环

求1到100的和。
delimiter $
create procedure p100()
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$

调用存储过程:call p100(); 其结果是如下:
mysql基础四 存储过程

五、存储过程:输出参数:
求1到n的和。
delimiter $
create procedure p8(in n int ,out total int)
begin
set total=0;
declare num int default 0;
while num<=n do

set total=total+num;
set num=num+1;

end while;

end$

调用存储过程:call p8(100,@sumary); select @sumary; 其结果是如下:

mysql基础四 存储过程

mysql基础四 存储过程

六、存储过程:输入输出参数:
delimiter $
create procedure p9(inout age int)
begin
set age =age+20;
end $

调用存储过程:set @currentage=18; call p9(@currentage) ;select @currentage; 其结果是如下:

mysql基础四 存储过程

七、存储过程:case控制语句:
delimiter $
create procedure p10()
begin

declare pos int default 0;

set pos=floor(4*rand()) ;

case pos
when 1 then select 'haha';
when 2 then select 'hehe';
else select 'heihei';
end case;

end $

八、存储过程:repeat控制语句:
delimiter $
create procedure p11()
begin

declare i int default 0;
declare total int default 0;

repeat 
    set i=i+1;
    set total =total +i;
until i>=100 end repeat;
select total;

end$

九、调用存储过程 call:

call procedure_Name();

相关内容

热门资讯

卖“毒蛋”的人,抓到了 作者 | 何国胜 编辑 | 向现“(人)抓到了,目前案件正在侦办中。”7月28日晚间,苏州禁毒部门有...
尺素金声丨实施零关税国家达63... 海关总署发布的数据显示,今年5月1日起,我国对53个非洲建交国全面实施零关税举措,目前,我国实施零关...
职业索赔盯上基层诊所,倒逼用药... 文 | 布丁基层诊所正在被职业索赔盯上。据新京报,去年夏天,一男子走进河南南阳一家诊所,要求购买三瓶...
“西瓜我全买了”就可以肆意妄为... 拿西瓜砸了人,把瓜都买了,就能一走了之吗?事实证明,这套逻辑在法治社会行不通。7月28日晚,据海峡都...
科学家在日本广岛发现新物质,系... 在美国对日本广岛进行原子弹轰炸近81年后,科学家们在广岛的沙滩上发现了一种奇异且从未被发现过的新物质...
AI失控,反噬开始 作者 | 贺一 编辑 | 阿树近期,中国开源模型在美国频繁引发热议。7月28日,月之暗面发布Kimi...
“总统千金天价离婚”,分到43... 2026年7月24日下午,首尔高等法院,一场持续近十年的司法拉锯战终于接近尾声。法庭裁定SK集团会长...
巴基斯坦,又拿下一个历史性协议 全世界都没想到,接连的中东大战,巴基斯坦正成为最大赢家。去年以色列追杀哈马斯,空袭卡塔尔首都,阿拉伯...
汇正财经贺峰的一对一指导服务怎...   对于考虑购买证券投资顾问服务的投资者来说,'一对一指导服务怎么样'是一个重要的考量维度。需要首先...
重庆失联00后网格员龚宝冬确认...   重庆失联00后网格员龚宝冬确认遇难  【重庆失联00后网格员龚宝冬确认遇难】2026年7月29日...