1.创建存储过程的语法
create procedure1()
begin
...;
end;
如果在命令行下执行 需要加上定界符
delimiter//
...;
//
delimiter;
2.创建有参的存储函数
create proceduce1(
in(标识入参) p1 decimal(8,2),(数据类型)
out(标识出参) p2 decimal(8,2),(数据类型)
inout(标识既可以做入参也可以做出参)
)
begin
select sum(t.user) as user into p2 where t.id=p1;
end;
3.调用过程
call proceduce1(如果有入参);
4.删除存储过程
drop proceduce proceduce1;
5.声明成员变量
declare xx decimal(8,2);
declare tax int default 6;
6.执行流程
beagin
set tax=1;(为变量赋值)
slect t.id from user where t.money=p1 into tax;(查询赋值语句)
if(tax==1) then (if语句)
....;
else
....;
end if;
end
add_loop:LOOP(执行一个重复循环)
set tax=tax+1;
if tax>10 then leave add_loop; (leave标识退出循环)
end if;
end LOOP add_loop;
while i < tax;(while语句)
....
end while;
case param(case语句)
when 0 then insert into userinfo(name) values('demo_0');
when 1 then insert into userinfo(name) values('demo_1');
else insert into userinfo(name) values('demo_default');
end case;
7.处理游标问题
处理流程:
一、声明一个游标: declare 游标名称 CURSOR for table;(这里的table可以是你查询出来的任意集合)
二、打开定义的游标:open 游标名称;
三、获得下一行数据:FETCH 游标名称 into testrangeid,versionid;
四、需要执行的语句(增删改查):这里视具体情况而定
五、释放游标:CLOSE 游标名称;
declare testrangeid BIGINT;
declare versionid BIGINT;
declare done int;
--创建游标,并存储数据
declare cur_test CURSOR for select id as testrangeid,version_id as versionid from tp_testrange;
--游标中的内容执行完后将done设置为1
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
--打开游标 open cur_test;
--执行循环 posLoop:LOOP --判断是否结束循环 IF done=1 THEN LEAVE posLoop;
END IF;
--取游标中的值 FETCH cur_test into testrangeid,versionid;
--执行更新操作 update tp_data_execute set version_id=versionid where testrange_id = testrangeid;
END LOOP posLoop;
--释放游标 CLOSE cur_test;