-- 秒杀执行存储过程
DELETE $$ -- console的结束符号由;转换成 $$
-- in输入参数;out:输出参数
-- ROW_COUNT():返回上条dml影响的条数: 小于0:sql语句错误/未执行修改sql操作
-- 存储过程执行秒杀过程
delimiter $$
CREATE PROCEDURE kill.execute_seckill -- 在kill数据库中创建名称为execute_seckill的存储过程
(in v_seckill_id BIGINT, in v_phone BIGINT, in v_kill_time timestamp, out r_result int)
BEGIN
DECLARE insert_count INT;
SET insert_count = 0;
START TRANSACTION;
INSERT IGNORE into success_killed (seckill_id, user_phone,create_time)
values (v_seckill_id,v_phone,v_kill_time);
select ROW_COUNT() into insert_count;
if(insert_count = 0) THEN
ROLLBACK;
SET r_result = -1;
ELSEIF (insert_count < 0) THEN
ROLLBACK;
set r_result = -2;
ELSE
update seckill set number = number - 1
where seckill_id = v_seckill_id
and end_time > v_kill_time
and start_time < v_kill_time
and number > 0;
SELECT ROW_COUNT() into insert_count;
IF (insert_count = 0) THEN
ROLLBACK;
set r_result = 0;
ELSEIF (insert_count < 0) THEN
ROLLBACK;
set r_result = -2;
ELSE
COMMIT;
SET r_result = 1;
END IF;
END IF;
END;
$$
-- 存储过程定义结束
-- 删除存储过程
drop procedure execute_seckill
delimiter ; -- console的结束符号由$$转换成;
set @r_result = -3; -- 在console中定义变量需要使用@
-- console 中执行存储过程
call execute_seckill(1001, 15665662547,NOW(),@r_result);
-- 获取结果mysql语法
select @r_result;
show CREATE PROCEDURE execute_seckill -- 查询存储过程