zoukankan      html  css  js  c++  java
  • procrdure存储过程

    /*
    存储过程
    
    在一些语言中,有一个概念叫”过程“ procedure,和”函数“ function
    
    过程:封装了若干条语句,调用时,这些封装体执行
    函数:是一个有返回值的“过程”
    过程:没有返回值的函数
    
    我们把若干条sql封装起来,起个名字---过程
    把此过程存储在数据库中---存储过程
    
    */
    
    -- 创建一个简单的存储过程
    create procedure p1()
    begin
    select 1+2 ;
    end;
    
    --查看已有的procedure
    show procedure status;
    
    --调用存储过程
    call p1();
    
    --存储过程引入变量和控制结构
    /*
    存储过程是可以编程的
    意味着可以使用变量,表达式,控制结构来完成复杂的操作
    在存储过程中使用declare声明变量
        格式 declare 变量名 变量类型 [default 默认值]
    只是简单的声明输出变量值不能满足我们的要求,我们喜欢用变量做一些运算,如:+*-/
    运算的结果,如何复制给变量呢?
        set 变量名 := exp
      select num into rnum from goods where gid=new.gid;
    select count(*) from goods into countData;//这样可以赋值给countData变量
    控制结构: if/else语句:格式: if condition then statement else statement end if; if/elseif/else语句:格式: if condition then statement elseif condition then statement ... end if; --给存储过程传参 存储过程的括号里,可以声明参数 语法是: [in/out/inout] 参数名 参数类型
    */ --使用变量的存储过程 create procedure p2() begin declare height int default 180; declare age int default 18; select concat('年龄',age,'身高',height); end; call p2(); --加入运算 create procedure p3() begin declare age int default 18; set age=age+20; select concat('20年后这么大了:',age); end; call p3(); --加入控制语句 create procedure p4() begin declare age int default 18; set age=age+20; if age<12 then select '未成年'; else select '成年人'; end if; end; call p4(); --使用简单的参数 create procedure p5(width int,height int) -- 求面积 begin select concat('你的面积是',width*height) as area; if width>height then select '你很胖'; elseif width<height then select '你很瘦'; else select '你挺方'; end if; end; call p5(4,5); /* 循环语句 while循环 */ create procedure p6() begin declare total int default 0; declare num int default 0; while num<100 do set total :=total+num; set num :=num+1; end while; select total; end; call p6(); --1~n的和 create procedure p7(num int) begin declare total int default 0; while num>0 do set total :=total+num; set num :=num-1; end while; select total; end; call p7(3);
    /*
    输出型参数 in out inout
    默认使用的是in型,in型就是简单的接收参数,然后在过程中使用。无法向外界返回
    三种类型的区别:
            in:将变量或者常量作为参数传递进入存储过程,但是无法返回。存储过程外的原来的变量的值不会改变。相当于java中的方法,参数是基本数据类型
            out:将变量作为参数传入,如果原来的变量有值,将值变为null传入,相当于参数只是声明的变量。返回经过存储过程中存储的该变量的值在对应的out变量上。out类型的变量类似于成员变量
            inout:inout兼并in和out的特点。只能传入变量,但是原来变量的值可以传入进来而不会清除为null,仍然保留原来的值,和in效果一样。结束存储过程后out变量的值也改变了,也相当于成员变量
    */
    create procedure p8(in n int)
    begin
    declare total int default 0;
    
    while n>0 do
    set total := total+n;
    set n :=n-1;
    end while;
    
    select total;
    end;
    
    call p8(10);
    --传入变量值也可以
    set @n :=10;
    call p8(@n);
    
    drop procedure p9;
    --使用out型变量
    create procedure p9(in n int,out total int)
    begin
    declare num int default 0;
    select total;
    while num<n do
    set num :=num+1;
    set total :=total+num;
    
    end while;
    end;
    
    call p9(100);-- 参数必须一一对应
    call p8(10,8); --这个也报错,out型的指的是过程要输出的变量,一定是变量而不是常量
    --传入变量试试
    set @n :=10;
    call p9(10,@n);
    select @n;
    -- 经过测试,out型参数传入的变量前面会自动将值变成null,null值具有特殊性,沾到什么什么变null。所以输出的就是null了。希望得到0~n累加的结果,先修改存储过程给变量一个初值。
    drop procedure p9;
    create procedure p9(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;
    
    call p9(10,@a);
    select @a;
    -- out类型的参数,我的理解是如果存在将原来的分配的值的地址删掉,变为null,如果不存在声明一个变量。在存储过程中对变量操作。该变量类似于成员变量。
    
    -- 第三种参数类型:inout,inout和out的区别在于inout可以得到前面输入的参数的值而不会抹去变为null
    
    create procedure p10(in n int,inout total int)
    begin
    declare num int default 0;
    while num<n do
    set num :=num+1;
    set total :=total+num;
    
    end while;
    end;
    
    set @n:=10;
    call p10(10,8);--报错,类型错误,与out有关就只能是参数为变量
    call p10(10,@n); -- 成功。并且累加到@n上面了
    select @n;
    
    create procedure p11(in n int) --测试in
    begin
         set n:=n+10;
         select n;
    end;
    
    call p11(@n);
    select @n;
    -- case语句
    create procedure p12()
    begin
         declare pos int default 0;
         set pos := floor(4*rand());
         case pos
         when 1 then select '在飞';
         when 2 then select '掉到海里去了';
         when 3 then select '在小岛上';
         else select '我也不知道在哪';
         end case;
    end;
    call p12();
    
    --repeat 循环
    /*
    repeat
    sql statement;
    sql statement;
    until ciondition end repeat;
    我感觉它好像for循环
    */
    drop procedure p13;
    create procedure p13(n int)
    begin
         declare i int default 0;
         declare cott int default 0;
         repeat
               set cott :=cott+ i;
               set i :=i+1;
         until i> n end repeat;
         select cott;   
    end;
    call p13(3);
  • 相关阅读:
    2014上半年-学习目录
    c++中智能输出文件
    如何在微博侧栏中加入自己的微博[js]
    oracle数据库性能
    Arcgis for Android 空间数据WKT与JSON描述
    echart 折线图、柱状图、饼图、环形图颜色修改
    Echarts横坐标倾斜,顶部显示数字
    解决svn中“工作副本已经锁定”,或者svn清理失败的解决方法
    Oracle 空间查询, 数据类型为 sdo_geometry
    OSS上无法使用字体文件解决方案
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/5598945.html
Copyright © 2011-2022 走看看