title: Syntax error when create Postgres function
body: <p>I'm trying to create a function that sums the result of all of one query's values and compares it to a number of another, simple query.</p>
  
  <p>This is what I have, however I'm getting a syntax error near begin (2nd line):</p>
  
  <pre><code>CREATE FUNCTION trigf1(sbno integer, scid numeric(4,0)) RETURNS integer
  BEGIN
      declare sum int default 0;
      declare max as SELECT totvoters FROM ballotbox WHERE cid=scid AND bno=sbno;
  
      for r as
          SELECT nofvotes FROM votes WHERE cid=scid AND bno=sbno;
      do
          set sum = sum + r.nofvotes;
      end for
  
      if sum &gt; max
          then return(0);
      else
          return(1);
  END
  </code></pre>
  
  <p>This results in: </p>
  
  <blockquote>
    <p>Syntax error near 'BEGIN' </p>
  </blockquote>
  
  <p>I'm using postgreSQL and the pgadminIII (just in case it is relevant).</p>
  
  <p>I have no idea why I'm getting this error, everything seems to be exactly as the textbook defined. (This is the text book I'm using: <a href="http://digilib.usu.ac.id/buku/107859/Database-systems-concepts,-6th-ed.html" rel="nofollow">http://digilib.usu.ac.id/buku/107859/Database-systems-concepts,-6th-ed.html</a>)</p>