Я пытаюсь создать триггер и по какой-то причине второй оператор if нарушает мой код.
Если я удаляю второй оператор if, код будет работать.Я получаю сообщение об ошибке:
Запрос на транзакцию отката не имеет соответствующей начальной транзакции
Мой код:
create trigger insertemployee
on jobinsailing
after insert
as
BEGIN TRANSACTION
declare @sailingID int,
@employeeID char(9),
@jobTitle varchar(10),
@sailingStartDate date,
@sailingEndDate date
select @sailingId = inserted.sailingid,
@employeeID = inserted.employeeid,
@jobTitle = inserted.jobtitle,
@sailingStartDate = sailing.leavingtime,
@sailingEndDate = sailing.returntime
from inserted
inner join sailing
on inserted.sailingid = sailing.sailingid
if exists(select employeeid
from sailing
inner join jobinsailing
on sailing.sailingid = jobinsailing.sailingid
where ( @sailingStartDate <= returntime )
and ( leavingtime <= @sailingEndDate )
and employeeid = @employeeID) rollback TRANSACTION;
else if exists((select sailing.sailingid
from sailing
inner join jobinsailing
on jobinsailing.sailingid = sailing.sailingid
where Datediff(day, @sailingStartDate, sailing.leavingtime) <= 4
or Datediff(day, @sailingEndDate,sailing.leavingtime) <= 4
or Datediff(day, @sailingStartDate,sailing.returntime) <= 4
or Datediff(day, @sailingEndDate,sailing.returntime) <= 4
)) rollback TRANSACTION;
COMMIT TRANSACTION