title: SQL Server : sum by cursor
body: <p>This SQL Server 2012 function, and I want it to return all values by the cursor, but this <code>SUM</code> only last record from base. </p>
  
  <p>When I want to SUM by: </p>
  
  <pre><code>SET @AllSalary = @salarya + @AllSalary 
  </code></pre>
  
  <p>it shows NULL. </p>
  
  <p>I don't know what is the problem, it could be syntax error, fact is it doesn't display the desired result. </p>
  
  <p>(<code>param @montha INT</code>, will by used later)</p>
  
  <pre><code>CREATE FUNCTION allCasR
  (
      @montha INT
  )
  RETURNS INT
  AS
  BEGIN
      -- Declare the return variable here
      DECLARE @AllSalary INT;
      DECLARE @salarya FLOAT;
      DECLARE @tymC FLOAT;
      -- Add the T-SQL statements to compute the return value here
  
  DECLARE kursor_pensja CURSOR FOR
      SELECT contracts.salary
  FROM dbo.contracts ;
  OPEN kursor_pensja;
  FETCH NEXT FROM kursor_pensja INTO @salarya
  WHILE @@FETCH_STATUS=0 
  BEGIN
      SET @AllSalary =+ @salarya
  FETCH NEXT FROM kursor_pensja INTO @salarya
  END
  CLOSE kursor_pensja  
  DEALLOCATE kursor_pensja
  RETURN @AllSalary;
  END
  </code></pre>