title: Variable Table Update Case
body: <p>I have (simplified for this example) TableA with Id, Speed and Description fields. TableB contains modifications to TableA. Table B contains a Id (FK so will match a Id in TableA), ModifiedSpeed and OriginalSpeed field. </p>
  
  <p>Basically what I would like to be able to do is update either TableA Speed or TableB OriginalSpeed within one statement based on whether or not TableB has a record relating to TableA. </p>
  
  <p>Here's what I have tried so far, hopefully it will give some idea of what I want as i'm not sure I am explaining it very well:</p>
  
  <pre><code>SELECT a.Id, a.Description, a.Speed, CASE 
      WHEN EXISTS(SELECT b.Id FROM TableB b WHERE b.Id = a.Id) THEN
          UPDATE TableB SET OriginalSpeed = 
              CASE
                  WHEN (a.Speed is not null) THEN a.Speed
                  WHEN a.Description = 'Motorway' THEN 70
                  WHEN a.Description = 'A Road' THEN 60
                  ELSE 30
              END
      ELSE
          UPDATE TableA SET Speed =
              CASE
                  WHEN (a.Speed is not null) THEN a.Speed
                  WHEN a.Description = 'Motorway' THEN 70
                  WHEN a.Description = 'A Road' THEN 60
                  ELSE 30
              END
  END
  FROM TableA a
  </code></pre>
  
  <p>Any help would be appreciated.</p>