title: Is there any way to use a variable with data change triggers (old./new.) for a trigger function?
body: <p>I am writing a function in PostgreSQL for a trigger where I want to log a change (update) on one table (core_table) and insert it into another table (audit_table) but only have it insert the columns that were actually changed. The way I am going about it is this:</p>
  <pre><code>IF (old.color &lt;&gt; new.color) then
  insert into audit_table select 'core_table', Data, old.color, new.color;
  end if;
                      
  </code></pre>
  <p>I have it working hardcoded (where I have ~40 IF statements to handle each column) but I want to turn it into a loop.  I have a dynamic SQL inside this function that returns the name of the next column for each iteration into a variable named &quot;Data&quot; (on loop 1 Data = &quot;color&quot;, loop 2 Data = &quot;model&quot;, etc.)</p>
  <p>The problem now is that when I replace old.color with old.Data it tells me that there is no column named &quot;Data&quot; (SQL Error [42703]: record &quot;old&quot; has no field &quot;column_name&quot;) it is not reading the value stored in Data (which is &quot;color&quot;) but the name of the variable itself (&quot;Data&quot;).</p>
  <p>So my question is this, is there any way to use a variable with data change triggers (old./new.) for a trigger function?</p>