• 多测师课堂011_mysql之存储过程(4)高级讲师肖sir


    存储过程 (procedure)


    存储过程是一组完成特定功能的sql 语句的集合,通过编译后存储在数据库中,通过存储过程的名称进行调用。可以反复的调用执行
    存储过程=sql语句集合+控制语句

    存储的优点:
    1、存储创建后,可以反复调用和使用,不需要重复写复杂的sql语句
    2、创建和修改存储过程不会对数据有任何影响
    3、存储过程可以通过输入的参数,返回值
    4、通过存储过程中 可以加入控制语句,可以让sql语句更灵活

    mysql5.0版本以后才开始支持存储过程


    4.创建一个存储过程
    delimiter // #分隔符
    create procedure 存储过程名称([in|out|inout]参数名,数据类型)

    begin #开始
    存储过程体(sql语句)
    end// #结束
    delimiter: 分隔符,分界符,这里指定的分隔符是//,可自定义create procedure 存储过程名称() 创建一个存储过程
    in参数值在调用时必须指定
    out参数可以在调用后被返回
    inout参数调用时指定,并且可以被返回
    begin...end 代表存储过程体的开始和结束
    5. 调用一个存储过程call 存储过程名称()

    6. 删除一个存储过程drop procedure 存储过程名称drop procedure if exists 存储过程名称 #加强代码的健壮性
    1、创建一个不带参数的存储

    create procedure ba()
    begin
    select * from emp inner join dept on dept1=dept2;
    END
    //

    call ba

    2、带in存储过程 (in是输入参数)

    delimiter //
    create procedure ba001(in x int)
    BEGIN

    select * from emp where sid=x ;

    END
    //

    call ba001(1879)

    3、带out的语句 (out输出参数)
    delimiter //
    create procedure ba004 (out n int )
    BEGIN
    select age into n from emp where name ="牛八" ;
    END
    //

    call ba004(@n)
    select @n

    4、带in,out 参数

    delimiter //
    create procedure ba006 (in m int ,out n int )
    BEGIN
    select age into n from emp where sid = m;
    END
    //

    call ba006(1674,@n)
    select @n

    5、inout参数

    案例1: create procedure ba008(inout y int)
    BEGIN
    set y:=y+1 ;
    END
    //
    set @y=2
    call ba007(@y)
    select @y

    案例2:
    delimiter //

    create procedure ba009(inout x int)
    BEGIN
    select max(age) into x from emp where dept2=x;
    end

    //
    set @x=101
    call ba009(@x)
    select @x

    用户变量:
    定义语法:
    set @ 变量名 ;
    方法一:普通赋值
    set @变量名:=值 或 set @变量名=值

    方法二:通过查询结果为变量赋值
    select @变量名
    select 字段|表达式 变量名 from 表名 where 条件

    declare 声明变量

    declare i int

    练习题:造数

    delimiter //
    drop procedure if exists ba10 ;
    create procedure ba10 (in n int)
    BEGIN
    declare i int;
    SET i=0 ;
    while i<n DO
    INSERT into jj values (i);
    set i=i+1;
    end while;
    select * from jj ;
    END
    //
    call ba10(10000)

    例题2:
    delimiter //
    #drop table if exists jj ;
    drop procedure if exists ba10 ;
    create procedure ba10 ()
    BEGIN
    #create table jj (id int(100));
    declare i int;
    SET i=0 ;
    while i<100 DO
    INSERT into jj values (i);
    set i=i+1;
    end while;
    select * from jj ;
    END
    //
    call ba10()


    #存储内建表
    delimiter //
    drop table if exists jj ;
    drop procedure if exists ba10 ;
    create procedure ba10 ()
    BEGIN
    #create table jj (id int(100));
    declare i int;
    SET i=0 ;
    create table jj (id int(100));
    while (i<100) DO
    INSERT into jj values (i);
    set i=i+1;
    end while;
    select * from jj ;
    END
    //
    call ba10()

    if语句


    while语句

    delimiter //
    drop table if exists jj ;
    drop procedure if exists ba11 ;
    create procedure ba11 (in n int)
    begin
    declare i int default 0;
    create table jj (id int(100));
    repeat
    insert into jj values(i);
    set i=i+1;
    until i<=n
    end repeat;
    end
    //

    call ba11(10)


    delimiter //
    drop table if exists jj ;
    drop procedure if exists ba12 ;
    create procedure ba12 (in n int)
    begin
    declare i int;
    set i=0;
    create table jj (id int(100));
    while i<n do
    insert into jj values(i);
    set i=i+1;
    end while;
    end
    //

    call ba12(10)

    delimiter //
    drop procedure if exists ba13;
    create procedure ba13 (in n int)
    begin
    declare i int default 0;
    loop_label: loop


    insert into t1(filed) values(i);
    set i=i+1;
    if i>=5 then
    leave loop_label;
    end if;
    end loop;
    end;//

  • 相关阅读:
    NTC温度采集之数据拟合——freemat软件实现
    头文件中不能定义变量
    好的博客空间收藏
    STM32F407VET6之IAR之ewarm7.80.4工程建立(基于官方固件库1.6版本) 的工程文件目录
    STM32F407VET6之IAR之ewarm7.80.4工程建立(基于官方固件库1.6版本)
    JAVA反射机制o
    Java反射机制
    c+内存管理机制
    java内存空间详解
    JAVA内存管理再解
  • 原文地址:https://www.cnblogs.com/xiaolehua/p/13970979.html
Copyright © 2020-2023  润新知