Question: Is there a way in (maybe pl/pgsql function way?) to create an INSERT/UPDATE/DELETE query on a specific table using the values found in my log table (namely the values 'action', 'schema_name', 'table_name', 'column_name', 'data_type' (i.e. the column data type), and 'new_val'?).
The table being logged and the table that I need to run the INSERT/UPDATE/ or DELETE on looks like this:
..and the log table looks like this:
...the 4 highlighted log entries should be INSERT'ed into the table like this:
... I'm trying to find a way to run a INSERT/UPDATE/or DELETE on ANOTHER DATABASE table (which is identical in names/schema/etc to the table being logged) after a selection of the specific 'usr' and 'event_date' in the logging table.
To just get the results I want (for the INSERT statement only - see below) the SQL is pretty knarly (demo in SQL FIDDLE). I'm pretty interested in finding out if another way is possible....
INSERT INTO Engineering.Elective_Courses 
            (gid, grade, class, student_id) 
    WITH
    t1 AS
    (Select new_val 
    From student.history
    WHERE
       column_name = 'gid'
    AND
       usr = 'Principal K.'
    AND
       (event_date >= '2017-01-26' AND event_date <  '2017-01-29')),
    t2 AS (Select new_val 
    From student.history
    WHERE
       column_name = 'grade'
    AND
       usr = 'Principal K.'
    AND
       (event_date >= '2017-01-26' AND event_date <  '2017-01-29')),
    t3 AS (Select new_val 
    From student.history
    WHERE
       column_name = 'class'
    AND
       usr = 'Principal K.'
    AND
       (event_date >= '2017-01-26' AND event_date <  '2017-01-29')),
    t4 AS (Select new_val 
    From student.history
    WHERE
       column_name = 'student_id'
    AND
       usr = 'Principal K.'
    AND
       (event_date >= '2017-01-26' AND event_date <  '2017-01-29'))
     select t1.new_val::int, t2.new_val, t3.new_val, t4.new_val::int
      from t1,t2, t3, t4;



 
     
     
    