IF EXISTS (SELECT * FROM dbo.SysObjects WHERE id = object_id(N'dbo.TRIGGER_ReservationsLastUpdDateStamp') AND OBJECTPROPERTY(id, N'IsTrigger') = 1) DROP TRIGGER dbo.TRIGGER_ReservationsLastUpdDateStamp 
GO
CREATE TRIGGER [dbo].[TRIGGER_ReservationsLastUpdDateStamp] ON [dbo].[RESERVATIONS] AFTER INSERT,UPDATE AS
BEGIN
  DECLARE @ReferenceNumber INT
  DECLARE @GuestNumber INT

  DECLARE db_cur CURSOR LOCAL FAST_FORWARD FOR
    SELECT ReferenceNumber, GuestNumber FROM inserted
  OPEN db_cur

  FETCH NEXT FROM db_cur INTO @ReferenceNumber, @GuestNumber

  WHILE @@FETCH_STATUS = 0
  BEGIN
    UPDATE ReservationsExt SET LastUpdDateStamp = GETDATE() 
    WHERE ReservationsExt.FK_ReferenceNumber = @ReferenceNumber
    AND ReservationsExt.FK_GuestNumber = @GuestNumber
    IF @@ROWCOUNT = 0
      INSERT INTO ReservationsExt (FK_ReferenceNumber, FK_GuestNumber, LastUpdDateStamp) VALUES (@ReferenceNumber, @GuestNumber, GETDATE())
    FETCH NEXT FROM db_cur INTO @ReferenceNumber, @GuestNumber
  END
  CLOSE db_cur
  DEALLOCATE db_cur
END
