-- mysql游标循环的使用 --
delimiter //
drop procedure if EXISTS sum_money;
create procedure sum_money()
begin
declare p_uid int default 0;
declare p_money DECIMAL(10,2) default 0.0;
declare p_sumMoney DECIMAL(11,2) default 0.0;
declare no_more_record int default 0;
declare cur CURSOR for select uid,money from `ezcm_member` order by uid limit 0,1000;
declare CONTINUE HANDLER for not found set no_more_record=1;
open cur;
fetch cur into p_uid,p_money;
while no_more_record !=1 DO
set p_sumMoney = p_sumMoney+p_money;
fetch cur into p_uid,p_money;
end while;
close cur;
SELECT p_sumMoney;
END
//
delimiter ;
call sum_money();
函数与存储过程的区别:
1、只有函数有返回值。
函数的格式:
create function func_name()
返回类型
returns int(5)
begin
定义变量
。。。。
return return_value;
end;
-- 随机字符串函数 --
delimiter //
drop function if EXISTS rand_string;
create function rand_string(p_length int)
returns varchar(255)
BEGIN
declare chars_str varchar(100) default 'abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNUVWXYZ';
declare return_str varchar(255) default '';
declare i int default 0;
while i
set return_str = concat(return_str,substring(chars_str,floor(1+rand()*52),1));
set i=i+1;
end while;
return return_str;
END
//
delimiter //
set global log_bin_trust_function_creators=TRUE;
drop function if EXISTS rand_num;
create function rand_num()
returns int(5)
begin
declare i int default 0;
set i= floor(10+rand()*500);
return i;
end
//
delimiter ;
select rand_string(10);
2、调用方式
select rand_string(10);
存储过程为 call procedure_name();
所有示例代码:
delimiter //
create procedure demo_in_parameter1(in p_in int)
begin
select p_in;
set p_in=2;
SELECT p_in;
end;
//
delimiter ;
set @p_in = 1;
call demo_in_parameter1(@p_in);
select @p_in;
delimiter //
create procedure demo_out_parameter(out p_out int)
begin
SELECT p_out;
set p_out=2;
SELECT p_out;
end;
//
delimiter ;
set @p_out=1;
call demo_out_parameter(@p_out);
select @p_out;
delimiter //
drop PROCEDURE if EXISTS demo_inout_parameter;
create procedure demo_inout_parameter(inout p_inout int)
begin
SELECT p_inout;
set p_inout=2;
SELECT p_inout;
end;
//
delimiter ;
set @p_inout=1;
call demo_inout_parameter(@p_inout);
select @p_inout;
delimiter //
drop PROCEDURE if EXISTS demo_declare;
create PROCEDURE demo_declare(inout p_inout int)
BEGIN
DECLARE l_int int unsigned default 4000000;
DECLARE l_numeric float(8,2) DEFAULT 9.95;
declare l_date date default '1999-12-23';
declare l_datetime datetime default '1999-12-23 23:59:59';
declare l_varchar varchar(255) default 'This will not be padded';
select l_int;
select l_numeric;
select l_date;
select l_datetime;
select l_varchar;
select p_inout;
END ;
//
delimiter ;
set @p_inout=5;
call demo_declare(@p_inout);
select 'Hello world' into @x;
select @x;
set @y = 'goodby cruel world';
select @y;
set @z=1+2+3;
select @z;
delimiter //
drop PROCEDURE if EXISTS GreetWorld;
create procedure GreetWorld(in p_in varchar(32),in p_in_2 int)
begin
set p_in_2=p_in_2+1;
select p_in_2;
select concat(p_in,' World',10);
END
//
delimiter ;
set @greeting = 'Hi ';
set @p_in_2 = 0;
call GreetWorld(@greeting,@p_in_2);
drop PROCEDURE if EXISTS p1;
drop PROCEDURE if EXISTS p2;
create procedure p1() set @last_procedure='p1';
create procedure p2() select concat('Last procedure was ',@last_procedure);
call p1();
call p2();
delimiter //
drop PROCEDURE if EXISTS proc10;
create procedure proc10 #name存储过程名
(in param int)
BEGIN
declare variable1 char(10);
if param = 17 then
set variable1='birds';
else
set variable1='beasts';
end if;
select variable1;
select param;
end
//
delimiter ;
call proc10(20);
select name from mysql.proc where db='develop-212';
select routine_name from information_schea.routines where routine_schema='develop-212';
show procedure status where db='develop-212';
show create procedure `develop-212`.proc10;
delimiter //
create procedure proc11()
BEGIN
declare x1 varchar(5) default 'outer';
BEGIN
declare x1 varchar(5) default 'inner';
select x1;
end;
select x1;
end;
//
delimiter ;
call proc11();
-- if-then-else--
delimiter //
drop PROCEDURE if EXISTS proc12;
create procedure proc12(IN param int)
begin
declare var int;
declare s1 int default 0;
set var=param+1;
if var=0 then
set s1=17;
end if;
if param=0 then
set s1= s1+1;
ELSE
set s1=s1+2;
end if;
select s1;
end;
//
delimiter ;
call proc12(0);
-- case语句 --
delimiter //
drop procedure if EXISTS proc14;
create procedure proc14(in para int)
BEGIN
declare var int;
declare temp int default 0;
set var=para+1;
case var
WHEN 0 THEN
set temp=17;
when 1 THEN
set temp=18;
ELSE
set temp=19;
end case;
select temp;
end;
//
delimiter ;
call proc14(0);
-- while 循环 --
delimiter //
drop PROCEDURE if EXISTS proc15;
create procedure proc15()
BEGIN
declare var int;
set var=0;
while var<6 DO
set var=var+1;
end while;
select var;
end;
//
delimiter ;
call proc15();
delimiter //
drop PROCEDURE if EXISTS proc16;
create procedure proc16()
BEGIN
declare v int;
declare tmp int;
set tmp=5;
set v=0;
REPEAT
set tmp = tmp+2;
set v = v+1;
until v>=5
end repeat;
select v,tmp;
end;
//
delimiter ;
call proc16();
delimiter //
drop PROCEDURE if EXISTS proc17;
create procedure proc17()
BEGIN
declare v int;
declare tmp int;
set v=0;
set tmp=0;
LOOP_LABLE:LOOP
set v=v+1;
set tmp=tmp+3;
if v>=5 THEN
leave LOOP_LABLE;
end if;
end loop;
select v,tmp;
end;
//
delimiter ;
call proc17();
select concat(1,2,3,4,5,6,7);
select charset('hi');
select instr(lcase('hello,World'),'W');
select lcase('hello,World');
BEGIN
DECLARE no_more_record INT DEFAULT 0;
DECLARE pID BIGINT(20);
DECLARE pValue DECIMAL(15,5);
DECLARE cur_record CURSOR FOR SELECT colA, colB from tableABC; /*首先这里对游标进行定义*/
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_record = 1; /*这个是个条件处理,针对NOT FOUND的条件,当没有记录时赋值为1*/
OPEN cur_record; /*接着使用OPEN打开游标*/
FETCH cur_record INTO pID, pValue; /*把第一行数据写入变量中,游标也随之指向了记录的第一行*/
WHILE no_more_record != 1 DO
INSERT INTO testTable(ID, Value)
VALUES (pID, pValue);
FETCH cur_record INTO pID, pValue;
END WHILE;
CLOSE cur_record; /*用完后记得用CLOSE把资源释放掉*/
END
-- mysql游标循环的使用 --
delimiter //
drop procedure if EXISTS sum_money;
create procedure sum_money()
begin
declare p_uid int default 0;
declare p_money DECIMAL(10,2) default 0.0;
declare p_sumMoney DECIMAL(11,2) default 0.0;
declare no_more_record int default 0;
declare cur CURSOR for select uid,money from `ezcm_member` order by uid limit 0,1000;
declare CONTINUE HANDLER for not found set no_more_record=1;
open cur;
fetch cur into p_uid,p_money;
while no_more_record !=1 DO
set p_sumMoney = p_sumMoney+p_money;
fetch cur into p_uid,p_money;
end while;
close cur;
SELECT p_sumMoney;
END
//
delimiter ;
call sum_money();
6867059845.71
376280901.63
376280901.63
select money from ezcm_member order by uid limit 0,1000;
select sum(money) from (select money from ezcm_member order by uid limit 0,1000) as t;
-- 随机字符串函数 --
delimiter //
drop function if EXISTS rand_string;
create function rand_string(p_length int)
returns varchar(255)
BEGIN
declare chars_str varchar(100) default 'abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNUVWXYZ';
declare return_str varchar(255) default '';
declare i int default 0;
while i
set return_str = concat(return_str,substring(chars_str,floor(1+rand()*52),1));
set i=i+1;
end while;
return return_str;
END
//
delimiter //
set global log_bin_trust_function_creators=TRUE;
drop function if EXISTS rand_num;
create function rand_num()
returns int(5)
begin
declare i int default 0;
set i= floor(10+rand()*500);
return i;
end
//
delimiter ;
select rand_string(10);
show PROCESSLIST;