上一章mysql-存储过程(一),写了一个最简单的例子。现在学习下具体的内容,相信看此文章的都是程序员

,我就用最简单的方式说明下,太多的文字反而不好

 

1.局部变量声明

declare num int; declare 名称 类型

 

2.使用set赋值

set num =4;

 

3.使用select...into 语句,把选定列的值直接存储到变量中

declare name;

declare age;

select username,userage into name,age from user where id=1000

注意:使用select...into返回结果只能是一行

 

4.IF语句

delimiter $$
create procedure iftest(in num1 int,in num2 int ,out res char(6))
begin
   if num1>num2 then set res='dayu';
   elseif num1=num2 then set res='dengyu';
   else set res='xiaoyu';
   end if;
end$$
delimiter ;

mysql-存储过程(二)
注意:elseif num1=num2,elseif是连在一起的,等号比较是用"=",而不是"=="

 

 

5.case语句

......
create procedure casetest(in num int,out sex char(10))
begin
case num
    when 1 then set sex='man';
    when 2 then set sex='woman';
    else set set='error';
end
.......
 

 

 

相关文章:

  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
  • 2022-12-23
  • 2021-09-17
猜你喜欢
  • 2022-12-23
  • 2021-11-18
  • 2021-10-17
  • 2021-06-23
相关资源
相似解决方案