title: SQL Adding Function
body: <p>I'm trying to write a function that adds values that are selected from two different tables. I need the <code>SUM</code> of one column for a specific id in table 1, added to the <code>SUM</code> of a column for a specific id in table 2.</p>
  
  <pre><code>CREATE FUNCTION dbo.getSum(@id varchar(9)) --Our IDs are strings of length 9
  RETURNS integer --I've also tried decimal(x,x) and int
  AS
  BEGIN
  DECLARE @total as integer; -- same here with decimal and int
  SELECT @total = 
  (
      (select SUM(Amount)
      from table1
      where id = @id)
      +
      (select SUM(amount)
      from table2
      where id = @id)
  );
  RETURN @total;
  END;
  </code></pre>
  
  <p>I get several types of errors when I try to create this function, like <code>incorrect syntax near 'integer'</code>, <code>Must declare the scalar variable "@total".</code>, and <code>Incorrect syntax near 'END'.</code></p>
  
  <p>I've tried several variations and looked at several SO questions, but haven't found one that's fixed this issue for me yet. This is on SQL Server.</p>