日期:2014-05-16 浏览次数:20789 次
问题:mysql存储过程的意义?
问题:mysql存储过程使用的时机?
mysql存储过程的实战教程
1 创建一个存储过程
2.创建存储函数
3.定义处理程序
方法一:捕获sqlstate_value
Declare CONTINUE HANDLER FOR SQLSTATE ‘42S02’ SET @info=’cannot find’;
方法二:捕获mysql_error_code
Declare CONTINUE HANDLER FOR 1146 SET @info=’con not find’;
方法三:是定义条件,然后调用
Declare can_not_find CONDITION FOR 1146;
Declare CONTINUE HANDLER FOR can_not_find set @info=’can not find’;
方法四:使用SQLWARNING
Declare exit handler for SQLWARNING SET @info=’error’;
方法五:使用not found
Declare exit handler for NOT FOUND SET @info=’can not find’;
方法六:使用SQLEXCEPTION
Declare EXIT HANDLERFOR SQLEXCEPTION SET @info=’error’;
4.定义光标
Declare cur_name varchar(20);
Declare cur_address varchar(20);
声明光标
Declare cur_student CURSOR FOR select name,address from student;
打开光标
OPEN cur_student
使用光标
FETCH cur_student into cur_name,cur_address;
关闭光标
Close cur_student
5.循环控制流程
循环控制流程
l IF语句
If age=10 then
Elseif age=20 then
Else
End if;
l Case语句
CASE
WHEN THEN
WHER THEN
ELSE
END CASE;
l LOOP语句和LEAVE语句 LEAVE是跳出循环 add_num结束标签
add_num LOOP
SET @count=@count+1;
If @count=100 then
LEAVE add_num;
END LOOP add_num;
l ITERATE语句 结束本次循环
add_num LOOP
SET @count=@count+1;
If @count=100 then
ITERATE add_num;
Select * from student;
&n