mysql 存储过程 基础例子
2017-06-02 16:49
507 查看
DELIMITER // -- 定义分隔符,防止作为sql语句执行 drop procedure if exists pr_add; create procedure pr_add( ) begin declare c int; set c = 2; if c = 1 then insert into ONLINE_USER_INFO values(1,'admin','厉害了',''); else delete from ONLINE_USER_INFO where user_code = 1; end if; while c < 10 do insert into ONLINE_USER_INFO values(c+2,'admin','厉害了',''); set c = c + 1; end while; test_loop:loop delete from ONLINE_USER_INFO where user_code = c-2; set c = c - 1; if c < 8 then leave test_loop; end if; end loop; end // -- 必须,否则创建存储过程失败 DELIMITER ; call pr_add();
2.事务回滚
DELIMITER // -- 定义分隔符,防止作为sql语句执行
drop procedure if exists pr_add;
create procedure pr_add( )
begin
declare c int;
declare t_error INTEGER default 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1; --出现sql异常则设置t_error为1
set c = 1;
START TRANSACTION; --开启事务
if c = 1 then
insert into ONLINE_USER_INFO values(1,'admin','厉害了','');
else delete from ONLINE_USER_INFO where user_code = 1;
end if;
while c < 10 do
insert into ONLINE_USER_INFO values(c+2,'admin','厉害了','');
set c = c + 1;
end while;
test_loop:loop
delete from ONLINE_USER_INFO where user_code = c-2;
set c = c - 1;
if c < 8 then
insert into ONLINE_USER_INFO values('admin','厉害了',''); --出现异常
leave test_loop;
end if;
end loop;
if t_error = 1 then
ROLLBACK; --执行回滚操作
else
commit; --提交sql操作
end if;
end // -- 存储的结束
DELIMITER ;
call pr_add()
相关文章推荐
- mysql存储过程 例子 游标cursor 循环嵌套 事物
- mysql存储过程(基础3)
- MySQL 存储过程 游标例子
- mysql 入门基础(5)—存储过程和游标
- MYSQL 存储过程实现类似ORACLE row_number 和 rank 的简单例子
- MySQL 5.0 存储过程例子(使用了游标)
- MySQL基础之存储过程
- MySQL基础笔记(六) 存储过程与函数
- 基础 ADO.NET 访问MYSQL 与 MSSQL 数据库例子
- java基础第22天_mysql存储过程、事务隔离
- Mysql使用大全-从基础到存储过程
- mysql 存储过程 (一) 基础
- Mysql使用大全 从基础到存储过程
- MySQL基础笔记(六) 存储过程与函数
- mysql 存储过程 动态sql例子
- mysql存储过程(基础1)
- 简单说说MYSQL的触发器(例子包括了存储过程)
- Mysql使用大全 从基础到存储过程
- IT十八掌作业_java基础第22天_mysql存储过程、事务隔离
- MySQL基础八之存储过程