2

I am having trouble getting this code to work. This is executed as a result of a insert, update or delete trigger. The same code is going to be called from several table triggers. Therefore, I don't know pre-hand what the table names are or what their columns are. Any ideas?

EXECUTE format('INSERT INTO %s VALUES %s', _tbl, (OLD).*);

Error is about the strings not being surrounded with quotes:

ERROR:  column "bangor" does not exist
LINE 1: INSERT INTO sample_test_table_deletes VALUES (3,bangor,D,"20...
                                                          ^

On the other hand, the following works:

INSERT INTO my_table VALUES((OLD).*);

That is, when the query is not through an execute block.

Update (Trigger Code):

CREATE OR REPLACE FUNCTION sample_trigger_func() RETURNS TRIGGER AS $$
DECLARE
    operation_code char;
    table_name varchar(50);
    delete_table_name varchar(50);
    old_id integer; 

BEGIN
    table_name = TG_TABLE_NAME;
    delete_table_name = TG_TABLE_NAME || '_deletes';

    SELECT SUBSTR(TG_OP, 1, 1)::CHAR INTO operation_code;

    IF TG_OP = 'DELETE' THEN
        OLD.mod_op = operation_code;
        OLD.mod_date = now();

        RAISE INFO 'OLD: %', (OLD).name;

        EXECUTE format('INSERT INTO %s VALUES %s', delete_table_name, (OLD).*);

    ELSE
        EXECUTE format('UPDATE TABLE %s SET mod_op = %s AND mod_date = %s'
                      , TG_TABLE_NAME, operation_code, now());
    END IF;

    RETURN NEW;
END;
$$ LANGUAGE plpgsql;
5
  • What is the error you get? Where does _tbl com from? What is the complete code of the trigger? Commented May 4, 2015 at 13:54
  • And what about the trigger code? Please don't play 20 questions with us... Commented May 4, 2015 at 14:10
  • Added the trigger code Commented May 4, 2015 at 14:19
  • You write: This is executed as a result of a delete trigger. But then you have an ELSE twig for INSERT / UPDATE .. which is completely invalid. Clarify what you are trying to do, please. I doubt every possible original table has a column mod_op? I suggest you start a new question. Focus this one on the topic asked and put the rest in a new question. Commented May 4, 2015 at 14:47
  • Erwin, actually, it is not a delete trigger, but an insert, update or delete trigger. I am sorry for the confusion. Also, yes, every table has a mod_op column. I updated the question. Do you have anything else wrong in this too? Commented May 4, 2015 at 15:25

1 Answer 1

3

The special variable OLD is not visible inside EXECUTE. (Nor is any other variable in the scope of the function.) Pass in values with the USING construct:

EXECUTE format('INSERT INTO %I VALUES (($1).*)', _tbl)
USING OLD;

Also, use %I for the identifier, not %s.

Details:

Sign up to request clarification or add additional context in comments.

4 Comments

But your added trigger function has a couple of additional serious problems.
Hi Erwin. Could you suggest the improvements? Do they need to come in the form of answers to a different question? Thanks.
@picmate: I answered your question asked and then saw the problems in the function you added later. The policy here is to ask one question per issue to keep things clear and useful for the general public.
Hi Erwin. I started a new question: stackoverflow.com/questions/30034194/problems-with-the-trigger. Hope you will be able to take a look at it and give some suggestions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.