CREATE PROCEDURE DBO.UpdateAllotmentStartAndEndDate @OLD_AllotmentNumber int, @StartDate datetime OUTPUT, @Enddate datetime OUTPUT AS
SET NOCOUNT ON
Declare @StartDateTime char(8)
Declare @EndDateTime char(8)

-- Modify START and END of the allotment!
UPDATE Allotments SET
  StartDate = (SELECT MIN (DateStamp) FROM AllotmentLines WITH (NOLOCK) WHERE AllotmentNumber = @OLD_AllotmentNumber AND InitialCount + TotalChange > 0),
  EndDate =  (SELECT MAX (DateStamp) FROM AllotmentLines WITH (NOLOCK)  WHERE AllotmentNumber = @OLD_AllotmentNumber AND InitialCount + TotalChange> 0)
WHERE AllotmentNumber = @OLD_AllotmentNumber

select @startDateTime=Startdate, @EnddateTime=Enddate from allotments WITH (nolock) where allotmentnumber = @old_AllotmentNumber --TODO Remove these ekstra variables

Select  @StartDate = Convert (Datetime, @StartDateTime)
Select  @EndDate =  Convert (DateTime, @EndDateTime)

