title: How to set variable in to Ms Sql select
body: <p>How can I set in MS Sql select variables, idea looks like this:</p>
  
  <pre><code>Declare @var int
  
  SET @var = 0;
  
  Select (if(idid = @var) then sum+1 else sum-1) AS Sum,
     set @var = id 
  FROM table
  </code></pre>
  
  <p>Edit based on comments.</p>
  
  <pre><code>DECLARE @T TABLE
  (
  ID  INT PRIMARY KEY,
  IDID INT,
  SUMM INT 
  )
  
  INSERT INTO @T
  SELECT 1,1,4 UNION ALL
  SELECT 2,1,5 UNION ALL 
  SELECT 3,2,6 UNION ALL 
  SELECT 4,2,7 UNION ALL 
  SELECT 5,3,8
  </code></pre>
  
  <p>In select result I need: </p>
  
  <pre><code>ID  IDID  SUMM 
  --  ----  ----
  1   1     4 
  2   1     0 
  3   2     6 
  4   2     0 
  5   3     8
  </code></pre>