title: SQL SERVER: CURSOR FETCH causes error while identical row works fine
body: <p><strong>the context</strong></p>
  
  <p>I am working on a SQL server database for my e-mobility research. In this database we store charge sessions of electric cars in the Netherlands. Part of the ETL is to transform the recorded charge sessions into a new table that stores the (arrival) time between the charge sessions. </p>
  
  <p><strong>the problem</strong></p>
  
  <p>I use a cursor <code>crChargeSessions</code> in which I FETCH columns into variables. I do this 3 times. First a fetch in variables called @old[etc] then @new[etc] and at last again in @new[etc]. 
  t
  SQL server displays an error at this last FETCH row, while this row is a full copy of the other row. How is it possible that a row that works in the first place doesnt work a few rows later??</p>
  
  <p>note: I deleted the @variable declarations for readability improvement
      USE [CHIEF_Production]
      DECLARE crChargeStation CURSOR SCROLL FOR 
          SELECT [PostalCode],[Nr] 
          FROM [dbo].TBL_ChargeSessions 
          GROUP BY [PostalCode],[Nr] </p>
  
  <pre><code> OPEN crChargeStation
  
   FETCH NEXT FROM crChargeStation
   INTO @strPostalCode ,@strNumber
  
  WHILE @@FETCH_STATUS = 0 
  BEGIN
  
      DECLARE crChargeSessions CURSOR FOR 
      SELECT [ChargeSession_ID]
          ,[PostalCode]
          ,[Nr]     
          ,[RFID]
          ,[StartConnectionDate]
          ,[StartConnectionTime]
          ,[EndConnectionDate]
          ,[EndConnectionTime]
          ,[kWh]
      FROM [dbo].TBL_ChargeSessions 
      WHERE [PostalCode] = @strPostalCode AND [Nr] = @strNumber AND ([LambdaSyncRFID] = 0)
      ORDER BY  [EndConnectionDate] ASC ,  [StartConnectionTime] ASC
  
      OPEN crChargeSessions
  </code></pre>
  
  <p>FIRST TIME I FETCH</p>
  
  <pre><code>FETCH NEXT FROM crChargeSessions    INTO @LastChargeSession_ID, @strPostalCode, @strNumber ,@strLastRFID ,@dtlastStartConnectionDate ,@dtLastStartConnectionTime, @dtlastEndConnectionDate, @dtLastEndConnectionTime, @LastkWh
  </code></pre>
  
  <p>SECOND TIME I FETCH</p>
  
  <pre><code>FETCH NEXT FROM crChargeSessions    INTO @NewChargeSession_ID,  @strPostalCode, @strNumber ,@strNewRFID  ,@dtNewStartConnectionDate ,@dtNewStartConnectionTime, @dtNewEndConnectionDate, @dtNewEndConnectionTime, @NewkWh
  
      WHILE @@FETCH_STATUS = 0 
      BEGIN   
  
  
          SET @dcDeltaBBConnection = CAST ((@dtNewStartConnectionDate + @dtNewStartConnectionTime) - (@dtlastStartConnectionDate + @dtLastStartConnectionTime) as decimal(18,6))
          SET @dcDeltaEBConnection = CAST ((@dtNewStartConnectionDate + @dtNewStartConnectionTime) - (@dtlastEndConnectionDate + @dtLastEndConnectionTime) as decimal(18,6))
  
              PRINT @strChargeStation
  
          INSERT INTO [dbo].[TBL_Lambda_ChargeStation]
             ([Nr]
             ,[PostalCode]
             ,[LastRFID]
             ,[NewRFID]
             ,[LastStartConnectionDate]
             ,[LastStartConnectionTime]
             ,[NewStartConnectionDate]
             ,[NewStartConnectionTime]
             ,[deltaBeginBegin]
             ,[deltaEndBegin]
             ,[LastkWh]
             ,[NewkWh])
         VALUES
              (@strNumber
              ,@strPostalCode
              ,@strLastRFID
              ,@strNewRFID
              ,FORMAT(@dtlastStartConnectionDate ,'MM-dd-yyyy')
              ,FORMAT(@dtLastStartConnectionTime  ,'hh:mm:ss' )
              ,FORMAT(@dtNewStartConnectionDate ,'MM-dd-yyyy')
              ,FORMAT(@dtNewStartConnectionTime,'hh:mm:ss')
              ,@dcDeltaBBConnection 
              ,@dcDeltaEBConnection
              ,@LastkWh
              ,@NewkWh)
  
          -- UPDATE boolean 
              UPDATE [dbo].[TBL_ChargeSessions] SET [LambdaSyncStation] = 1 WHERE [dbo].[TBL_ChargeSessions].[ChargeSession_ID] = @lastChargeSession_ID
  
  
              SET @strLastRFID = @strNewRFID
              SET @dcDeltaBBConnection = 0
              SET @dcDeltaEBConnection = 0
              SET @lastChargeSession_ID =  @NewChargeSession_ID
              SET @dtlastStartConnectionDate =  @dtNewStartConnectionDate
              SET @dtLastStartConnectionTime =  @dtNewStartConnectionTime
              SET @dtlastEndConnectionDate =  @dtNewEndConnectionDate
              SET @dtLastEndConnectionTime =  @dtNewEndConnectionTime
              SET @LastkWh =  @NewKWh
  </code></pre>
  
  <p>THIRD TIME I FETCH<br>
          FETCH NEXT FROM crChargeSessions    INTO @NewChargeSession_ID,  @strPostalCode, @strNumber ,@strNewRFID  ,@dtNewStartConnectionDate ,@dtNewStartConnectionTime, @dtNewEndConnectionDate, @dtNewEndConnectionTime, @NewkWh</p>
  
  <pre><code>    END
  
      CLOSE crChargeSessions
      DEALLOCATE crChargeSessions 
  
  
      FETCH NEXT FROM crChargeStation
      INTO @strChargeStation 
  END
  
  
  CLOSE crChargeStation
  DEALLOCATE crChargeStation 
  </code></pre>
  
  <p>END</p>