title: SQL case statement with condition adding two large int values
body: <p>I have a sql query that has a case statement. The case statement has a condition that adds two integer values and verifies their sum is smaller than the max integer value allowed by sql (rounded in the example for simplicity) beeefore assigning it back to another int type variable.
  The issue is, in the WHEN statement where the two values are being added, since the sum value is larger than the sql max allowed integer, the whole statement throws a sql arithmetic exception. I am ASSUMING internally the sum value is placed in a temporary integer variable when adding the two integer values...
  Eg,</p>
  
  <pre><code>Declare @int1 as Int =2147483598
  Declare @int2 as Int =2147483598
  Declare @int3 as Int =0
  DECLARE @MaxIntValue INT = 2147483600
  
  set @int3 = (CASE WHEN (@int1 &lt; 2)
                      THEN @int1 
                    WHEN ((@int1 + @int2) &lt; @MaxIntValue)
                      THEN @int1 + @int2
                    ELSE @int1
              END)
  select @int3 
  </code></pre>
  
  <p>I found a work around where if I change the type of one of the variables to a bigint instead
  (i.e, </p>
  
  <pre><code>Declare @int1 as bigint =2147483598
  </code></pre>
  
  <p>)
  the issue is resolved since I am again ASSUMING the datatype of the variable used to hold the sum value internally now changes to a bigint type as well. I would like to avoid this solution since in reality this query is part of a much bigger and complex stored procedure which involves multiple tables from where these values are procured.
  Thank you.</p>