--begin tran
--nb20140129 Fixed dependancy to count hallline up by booking before the script works
--nb20130319 Somebudy overlooked this script. Changed to update the new table
--nb20120614 Ensure the change from old to new table booking is not coliding
declare @OldIdMax INT, @NewIdMax INT, @IdNew INT, @IdOld INT, @Id INT
SET @OldIdMax = 0
SET @NewIdMax = 0
SET @IdNew = 0
SET @IdOld = 0
select @OldIdMax = MAX(RestaurantReservationNumber) from RestaurantReservations with (nolock)
select @NewIdMax = MAX(ReservationConfLineNumber) from RESERVATIONCONFERENCELINE with (nolock)
select @IdOld = IdentityField from IdentityFields with (nolock) where DbTableName = 'RESERVATIONROOMLINEIDENTITY'
select TOP 1 @IdNew = Id FROM IdentityReservationRoomLineNumber WITH (NOLOCK) ORDER BY ID DESC
SET @Id = @IdNew
if @Id < @IdOld
  SET @Id = @IdOld
if @Id < @OldIdMax 
  SET @Id = @OldIdMax
if @Id < @NewIdMax 
  SET @Id = @NewIdMax
PRINT @Id

if @Id > @IdNew
begin
  SET IDENTITY_INSERT IdentityReservationRoomLineNumber ON
  insert into IdentityReservationRoomLineNumber (Id, Field1) VALUES (@Id, 1)
  SET IDENTITY_INSERT IdentityReservationRoomLineNumber OFF
end
/*
select MAX(RestaurantReservationNumber) from RestaurantReservations with (nolock)
select MAX(ReservationConfLineNumber) from RESERVATIONCONFERENCELINE with (nolock)
select IdentityField from IdentityFields with (nolock) where DbTableName = 'RESERVATIONROOMLINEIDENTITY'
select TOP 1 Id FROM IdentityReservationRoomLineNumber WITH (NOLOCK) ORDER BY ID DESC
*/
--rollback tran