0

I often have to create trigger which will copy all contents of the rows on insert/update to another table. As some tables have 200 columns this often is a long writing of

CREATE TRIGGER scheme.trigger AFTER UPDATE ON scheme.table
  REFERENCING OLD as o_row NEW as n_row
  FOR EACH ROW
BEGIN
  INSERT INTO archive (...) VALUES(...);
END;

This is a lot of typing. Is there an easy generator to build these type of triggers, inserts, updates?

3
  • 4
    I haven't done DB2 in years, but isn't there a data dictionary table you can reference to get the column names? Write a stored proc to output the above, while inserting into that output the list of columns? I have done that in Oracle and SQL Server, so I assume that is how you would do it in DB2. Commented May 10, 2011 at 12:20
  • I am planning to do an assistant tool for that but I am a bit short on time currently and was wondering if there is no freeware tool available doing this. Commented May 10, 2011 at 12:38
  • 1
    start with "select * from systables" and go from there. I suspect it is far easier to roll your own than to search for the exact tool. this is not that difficult a problem. Commented May 10, 2011 at 13:59

2 Answers 2

1

If the process has a set of input parameters and the process with these parameters is the same, you can call a stored procedure from the trigger, passing the parameters. In this way, you do not have to recopy/recreate all these DML sentences.

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

2 Comments

This sounds quite good but the count of parameters differs from table to table. Are variable parameter lengths supported by iSeries DB2 stored procedures (maybe you know)?
Accepted as there is no tool and this is the most flexible way to handle it manually.
1

I don't know of a tool that generates the CREATE TRIGGER statements for you, but I've generated that DDL from a query many times, and the only view I ever needed to references was SYSCAT.COLUMNS

The following factors will simplify the trigger DDL generator query you'll be writing: - Column names between original table and archive table are identical. - None of the columns in the target table are defined as GENERATED ALWAYS, otherwise you'll need to omit them from your INSERT statement.

Comments

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.