title: Check to see if a record exists postgres function
body: <p>I am attempting to create a function that will determine if a record exists for our applications developers to help simplify things. </p>
  
  <pre><code>CREATE FUNCTION records_exist(schema_name VARCHAR(255), table_name VARCHAR(255), field_name VARCHAR(255), field_value VARCHAR(255))
    RETURNS BOOLEAN
  LANGUAGE plpgsql
  AS $$
  DECLARE
  
      _schema_name ALIAS FOR $1;
    _table_name  ALIAS FOR $2;
    _field_name  ALIAS FOR $3;
    _field_value ALIAS FOR $4;
    _sql_string  VARCHAR(5000);
  BEGIN
  _sql_string= 'SELECT EXISTS(SELECT 1 FROM ' || _schema_name || '.' || _table_name || ' WHERE ' || _field_name || '=' || _field_value || ');';
  
  
  
      RETURN BOOLEAN EXECUTE _sql_string;
      --RETURN TABLE   EXECUTE _sql_string; doesn't work  
  END
  $$;
  </code></pre>
  
  <p>The following should work, but I keep getting <code>ERROR: syntax error at or near "EXECUTE"</code></p>
  
  <p>Please let me know the error of my ways.</p>