title: Error when creating plpgsql function with if then statement
body: <p>I am trying to create this PostgreSQL function:</p>
  
  <pre><code>create or replace function sp_get_user_activity(
          begindate date = null,
      enddate date = null)
  returns table ( 
      page varchar(75)) as
  $body$
  begin
      return query
      if begindate is not null and enddate is not null then
          begin
          select page from log_pg where begindate &gt;= created and endate &lt;= created;
          end;
      else
          if begindate is not null then
              begin
              select page from log_pg where begindate &gt;= la.created_on;
              end;
          end if;
          if enddate is not null then
              begin
              select page from log_pg where endate &lt;= created;
              end;
          end if;
      end if;
  end;
  $body$
  language plpgsql;
  </code></pre>
  
  <p>I want to create a function that gets page names from a table, either the function can except a begin date and end date, or just an end date or just a begin date.   </p>
  
  <p>The error message I'm getting when trying to create the function is and is referring to the very first "if" statement (line 9 in the code I cut/pasted above)</p>
  
  <blockquote>
  <pre><code>ERROR:  syntax error at or near "if"
  LINE 14:  if begindate is not null and enddate is not null then
            ^
  
  ********** Error **********
  
  ERROR: syntax error at or near "if"
  SQL state: 42601
  </code></pre>
  </blockquote>
  
  <p>Tried googling the error code for but with no luck.</p>