存储过程(procedure)
修改mysql结束符   delimiter name
procedure创建语法:
    create procedure procedureName();
    begin
        declare 声明变量
        sql语句
    end$
查看:procedure
    show procedure status;
调取:procedure
    call procedureName();
删除:drop procedure procedureName();
例1:带入变量
     create procedure p2()
     begin
     declare age int default 18;
     declare height int default 180;
     select concat('年龄',age,'身高',height);
     end$
例2:变量计算
      create procedure p3()
     begin
     declare age int default 18;
     set age:= age+20;
     select concat('20年后年龄',age);
     end$
例3:变量判断if
     create procedure p4()
     begin
     declare age int default 18;
     if age >=18 then
        select concat('成年');
     else
        select concat('未成');
     end if ;
     end$
例4:存储过程传参if
    /**
      存储过程的括号里,可以声明参数
      语法是[in/out/inout]参数名 参数类型
        in 传入
        out 输出
     **/
      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$
 
例5:存储过程  while 
    create procedure p6()
    begin
     declare total int default 0;
     declare num int default 0;
    while num < 100 do
        set num :=num+1;
        set total := total+num;
    end while;
    select total;
    end $
例6:存储过程中的传参类型 in  out
    create procedure p7(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 p7(100,@q)$
    select @q$
例7:存储过程 inout
    create procedure p8(inout age int)
    begin
        set age :=age+20;
    end$
    首先声明变量  set @age=20;
    调用存储过程  call p8(@age);
    查询结果      select @age;
例8:when case
    create procedure p9()
    begin
        declare pos int default 0;
        set pos := floor(5*rand());
        case pos 
        when 1 then select '飞';
        when 2 then select '劫持';
        when 3 then select '打击';
        end case;
    end$
例9: repeat
    create procedure p10()
    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$
 
例10:游标cursor  for
    create procedure p11()
    begin
         declare r_gid int;
        declare r_num int;
        declare r_name char(20);
        declare getgoods cursor for select gid,num,name from goods;
         
        open getgoods;
            fetch getgoods into r_gid,r_num,r_name;
            select r_gid ,r_num,r_name;
            fetch getgoods into r_gid,r_num,r_name;
            select r_gid ,r_num,r_name;
        close getgoods;
    end$
例11:游标cursor  for
    create procedure p12()
    begin
         declare r_gid int;    --声明变量    
        declare r_num int;     --声明变量
        declare r_name char(20);    --声明变量
         
        declare cnt int default 0;
        declare i int default 0;
 
        declare getgoods cursor for select gid,num,name from goods;  --声明游标
        select count(*) into cnt from goods;    --查询总条数赋值给变量cnt
        open getgoods;  --打开游标
           repeat        
            set i := i+1;
            fetch getgoods into r_gid,r_num,r_name;
            select r_gid ,r_num,r_name;
           until i>=cnt end repeat;
        close getgoods;  --关闭游标
    end$
例12:游标cursor  for
    /*
    declare [exit|continue|undo] handler for NOT FOUND set you := 0;
    声明是使用continue 将会出现最后一条数据重复  因为conticue 触发不回立即停止  
    反之exit立即停止
    undo 触发撤销最后一条    mysql 还不支持    
    */
    create procedure p13()
    begin
         declare r_gid int;    
        declare r_num int;     
        declare r_name char(20);    
        declare you int default 1;    
 
        declare getgoods cursor for select gid,num,name from goods;  
        declare exit handler for NOT FOUND set you := 0;
 
        open getgoods; 
           repeat        
            fetch getgoods into r_gid,r_num,r_name;
            select r_gid ,r_num,r_name;
           until you=0 end repeat;
        close getgoods;  
    end$
例13:游标cursor  for
    /*
    declare [exit|continue|undo] handler for NOT FOUND set you := 0;
    声明是使用continue 将会出现最后一条数据重复  因为conticue 触发不回立即停止  
    反之exit立即停止
    undo 触发撤销最后一条    mysql 还不支持    
 
    改变逻辑修复continue的最后一条数据重复问题  
    首先查询赋值在循环
    */
    create procedure p14()
    begin
         declare r_gid int;    
        declare r_num int;     
        declare r_name char(20);    
        declare you int default 1;    
 
        declare getgoods cursor for select gid,num,name from goods;  
        declare continue handler for NOT FOUND set you := 0;
 
        open getgoods; 
              fetch getgoods into r_gid,r_num,r_name;
          repeat
            select r_gid ,r_num,r_name;        
            fetch getgoods into r_gid,r_num,r_name;            
           until you=0 end repeat;
        close getgoods;  
    end$
例14: 
    create procedure p15()
    begin
         declare r_gid int;    
        declare r_num int;     
        declare r_name char(20);    
        declare you int default 1;    
 
        declare getgoods cursor for select gid,num,name from goods;  
        declare continue handler for NOT FOUND set you := 0;
 
        open getgoods; 
              fetch getgoods into r_gid,r_num,r_name;
          while you = 1 do
            select r_gid ,r_num,r_name;        
            fetch getgoods into r_gid,r_num,r_name;            
          end while;
        close getgoods;  
    end$

 

存储过程procedure的更多相关文章

  1. SQL Server 数据库的维护(一)__存储过程(procedure)

    --维护数据库-- --存储过程(procedure)-- --概述: SQl Serve的存储过程是由一个或多个T-SQL语句组成的一个集合.常用的程序代码段通常被创建成存储过程,一次创建多次调用, ...

  2. Oracle存储过程procedure in、out、in out 模式参数【不发布,纯转】

    Oracle存储过程procedure in.out.in out 模式参数 Oracle存储过程基本语法介绍 注意存过不会自动提交,需要在存过本身添加commit; rollback;等语句

  3. 数据库之存储过程Procedure

    数据库之存储过程 一.概述 SQLserver中视图通过简单的select查询来解决复杂的查询,但是视图不能提供业务逻辑功能,而存储过程可以办到. 二.什么是存储过程 存储过程procedure是一组 ...

  4. oracle 备份数据库对象(存储过程PROCEDURE,FUNCTION,VIEW,TRIGGER...)

    开发过程中,需要不停的备份数据库对象, 特别是存储过程, 每次手动备份不免很低能啊 历经几次修改终于, 完美了,O(∩_∩)O哈哈~      (当然,你也可以再改简便一点~~~) select db ...

  5. Oracle 存储过程 PROCEDURE

    存储过程  一组用于完成特定数据库功能的SQL语句集,该SQL语句集经过编译后存储在数据库系统中.在使用时候,用户通过指定已经定义的存储过程名字并给出相应的存储过程参数来调用并执行它,从而完成一个或一 ...

  6. Oracle 存储过程procedure之数据更新-游标

    在日常工作中,经常会碰到后台外导一批数据,并将外导数据处理至系统表中的情况. 面临这种情况,我一般采用写存储过程批处理的方式完成,写好一次以后,再次有导入需求时,只需要将数据导入到中间表,然后执行存储 ...

  7. Sqlserver2005:深入了解php执行sqlserver存储过程procedure:odbc_exe、odbc_execute

    以下存储过程(伪代码): -- 伪代码,假设相关操作是成功的 alter procedure pr_test as begin set nocount on update tab set col='n ...

  8. mysql存储过程(procedure)

    #创建带参数的存储过程 delimiter // ),out p int) begin ; end // delimiter call pro_stu_name_pass(@n,@p); select ...

  9. PL/SQL 05 存储过程 procedure

    --存储过程(不带参数) create or replace procedure 存储过程名as  变量.常量声明;begin  代码;end; --存储过程(带输入参数) create or rep ...

随机推荐

  1. C++ Windows进程管理

    功能: 1.各个进程启动.挂起.恢复.停止等 2.监听进程的运行状态,进程退出(正常.非正常)时,通知用户 3.异步队列 4.线程安全 进程管理器类: #ifndef __ProcessManager ...

  2. Schedule 学习

    现在做的项目都有用到Schedule,现在用一点时间来总结. 一.首先要到Nuget中下载Quartz.net. 二.下载下来了,你需要对它进行配置,使它能给你正常的使用. 三.在Global.asa ...

  3. 移动测试主要使用的测试框架,基于python

    1.uiautomator google自己的测试框架,可以跨应用测试, 语言支持java,python也可以在GitHub上找到封装的包,去调用uiautomator. 2.Robotium jav ...

  4. 长时间停留在calculating requirements and dependencies 的解决方案

    如果Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 ) 这个问题通常就是在点击安装之后显示“Calculating ...

  5. nodejs渲染模板

    为什么要用nodejs来渲染? 之前前端的任务就是用HTML+CSS 来高保真的还原UI所设计原图,偶尔会使用少量的jq来对页面添加一些特效,页面还要交付给后端开发人员进行数据填充(php jsp)等 ...

  6. Activity packagename has leaked window android.widget.PopupWindow$PopupDecorView{4f92660 V.E...... .......D 0,0-455,600} that was originally added here

    原因是在销毁Activity时,Activity中的popupwindow还处于显示状态. 解决方法是重写Activity的onDestroy()方法,在Activity销毁前调用popupWindo ...

  7. linux /boot 清理

    随着升级 /boot分区会越来越满 导致后续无法升级  原因是因为每次升级有可能升级内核  但是旧的内核没有删除  所以导致/boot一直增大 解决办法就是删除不需要的内核,一下步骤: 查看所有安装的 ...

  8. 如何开发H5项目 -- 入门篇

    前言 H5即HTML5,H5开发具有低成本.高效率.跨平台.研发周期短,用户接触成本低等特性. 一.开发环境 在开发一个H5项目之前,需要先搞好环境.主要有node.npm.gulp.bower.下面 ...

  9. How to connect to Linux VM from Windows

  10. iOS信号量的使用

    Core Audio render thread and thread signalling up vote2down votefavorite   Does iOS have any kind of ...