BEGIN
declare t_error int default 0;
declare addtime int default 0;
declare starttime int default 0;
declare endtime int default 0;
declare userid int default 0;
declare doctorid int default 0;
declare count int default 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=-1;
select start_time,end_time,user_id,doctor_id into @starttime,@endtime,@userid,@doctorid from doctor_order where id=orderid;
select count(*) into @count from user_doctor where user_id=userid and doctor_id=doctorid;
start TRANSACTION;
update doctor_order set pay_status=1 where id=orderid;
IF count>0 THEN
update user_doctor set start_time=@starttime,end_time=@endtime,status=1 where user_id=@userid and doctor_id=@doctori;
end if;
IF count=0 THEN
insert into user_doctor (user_id,doctor_id,add_time,start_time,end_time,status) values(@userid,@doctorid,123,@starttime,@endtime,1);
end if;
IF t_error = -1 THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
select t_error;
END