mysql存储过程

使用数据库test
use test;
创建存储过程delete_mathces
delimiter
delimiter ;

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

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

delimiter
delimiter ;

drop procedure in_param;
call in_param(1);

delimiter
delimiter ;

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

delimiter
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;

你可能感兴趣的:(mysql存储过程)