title: Postgres - Dynamically referencing columns from record variable
body: <p>I'm having trouble referencing record variable type columns dynamically. I found loads of tricks online, but with regards to triggers mostly and I really hope the answer isn't "it can't be done"... I've got a very specific and simple need, see my example code below;</p>
  
  <p>First I have an array containing a list of column names called "<strong>lCols</strong>". I loop through a record variable to traverse my data, replacing values in a paragraph which exactly match my column names.</p>
  
  <pre><code>DECLARE lTotalRec RECORD;        
  DECLARE lSQL text;
  DECLARE lCols varchar[];
  
  p_paragraph:= 'I am [names] and my surname is [surname]';
  
  lSQL := 
      'select         
          p.names,
          p.surname
      from 
          person p
      ';
  
  FOR lTotalRec IN
      execute lSQL        
  LOOP
      -- Loop through the already created array of columns to replace the values in the paragraph
      FOREACH lVal IN ARRAY lCols
      LOOP
          p_paragraph := replace(p_paragraph,'[' || lVal || ']',lTotalRec.lVal); -- This is where my problem is, because lVal is not a column of lTotalRec directly like this      
      END LOOP;
  
      RETURN NEXT;
  END LOOP;
  </code></pre>
  
  <p>My return value is the paragraph amended for each record in <strong>"lTotalRec"</strong></p>