• MYSQL高级第一天(3)


    存储过程和函数:

    一、定义:

      存储过程和函数是事先经过编译并存储在数据库中的一段SQL语句的集合,调用存储过程和函数可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提高数据处理的效果是有好处的。

    存储过程和函数的区别在于函数必须有返回值,而存储过程没有

    函数:是一个有返回值的过程;

    过程:是一个没有返回值的函数;

    二、语法:

    1、创建:

    delimiter $;
    create procedure pro_test1()
    begin
        select 'Hello';
    end$
    

    2、查看:

    --方法一
    select name from mysql.proc where db='db_name';
    
    --方法二
    show procedure status\G;
    
    --方法三
    show create procedure pro_test1\G;
    

    3、调用:

    call pro_test1();
    

    4、删除:

    drop procedure pro_test1;
    

    二、语法结构:

    1、变量:

    声明:declare

    delimiter $;
    create procedure pro_test1()
    begin
        declare num int default 0;
        select concat('num的值为:',num);
    end$
    

    赋值(set 或者 select into):

    delimiter $;
    create procedure pro_test2()
    begin
        declare num int default 0;
        set num=num+10;
        select concat('num的值为:',num);
    end$
    

      

    delimiter $;
    create procedure pro_test3()
    begin
        declare num int default 0;
        select count(*) into num from city;
        select concat('city表中的记录数为:',num);
    end$

    call pro_test3();

    三、流程控制:

    1、if条件判断:

    delimiter $;
    create procedure pro_test4()
    begin
        declare height int default 175;
        declare description varchar(50) default '';
        if height>=180 then
            set description="高挑";
        elseif heihgt>=170 then
            set description="标准";
        else
            set description="一般";
        select concat('身高为:',height,'对应的身材类型为:',description);
        end if;
    end$
    

    @会话变量,@@系统变量

    set @name='itcast';

    select @name;

    传参:

    mysql>delimiter $;
    ->create procedure pro_test6(in height int,out description varchar(10))
    ->begin
    ->   if height>=180 then
    ->       set description='高';
    ->    elseif height>=170 then
    ->        set description='中';
    ->    elseif height>=160 then
    ->        set description='低';
    ->   end if
    
    mysql>call pro_test6(175,@description)$
    
    mysql>select @descriptioin$
    

      

    2、case语法:

    delimiter $;
    create procedure pro_test7(mon int)
    begin
        declare result varchar(10) default '';
        case
            when mon>=1 and mon<=3 then
                set result='第一季度';
            when mon>=4 and mon<=6 then
                set result='第二季度';
            when mon>=7 and mon<=9 then
                set result='第三季度';
            when mon>=10 and mon<=12 then
                set result='第四季度';
        end case;
        select concat('传递的月份为:',mon,‘,属于’,result);
    end$
    

    3、while循环:

    delimiter $;
    create procedure pro_test8(n int)
    begin
        declare total int default 0;
        declare num int default 1;
        while num<=n do
            set total=total+num;
            set num=num+1;
        end while;
        select total;
    end$
    

    4、repeat循环

    delimiter $;
    create procedure pro_test9(n int)
    begin
        declare total int default 0;
        repeat
             set total=total+n;
             set n=n-1;
             until n<0
        end repeat;
        select total;
    end$
    

      

    5、loop循环

    delimiter $;
    create procedure pro_test10(n int)
    begin
        declare total int default 0;
        c:loop
            set total=total+n;
            set n=n-1;
            if n<0 then
                leave c;
            end if;
         end loop c;
        select total;
    end$
    

    四、游标、光标:

    语法:

    delimiter $;
    create procedure pro_test11()
    begin
        declare e_id int default 0;
        declare e_name varchar(50) default '';
        declare e_age int default 0;
        declare e_salary int default 0;
        declare has_data int default 1;
        
        declare emp_result cursor as select * from emp;
        open emp_result;    
    
        fetch emp_result int e_id,e_name,e_age,e_salary;
        select concat('id=',e_id,',name=',e_name,',age=',e_age,',salary=',e_salary);
        select concat('id=',e_id,',name=',e_name,',age=',e_age,',salary=',e_salary);
        close emp_result;
    end$
    

      

    循环读取游标:

    delimiter $;
    create procedure pro_test11()
    begin
        declare e_id int default 0;
        declare e_name varchar(50) default '';
        declare e_age int default 0;
        declare e_salary int default 0;
        declare has_data int default 1;
        
        declare emp_result cursor as select * from emp;
        declare exit handler for not found set has_data=0;
        open emp_result;
        
        repeat
        fetch emp_result int e_id,e_name,e_age,e_salary;
        select concat('id=',e_id,',name=',e_name,',age=',e_age,',salary=',e_salary);
        until has_data=0
        end repeat;
        close emp_result;
    end$
    

      

    五、存储函数:

    语法:

    --创建
    delimiter $;
    create function fun1(countryId int)
    returns int
    begin
        declare cnum int;
        select count(*) into cnum from city where country_id=countryId;
         return cnum;
    end$
    
    select fun1(1)$
    

      

    --删除
    drop function fun1$
    

      

  • 相关阅读:
    vi编辑器命令大全
    Ubuntu环境搭建svn服务器
    Visual Studio中“后期生成事件命令行” 中使用XCopy命令
    解决 Visual Studio For Mac 还原包失败问题
    [Win10应用开发] 如何使用Windows通知
    [Win10应用开发] 使用 Windows 推送服务
    如何使用 PsExec 执行远程命令
    如何在调试Window App时,触发 Suspending ,Resuming 等事件
    如何 “解决” WPF中空域问题(Airspace issuse)
    浅谈可扩展性框架:MEF
  • 原文地址:https://www.cnblogs.com/haokan/p/16459781.html
Copyright © 2020-2023  润新知