DECLARE @MaxWareGroupNumber INT

SELECT @MaxWareGroupNumber = MAX(WareGroupNumber) FROM WareGroups WITH (NOLOCK)

IF NOT EXISTS (SELECT * FROM IdentityFields WITH(NOLOCK) WHERE DBTableName = 'WAREGROUPIDENTITY')
  INSERT INTO IdentityFields (DBTableName, HotelCustomerNumber, IdentityField, FKDepartment, Changedate) VALUES ('WAREGROUPIDENTITY', -1, ISNULL(@MaxWareGroupNumber,0), -1, GETDATE())