title: if-statement with string containing the condition
body: <p>This question is about <strong>Postgresql 8.3</strong>.</p>
  
  <p>I've got a table with a field containing conditions like 'lastcontact is null'. In code, I want to loop through this table and for each record, I want to check 'if condition then', like in this example:</p>
  
  <pre><code>FOR myrec IN
      SELECT * FROM tabel ORDER BY colorlevel, volgnummer
  LOOP
      if (myrec.conditie) then
          raise notice 'Condition % is true', myrec.conditie;
      else
          raise notice 'Condition % is false', myrec.conditie;
      end if;
  END LOOP;
  </code></pre>
  
  <p>The table which I have called 'tabel' in this example:</p>
  
  <pre><code>ID | Conditie             | Colorlevel | Volgnummer | Code | Description
  1  | lastcontact is null  | 1          | 1          | ...  | ...
  2  | lastchanged is null  | 1          | 2          | ...  | ...
  3  | lastmodified is null | 1          | 3          | ...  | ...
  </code></pre>
  
  <p>Is it possible to do the check I desire? The code above results in the following error:</p>
  
  <pre><code>ERROR:  invalid input syntax for type boolean: "lastcontact is null"
  </code></pre>
  
  <hr>
  
  <p><em><strong>New section containing the result of Erwin's function</em></strong></p>
  
  <p>I have used this function:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION foo(lastcontact timestamptz)
    RETURNS void AS
  $BODY$
  DECLARE
    myrec record;
    mycond boolean;
  BEGIN
  
  FOR myrec IN
      SELECT * FROM tabel ORDER BY colorlevel, volgnummer
  LOOP
      EXECUTE 'SELECT ' || myrec.conditie || ' FROM tabel' INTO mycond;
  
      IF mycond then
          RAISE NOTICE 'Condition % is true', myrec.conditie;
      ELSE
          RAISE NOTICE 'Condition % is false', COALESCE(myrec.conditie, 'NULL');
      END IF;
  END LOOP;
  
  END;
  $BODY$
  language 'plpgsql' volatile
  cost 100;
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>ERROR:  column "lastcontact" does not exist
  LINE 1: SELECT lastcontact is null FROM tabel
             ^
  QUERY:  SELECT lastcontact is null FROM tabel
  CONTEXT:  PL/pgSQL function "foo" line 9 at EXECUTE statement1
  </code></pre>
  
  <p>I tried to find an explanation myself, but to no avail. It obviously is trying to run the statement against the database, but it should understand that 'lastcontact' is the variable that's been given as a function parameter.</p>