mysql自定义函数

//注意规避mysql关键字,可能需要重定义结束符号符delimiter $$
//声明函数:参数类型以及返回类型
create function insertY(start int(11),end int(11))
returns varchar(255)
//开始函数实现
begin 
        //定义一个变量
	declare i int(11);
        //给变量赋值,需要加set
        set i = start;
        //while condition do,end while
	while i <= end do
                //if condition then,else,end if
                //注意比较是否相等使用 = 而不是 == 
		if (i mod 2 = 0) then
			insert into A values(i);
		else 
			insert into B values(i);
		end if;
		insert into C values(i);
		set i = i + 1;
	end while;
	return "finished";
end

//创建一个查询过程

create procedure sq(qq_ varchar(15))
begin
	select * from T_qq_9 where qq = qq_ union
	select * from T_qq_10 where qq = qq_ union
	select * from T_qq_5 where qq = qq_ union
	select * from T_qq_6 where qq = qq_ union
	select * from T_qq_7 where qq = qq_ union
	select * from T_qq_8 where qq = qq_;
end


call sq("343234223");
	

posted @ 2016-04-04 15:13  fcat  阅读(136)  评论(0编辑  收藏  举报