title: Postgres syntax error at or near "IF"
body: <p>I am new to postgres and I am working on an assignment of mine. I had to create a table with only 1 column and, then I was given this statement to run in on pgadmin III:</p>
  
  <pre><code>BEGIN;
  INSERT INTO mytable VALUES (1);
  SAVEPOINT savepoint1;
  INSERT INTO mytable VALUES (2);
  ROLLBACK TO SAVEPOINT savepoint1;
  INSERT INTO mytable VALUES (3);
  SAVEPOINT savepoint2;
  INSERT INTO mytable VALUES (4);
  INSERT INTO mytable VALUES (5);
  SAVEPOINT savepoint3;
  SELECT * FROM mytable;
  --NOTE: You need to run this IF statement as PGScript 
  --(button next to the normal run button)
  IF (CAST ((SELECT MAX(id) FROM mytable) AS INTEGER) = 4)
  BEGIN
        RELEASE SAVEPOINT savepoint2;
  END
  ELSE
  BEGIN
        INSERT INTO mytable VALUES(6);
  END
  --Run the next steps normally
  SAVEPOINT savepoint2;
  INSERT INTO mytable VALUES (7);
  RELEASE SAVEPOINT savepoint2;
  INSERT INTO mytable VALUES (8);
  ROLLBACK TO savepoint2;
  COMMIT;
  </code></pre>
  
  <p>when I run this I get this error: syntax error at or near "IF"</p>
  
  <p>I have already take a look at this <a href="http://www.postgresql.org/docs/8.3/static/plpgsql-control-structures.html#PLPGSQL-CONDITIONALS" rel="noreferrer">38.6.2. Conditionals38.6.2. Conditionals</a> , I dont understand this very well, Do I need to change the query to have </p>
  
  <pre><code>IF (CAST ((SELECT MAX(id) FROM mytable) AS INTEGER) = 4) THEN 
  BEGiN 
  </code></pre>
  
  <p>and then when it ends I should end it with:</p>
  
  <pre><code>END IF
  </code></pre>
  
  <p>Why there is an error after all?? </p>