mysql存储过程

mac2022-06-30  26

#第1个存储过程,体会"封装sql"delimiter $drop procedure if exists p1;$ #删除存储过程create procedure p1()#创建存储过程begin select * from rooms;end;$call p1();$#调用存储过程#第2个存储过程,体会"参数"delimiter $drop procedure if exists p2;$create procedure p2(n bigint(20))begin select * from rooms where id>n;end;$call p2(10050);$#第3个存储过程,体会“控制结构”delimiter $drop procedure if exists p3;$create procedure p3(n bigint(20),j char(1))begin if j='h' then #存储过程的If判断语句 select * from rooms where id>n; else select * from rooms where id<n; end if;#这里必须要用end if结束end;$call p3(10050,'h');$call p3(10050,'d');$#第4个储存过程,体会"循环语句" (计算1-n的和)delimiter $drop procedure if exists p4;$create procedure p4(n smallint)begin declare i int;#存储过程定义变量 declare s int; set i=1;#存储过程给变量初始值 set s=0; while i<=n do set s=s+i; #这里存储过程赋值也必须要用set,不能用+= set i=i+1; end while;#这里循环也必须要用end结束 select s;end;$call p4(200);$

 

转载于:https://www.cnblogs.com/hww9011/p/4218719.html

最新回复(0)