• Mysql 学习笔记06


    1 Mysql 存储过程的介绍:

            过程  : 封装了若干条语句,调用时,这些封装体执行。

            函数  : 是一个没有返回值得过程。

             过程 : 没有返回值的 函数。

        我们把若干条sql 封装起来,起个名字----过程。

        把此过程存储在数据库中------存储过程。 

      1 创建语法:

        create procedure procedurName()

         begin

           ---------sql 语句

         end;

         例如 : create procedure helle()

                      begin

                       select   'hello word' from dual;

                       end;

       2 查看 已有的过程  show procedure status;

      

       3 如何调用? call  procedure();

       4 存储过程是可以编程的,意味着可以使用变量,表达式,控制结构来完成复杂的功能。

       

        5 .. 求 0到100 的和,使用 while ( )循环实现

          create procedure p()

          begin

              declare total int default 0;

              declare num int default 0;

              while num <100 do

              set num:=num+1;

              set total := total +1;

              end whileo;

             select total;

            end;

    6 有参数的 例子 in/out

      输入参数

        create procedure p7(in n int)
    BEGIN
    declare total int default 0;
    declare num int default 0;
     while num <n DO
       set num:=num+1;
       set total :=total+num;
    end while;
    select total;
    end;

    drop procedure p7;

       call p7(10);

       输出参数

    create procedure p8(in n int,out total int)
    begin
      declare num int default 0;
      set total :=0;
        while num < n DO
            set num:=num+1;
            set total := total+num;
         end while;
     
    end;

     calll p8(100,@aa) + select @aa;  // 输入输出

     注意,一定要初始化参数,否则结果有误。

     inout  类型  例子

         create procedure p9(inout age int) 

          begin

             set age:=age+20;

         end;  使用时 需要先初始化参数  set @aa = 18; 然后 call p9(@aa)  然后查询结果 select @aa

    7  分支结构 case 的用法

         create procedure p10()

    begin

            declare pos int default 0;

           set  pos := floor(5* rand());

             case pos

                  when 1 then select ' stilll flying';

                  when 2 then select 'fall in sea';

                  when 3 then select 'in the island';

                  else select ' i dont know ';

                  end case;

    end;

    8 ----repeat 循环

      create procedure p11()

          begin

            declare total int default 0;

            declare i int default 0;

             repeat

              set i:= i+1;

              set total :=total+i;

             until i>=100 end repeat;

        select total;

    end;

  • 相关阅读:
    利用.Net的CLR和BCL实现函数IsNumeric
    30岁前挣到10万年薪 五位年轻人的高薪秘诀
    三级关链菜单通用版
    对Session和Cookie的区分与理解
    转贴:C#排序算法大全
    无效的 CurrentPageIndex 值.它必须大于等于 0 且小于 PageCount!的问题
    ASP.NET中“检测到有潜在危险的表单值”的解决方法
    如何让网页版面更适合浏览者呢?这里有技巧
    十二星座情侣恋爱积分
    asp.net 三种基本的参数传递方法(web forms)
  • 原文地址:https://www.cnblogs.com/wjgbok/p/10795792.html
Copyright © 2020-2023  润新知