title: SQL CASE with WHILE loop in not working
body: <p>I'm trying to include a WHILE loop inside a CASE statement but i am getting the following errors:</p>
  
  <blockquote>
    <p>Incorrect syntax near the keyword 'declare'.</p>
    
    <p>Incorrect syntax near '.'.</p>
    
    <p>Incorrect syntax near the keyword 'ELSE'.</p>
  </blockquote>
  
  <p>My code:</p>
  
  <pre><code>SELECT DISTINCT 
    iu.id AS 'number',
    iufv.v AS 'vnumber',
    iufv.timet AS 'time',
    iufv.loc AS 'loc',
    CASE WHEN iufv.timet &lt;= GETDATE() THEN 
      DECLARE @checktime DATE
      SET @checktime = iufv.timet 
      WHILE iufv.timet &lt;= GETDATE()
      BEGIN
        SET iufv.timet = DATEADD(DAY, 42, iufv.timet)
        PRINT @checktime
      End 
    GO
    ELSE 
      iufv.timet 
    END AS nexttime
  FROM dbo.iu
         INNER JOIN dbo.iufv ON iu.active_ufv = iufv.gkey
         INNER JOIN dbo.sr ON iu.gkey = sr.applied_to_gkey
  WHERE  iufv.ts = 'S4' AND iufv.loc = 'MBUL'
  </code></pre>
  
  <p>Expected output:</p>
  
  <pre><code>number vnumber time       loc  nexttime
  300    v300300 28/12/2016 MBUL 22/03/2017
  400    v400400 25/09/2016 MBUL 23/04/2017
  500    v500500 18/12/2016 MBUL 23/04/2017
  </code></pre>
  
  <p>Logic of WHILE LOOP:</p>
  
  <p>If the time is less than todays date add 42 days, </p>
  
  <p>if its still less than todays date add another 42 days etc </p>
  
  <p>until it is greater than todays date.</p>