Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Why does this duplicate last row
Message
 
À
Tous
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Why does this duplicate last row
Divers
Thread ID:
00998497
Message ID:
00998497
Vues:
72
I have the following setup as a stored procedure. It perform well, with the exception that it duplicates the last row. I ran the query that populates the ScheduledCases cursor it it has 5 rows, but the final results have 6 rows with the last being duplicated. If I change the while statement and remove make it just While(@ctr < @records) it works fine. Thanks for any tips and pointers.
... setup and query code removed

-- Open our cursor and determine how many rows are in it
set @Ctr=0
Open ScheduledCases
set @Records=@@cursor_rows

-- Loop Through the Cursor
while (@ctr <= @Records)
BEGIN

   fetch ScheduledCases into @c_CaseDate,@c_PrimarySurgeon,@c_ProceduresOnCase,@i_ProcedureCount,@c_RoomName,
         @c_EnterOR,@c_ProcStart,@c_ProcEnd,@c_ExitOR,@c_ScheduledStart,@b_isCancelled,@t_CancelledDate,@i_RoomMinutes,
	 @i_ProcMinutes,@i_deptID,@t_BeginTime,@t_EnterOR,@t_ProcStart

   -- Increment the counter
   SET @Ctr = @Ctr+1

   -- Determine if the room name has changed
   IF (@c_RoomName<>@LastRoomName)
     BEGIN
       set @bFlag=1
       -- Get the minutes from Scheduled Start to Enter OR
       -- Modified to procedure Start
--      set @CaseOffSet=datediff(mi,@t_BeginTime,@t_EnterOR)
	set @CaseOffSet=DateDiff(mi,@t_BeginTime,@t_ProcStart)
     END
   ELSE
     BEGIN
       SET @bFlag=0
       set @CaseOffSet=0
     END
   -- Set our RoomVariable to Last Record Value
   set @LastRoomName=@c_RoomName


   -- Insert the Record into the outputtable
   Insert into #OutPutTable (CaseDate,PrimarySurgeon,ProceduresOnCase,ProcedureCount,RoomName,EnterOR,
           ProcStart,ProcEnd,ExitOR,ScheduledStart,isCancelled,CancelledDate,RoomMinutes,ProcMinutes,deptID, FirstCaseFlag,CaseOffSet)
           values 
      (@c_CaseDate,@c_PrimarySurgeon,@c_ProceduresOnCase,
       @i_ProcedureCount,@c_RoomName,@c_EnterOR,@c_ProcStart,@c_ProcEnd,@c_ExitOR, 
       @c_ScheduledStart,@b_isCancelled,@t_CancelledDate,@i_RoomMinutes, 
       @i_ProcMinutes,@i_deptID,@bFlag,@CaseOffSet)
END


close ScheduledCases
Deallocate ScheduledCases
select * from #OutPutTable
drop table #OutPutTable
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform