I have a question due to an assignment i am working on at the moment. I need to make a trigger that checks if an airplane seat is taken before a customer can be inserted into the table. I have the following Trigger so far:
CREATE TRIGGER CheckIfSeatIsUnique
ON PassagierVoorVlucht
AFTER insert, update
AS
BEGIN
IF @@ROWCOUNT = 0 RETURN
SET NOCOUNT ON
BEGIN TRY
IF EXISTS (SELECT 1 FROM PassagierVoorVlucht P Join inserted I on P.vluchtnummer=i.vluchtnummer Where P.stoel = I.stoel)
BEGIN
RAISERROR('The chosen seat is taken', 16, 1)
END
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE()
DECLARE @ErrorSeverity INT = ERROR_SEVERITY()
DECLARE @ErrorState INT = ERROR_STATE()
RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState)
END CATCH
END
The problem i have, is that the trigger checks if the seat is taken AFTER the insert was done, So the seat will always be taken no matter what.
Is there some way to check if the seat is taken before the insert is done?
thanks in advance
Aucun commentaire:
Enregistrer un commentaire