美文网首页
mysql存储过程

mysql存储过程

作者: 大雨滂沱在关外 | 来源:发表于2020-01-08 19:40 被阅读0次

使用数据库test
use test;
创建存储过程delete_mathces
delimiter create procedure delete_matches(in in_a varchar(64)) begin delete from tab1 t where a = in_a; end;
delimiter ;

insert into tab1(a,b) values ('a','b');

select * from tab1 t;
执行存储过程delete_matches
call delete_matches('a');

delimiter create procedure in_param(in p_in integer) begin select p_in; set p_in = p_in + 1; select p_in; end
delimiter ;

drop procedure in_param;
call in_param(1);

delimiter create procedure out_param(out p_out integer) begin select p_out; set p_out = p_out + 1; select p_out; end
delimiter ;

set @p_out = 1;
call out_param(@p_out);
show create procedure out_param;

delimiter create procedure tab_name_param(in tab_name varchar(64)) begin set @sql = concat('select * from ',tab_name); prepare result from @sql; execute result; deallocate prepare result; end
delimiter ;

drop procedure tab_name_param;
call tab_name_param('tab1');
create temporary table tmp_tab1(t_a varchar(64),t_b varchar(64));
drop table if exists tmp_tab1 ;
insert into tmp_tab1(t_a, t_b) values ('t_a','t_b');
select * from tmp_tab1 t;

create temporary table tmp_tab2 as (select * from tab2 t limit 1,10);
select * from tmp_tab2 t;
drop table if exists tmp_tab2;

show tables ;
use test;
create table z(a int ,primary key (a)) engine =innodb;

create procedure load_test(count int)
begin
declare i int unsigned default 0;
start transaction;

while i < count do
    insert into z select i;
    set i = i + 1;
    end while;
commit ;

end;

call load_test(10);

select * from z z2;

show engine innodb status;

select @@autocommit;

show global status like 'com_commit';
create procedure sp_auto_rollback_demo()
begin
declare exit handler for sqlexception rollback ;select -1;
start transaction;
insert into z select 1;
insert into z select 2;
commit;
select 1;
end;

drop procedure if exists sp_auto_rollback_demo;
call sp_auto_rollback_demo();

select * from z z2;

call sp_auto_rollback_demo();

show create table z ;

show full processlist ;

create table dept_emp (emp_no int,dept_no varchar(20)) engine = innodb;
create procedure cur_demo()
begin
declare done int default 0;
declare _emp_no int;
declare _dept_no varchar(20);
declare curl cursor for select emp_no,dept_no from dept_emp de;
declare continue handler for not found set done = 1;
open curl;
read_loop:loop
fetch curl into _emp_no,_dept_no;
if done then
leave read_loop;
end if;

end loop;
close curl;

end;

call cur_demo();

create procedure cur_demo2()
begin
declare done int default 0;
declare _emp_no int;
declare _dept_no varchar(20);
declare ret int default 0;

declare curl cursor for select emp_no,dept_no from dept_emp de;

declare continue handler for not found set done = 1;
open curl;
read_loop:loop
    fetch curl into _emp_no,_dept_no;
    if done then
        leave read_loop;
        set ret = ret + 1;
    end if;

end loop;
close curl;
select ret;

end;

call cur_demo2();

create table ret (emp_no int,dept_no varchar(20)) engine = MEMORY;

create procedure cur_demo3()
begin
declare done int default 0;
declare _emp_no int;
declare _dept_no varchar(10);
declare curl cursor for select emp_no,dept_no from dept_emp de;

declare continue handler for not found set done = 1;
open curl;
read_loop:loop
    fetch curl into _emp_no,_dept_no;
    insert into ret select _emp_no,_dept_no;
    if done then
        leave read_loop;
    end if;
end loop;
close curl;

end;

call cur_demo3();

select * from ret r;

create procedure cur_aggredate()
begin
declare done int default 0;
declare _empid int default -1;
declare _prev_empid int default -1;
declare _count int default 0;
declare _prev_count int default 0;
declare _ordermonth DATETIME;
declare _qty int;
declare _total INT default 0;
declare curl cursor for

select empid, ordermonth, qty from emporders e order by empid,ordermonth;

declare continue handler for not found set done = 1;

drop temporary table if exists ret ;
create temporary table ret(
    empid int,
    ordermonth datetime,
    qty int,
    total int,
    avg double(5,2)
);

open curl;
read_loop:loop
    fetch curl into _empid,_ordermonth,_qty;
    if done then
        leave read_loop;
    end if;

    if _empid <> _prev_empid && _prev_empid <> -1 then
        set _prev_empid = _empid;
        set _count = 1;
        set _total = _qty;
        insert into ret select _empid,_ordermonth,_qty,_total,_total/_count;
    else
        if _prev_empid = -1 then
            set _prev_empid = _empid;
        end if;
        set _count = _count + 1;
        set _total = _total + _qty;

        insert into ret select _empid,_ordermonth,_qty,_total,_total/_count;
    end if;
    end loop;

end;

create table emporders (empid int,ordermonth datetime,qty int);

call cur_aggredate();

GRANT ALL PRIVILEGES ON . TO 'root'@'127.0.0.1' IDENTIFIED BY 'root';

use test;
help contents;

相关文章

  • 15 MySQL 存储过程

    MySQL 存储过程 [toc] 存储过程概述 存储过程介绍 存储过程,相当于是 MySQL 语句组成的脚本 指的...

  • 存储过程

    使用存储过程 1.存储过程 ​ 需要MySQL5---->> MySQL5添加了对存储过程的支持。 ​ 迄...

  • MySQL存储过程详解 mysql 存储过程

    原文链接 MySQL存储过程详解 1.存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然...

  • Mysql存储过程

    阅读目录:MySQL存储过程_创建-调用-参数 存储过程:SQL中的“脚本” 创建存储过程 调用存储过程 存储过程...

  • 面试官突然问我MySQL存储过程,我竟然连基础都不会!(详细)

    MySQL存储过程 一、存储过程 1.1 什么是存储过程 存储过程(Stored Procedure)是在大型数据...

  • MySQL存储过程创建及调用方法

    MySQL存储过程是一个sql语句,那么我们如何创建呢,MySQL存储过程创建及修改,删除操作。 1,存储过程创建...

  • 存储过程

    创建存储过程 mysql: 执行存储过程 mysql: 使用OUT参数和INOUT参数: PREPARE和EXEC...

  • SQL基本语句(以MySQL为例)-- 第五部分

    19、使用存储过程MySQL的存储过程的语句跟这本书上的差太远了吧,以下参考自:MySQL存储过程教程 20、管理...

  • MySQL存储过程和存储函数

    一、存储过程 MySQL存储过程存储过程和存储函数参考文章 SQL语句需要先编译然后执行,而存储过程(Stored...

  • MySQL存储过程

    1、关于MySQL的存储过程 存储过程是数据库存储的一个重要的功能,但是MySQL在5.0以前并不支持存储过程,这...

网友评论

      本文标题:mysql存储过程

      本文链接:https://www.haomeiwen.com/subject/ojosactx.html