title: Syntax error in dynamic SQL in pl/pgsql function
body: <p>I am using pl/pgsql in PostgreSQL 10, to create complex queries. I am testing a query with a couple of <code>JOIN</code>s and <code>AND</code>s. This is what I have so far:</p>
  
  <pre><code>DROP FUNCTION IF EXISTS search_person(name text);
  CREATE  FUNCTION search_person(name text) RETURNS TABLE(address_id integer, address_geom text, event_name text) AS $$
  --DECLARE 
  
  BEGIN
      RETURN QUERY EXECUTE 
      'SELECT address.id, event.name, address.geom 
      FROM  event JOIN person JOIN address JOIN person_address JOIN event_person
      WHERE 
      person_address.event_id = event.id AND
      event_person.event_id = event.id AND
      person.id = event_person.person_id AND
      person.name like
      $1'        
  
      USING name;
  END;
  $$
  LANGUAGE plpgsql;
  </code></pre>
  
  <p>I get no errors while creating this function. I call it like so <code>select search_person('nick');</code> and I get:</p>
  
  <blockquote>
  <pre><code>ERROR:  syntax error at or near "WHERE"
  LINE 3:     WHERE 
              ^
  QUERY:  SELECT address.id, event.name, address.geom 
      FROM  event JOIN person JOIN address JOIN person_address JOIN event_person
      WHERE 
      person_address.event_id = event.id AND
      event_person.event_id = event.id AND
      person.id = event_person.person_id AND
      person.name like
      $1
  CONTEXT:  PL/pgSQL function search_creator(text) line 5 at RETURN QUERY
  SQL state: 42601
  </code></pre>
  </blockquote>
  
  <p>I cannot see or fix the problem. I tried replacing <code>AND</code> with <code>||</code> in the <code>WHERE</code> clause, but nothing changed.</p>
  
  <p>What should I do?</p>
  
  <p>EDIT</p>
  
  <p>This is the code I have now and I get an empty table, even though I should get results, according to my database data that I checked. </p>
  
  <pre><code>CREATE  FUNCTION search_person(name character(600)) RETURNS TABLE(address_id bigint, address_geom geometry, event_name character(200)) AS $$
  
  BEGIN
      RETURN QUERY EXECUTE 
      'SELECT address.id, address.geom, event.name        
  
      FROM
      person 
      JOIN event_creator ON event_person.person_id = person.id
      JOIN event ON event.id = event_person.event_id 
      JOIN person_address ON person_address.event_id = event.id 
      JOIN address ON address.id = cep.address_id
  
      WHERE person.name LIKE $1'
      USING name;
  
  END;
  $$
  LANGUAGE plpgsql;
  </code></pre>