第1关:事务
任务描述
本关任务:
- 修改存储过程,使得存储过程中若有错误的
SQL
则不执行,否则将事务提交; - 使用事务往
t_emp
表中插入数据。
答案:
USE mydb;
#请在此处添加实现代码
########## Begin ##########
# 修改存储过程 ———— 向 t_emp 表中插入数据(注意请勿修改提供的代码框架)
drop procedure if exists mydb.proc_insert;
delimiter $$
create procedure proc_insert()
Begin
#开启事务
start transaction ;
insert into t_emp values(1,'Nancy',301,2300);
insert into t_emp values(2,'Tod',303,5600);
insert into t_emp values(3,'Carly',301,3200);
## update t_emp set salary=0 where id=1;
## update t_emp set salery=8000 where id=2;
#事务提交
commit;
#当有sql异常的时候,sql继续往下执行,并将变量err的值改变为1
END $$
delimiter ;
########## End ##########