-- Open our cursor and determine how many rows are in it 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 -- Increment the counter SET @Ctr = @Ctr+1 -- Determine if the room name has changed IF (@c_RoomName<>@LastRoomName) BEGIN set @bFlag=1 END ELSE BEGIN SET @bFlag=0 END -- Set our RoomVariable to Last Record Value set @LastRoomName=@c_RoomName -- Insert the Record into the outputtable Insert into #OutPutTable (@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) END