zoukankan      html  css  js  c++  java
  • 数据库-存储过程

     ###############    存储过程    ##############

    """
    3.存储过程
    那么什么是存储过程呢?怎么创建、查看和删除存储过程呢?存储过程有什么优点?
    存储过程:类似于函数(方法),简单的说存储过程是为了完成某个数据库中的特定功能而编写的语句集合,
    该语句集包括SQL语句(对数据的增删改查)、条件语句和循环语句等。
    
    1. 查看现有的存储过程
    show procedure status;
    2 .删除存储过程
    drop procedure 存储过程名称;
    3. 调用 存储过程
    call 存储过程名称(参数入/出类型 参数名 数据类型);
    
    #############################
    4.创建存储过程
    # 1.体会封装
    create procedure p1 ()
    begin
        select * from account;
    end
    
    # 2, SQL 体会参数
    create procedure p2(in i int,out n varchar(50))
    begin
     select name into n from account where id = i;
    end
    
    -- 调用
    set @name =null;
    CALL p2(1,@name);
    select @name;
    注意1: mysql中有三种出入参数类型:分别为:1. in 入参类型  2.out 出参类型   3. inout 出入参类型
    注意2: into 关键字 可以 将前面字段的查询结果 执行 给 into 后面的变量.
    
    #3.SQL 体会控制
     create procedure p3(in x int,in c char(1))
     begin
        if c ='d' then
             select * from account where money >x;
       else
             select * from account where money <x;
      end if;
    end
    
    #4.体会循环:计算1-100累加的和,并且返回计算结果.
    create procedure p4(inout n int)
    begin
          DECLARE sum int default 0; -- 设置总和变量,并且指定初始值0
          declare i int; -- 声明变量
          set i = 0;    -- 通过set为变量设置值
        while i<=n DO  -- 开始循环
                set sum = sum +i;
                set i = i+1;
          end while; -- 结束循环
    
        select sum; -- 提供结果
    
         set n = sum;--将计算结果提供给 输出变量 n;
    end;
    
     -- 调用:
     set @n = 100;
     call p4(@n);
     select @n;
    
    
    """
  • 相关阅读:
    Mybatis学习(2)原始dao开发和使用mapper接口代理开发
    Mybatis学习(1)
    Leetcode | Merge Intervals
    GDB打印STL容器内容
    LeetCode | Max Points on a Line
    最长不减子序列【转】
    LeetCode | Evaluate Reverse Polish Notation
    LeetCode | Word Ladder II
    LeetCode | Valid Number
    LeetCode | Set Matrix Zeroes
  • 原文地址:https://www.cnblogs.com/andy0816/p/12635670.html
Copyright © 2011-2022 走看看