Advertisement
Guest User

Untitled

a guest
Nov 10th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.00 KB | None | 0 0
  1. CREATE TRIGGER CheckDirection
  2. ON cars.RegistrationRecords
  3. AFTER INSERT
  4. AS
  5. BEGIN
  6.     DECLARE @insertedRecordID int
  7.     DECLARE @insertedAutoID int
  8.     DECLARE @insertedDirection int
  9.     SET @insertedRecordID = (SELECT TOP 1 RecordID
  10.                            FROM inserted
  11.                            ORDER BY RecordID DESC)
  12.     SET @insertedAutoID = (SELECT TOP 1 AutoID
  13.                            FROM inserted
  14.                            ORDER BY RecordID DESC)
  15.     SET @insertedDirection = (SELECT TOP 1 Direction
  16.                               FROM inserted
  17.                               ORDER BY RecordID DESC)
  18.     DECLARE @lastCapturedAutoDirection int
  19.     SET @lastCapturedAutoDirection = (SELECT TOP 1 Direction
  20.                                   FROM cars.RegistrationRecords
  21.                                   WHERE cars.RegistrationRecords.AutoID = @insertedAutoID and
  22.                                    cars.RegistrationRecords.RecordID < @insertedRecordID
  23.                                   ORDER BY RecordID DESC)
  24.     IF @insertedDirection = @lastCapturedAutoDirection
  25.     BEGIN
  26.         PRINT 'Автомобиль не может несколько раз подряд въехать/выехать'
  27.         ROLLBACK TRANSACTION
  28.     END
  29. END
  30. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement