MYSQL-5-存储过程

不想你离开。 提交于 2019-11-26 22:28:14
  1. 变量

    1. 全局变量前有@@字符,由系统定义和维护

       select @@rowcount;--返回上一条语句执行的条数
       select @@version;--返回当前SQL Server服务器的日期版本和处理器类型
      
    2. 局部变量前有@字符,由用户定义和维护

  2. 创建函数(了解)

     use school2;
     delimiter $$
     create function fun_get_sage(str varchar(12))
     returns char(12)
     begin
     	return(select sage from student where sname=str);
     end;
    

    调用:

     select fun_get_sage('李彤');
    
  3. 存储过程

    1. 语句

       if...then...
       else if...then...
       else...
       end if;
      
       label:while... do
             
             end while;
       
       label:loop
             end loop;
      
       case
       when...then...
       when...then...
       else...
       end case;
      
    2. 实例一

       delimiter $$
       create procedure pro_test2()
       begin
       	select "ok";
       end $$
       
       call pro_test2();
      
    3. 实例二

       delimiter $$
       create procedure pro_test21(in n int)
       begin
       	select n*n;
       end $$
       
       call pro_test21(20);
      
    4. 实例三

       delimiter $$
       create procedure pro_test22(in n int,out m int)
       begin
       	set m=n*n;
       end $$
       
       set @a=30;
       set @b=0;
       call pro_test22(@a,@b);
       select @b;
      
    5. 实例四

       delimiter $$
       create procedure pro_test23(inout n int)
       begin
       	set n=n*n;
       end $$
       
       set @a=10;
       call pro_test23(@a);
       select @a;
      
    6. 删除一个学生

       delimiter $$
       create procedure pro_test24(in strname varchar(12))
       begin
       	declare num varchar(12) default '';
       	select sno into num from student where sname=strname;
       	delete from sc where sno=num;
       	delete from student where sno=num;
       	select '删除成功';
       end $$
       
       call pro_test24('大傻');
      
    7. 从0加到num

       delimiter $$
       create procedure pro_loop(inout num double)
       begin
       	declare total double default 0;
       	declare i int default 0;
       	while i<num do
       		set total=total+i;
       		set i=i+1;
       	end while;
       	set num=total;
       end $$
       
       set @n=100;
       call pro_loop(@n);
       select @n;
      
    8. 从0加到num,若和大于2000,则停止

       delimiter $$
       create procedure pro_loop2(inout num double)
       begin
       	declare total double default 0;
       	declare i int default 0;
       	
       	label:while i<num do
       					set total=total+i;
       					if total>2000 then
       							leave label;
       					end if;
       					set i=i+1;
       				end while;
       				set num = total;
       end $$
       
       set @n=100;
       call pro_loop2(@n);
       select @n;
      
    9. 查询 李大奎 c语言的成绩

       DELIMITER $$
       create  procedure pro_getGrade(in name VARCHAR(12),in cname VARCHAR(12),out res INT)
       COMMENT '查询成绩'
       BEGIN
       	DECLARE xuehao varchar(12) DEFAULT '';
       	DECLARE kehao varchar(12) DEFAULT '';
       
       	select sno into xuehao from student where sname=name limit 1;
       	select cno into kehao from course where cname=cname limit 1;
       	select grade into res from sc where sno=xuehao and cno=kehao;        
       END $$
      
       -- 调用:
       set @r = 0;
       call pro_getGrade('李大奎','c',@r);
       select @r;
      
    10. 分数每次增加num直到没有不及格的,当超过100,则为100

       delimiter $$
       create procedure pro_loop_add(in num int)
       begin
       	declare min_grade int default 0;
       	select min(grade) into min_grade from sc_copy;
       	label:while min_grade<60 do
       					update sc_copy set grade=grade+num;
       					select min(grade) into min_grade from sc_copy;
       				end while;
       	update sc_copy set grade=100 where grade>100;
       end $$
       
       -- 调用:
       call pro_loop_add(5);
      
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!