Solved

DB2 SQL Trigger - Date Stamp single row update

Posted on 2009-04-08
10
1,401 Views
Last Modified: 2012-05-06
Hi All, I need to be able to update a single row in a DB table using a trigger so I can log when a particular row was last updated.

I have reviewed similar questions which point to using  the Getdate() function but this doesn't seem to be avaliable in db2

Is there an alternative I can use?

Thanks in advance

0
Comment
Question by:taylor99
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 12

Expert Comment

by:Dimitris
ID: 24095738
The CURRENT DATE special register gives the current system date and CURRENT TIMESTAMP the current system date and time.

sample

SELECT CURRENT TIMESTAMP
FROM SYSIBM.SYSDUMMY1
or
ELECT CURRENT DATE
FROM SYSIBM.SYSDUMMY1
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 24096237
Hi taylor,

Dan's got it right.  :)

Putting the whole thing into context, the trigger would look something like this:

CREATE TRIGGER timestamp_mytable
  NO CASCADE BEFORE UPDATE ON mytable
  REFERENCING OLD AS o NEW AS n
  FOR EACH ROW MODE DB2SQL
  BEGIN ATOMIC
    SET n.updatetime = current timestamp
END

Good Luck,
Kent
0
 

Author Comment

by:taylor99
ID: 24096738
Thanks, so how would I then use this to update a specific cell in a single DB row when another cell in that row is updated?

Below is what I've attempted to use but the whole column is updated.  Can you use an alternative for the FOR EACH ROW statement which will point to a specific row?

CREATE TRIGGER SCHEMA."TABLE1" AFTER  UPDATE OF PRICE ON TABLE1
FOR EACH ROW  
MODE DB2SQL
UPDATE TABLE1
   SET DATE_ENTERED = CURRENT DATE
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Dimitris
ID: 24096836
The trigger you have write will update the ENTIRE TABLE each time
I think that the solution has been provided for this issue by Kdo
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 24096958
Hi taylor.

Your trigger is close, but misses a critical subtlety.

Look that the UPDATE...SET portion of your trigger.  If you execute that you will update the DATE_ENTERED field for every row in the table.  Since what you want is to update only the modified row, you need to tell the trigger to filter out the unchanged rows.

You do that with the REFERENCING clause.  

  REFERENCING OLD AS o NEW AS n

Within the trigger, n.date_entered is the column (field) in the current (changed) row that you want to update.  Omitting the 'n.' causes the change to be cascaded through the entire table.

CREATE TRIGGER SCHEMA."TABLE1"
AFTER  UPDATE OF PRICE ON TABLE1
  REFERENCING OLD AS o NEW AS n
FOR EACH ROW  
MODE DB2SQL
UPDATE TABLE1
   SET n.DATE_ENTERED = CURRENT DATE



Good Luck,
Kent
0
 
LVL 27

Expert Comment

by:tliotta
ID: 24101484
taylor99:

What platform/operating system? What version/release of DB2?

Also, I can imagine two scenarios that kind of fit with what you've said. Can you clarify which one is correct?

  1. You have a table that clients update. The table has a column for a Last_Change TIMESTAMP. You want a trigger to populate that column with CURRENT TIMESTAMP whenever a client updates the row (or when a row is inserted perhaps).

  2. You have two tables. Clients update the first table. You want a trigger to log all updates into the second table, including a CURRENT TIMESTAMP value.
Tom
0
 

Author Comment

by:taylor99
ID: 24105387
Hi Kent,

Thanks for your input, sorry if my reply yesterday didn't make sense.  I hadn't refreshed my browser so hadn't seen your reply.  This seems to be exactly what I'm after but having attempted to create the trigger using your example I get the error below, although I assure you DATE_ENTERED is a valid column in my table.  

"DATE_ENTERED" is not valid in the context where it is used.

My trigger code is

CREATE TRIGGER SCHEMA."TABLE1 INTERNAL  SET PRICE UPDATE DATE STAMP" NO CASCADE
BEFORE  UPDATE OF PRICE ON SCHEMA.TABLE1  
REFERENCING  OLD AS OROW  NEW AS NROW  FOR EACH ROW  
MODE DB2SQL
UPDATE SCHEMA.TABLE1
   SET NROW.DATE_ENETERED = CURRENT DATE
0
 
LVL 12

Accepted Solution

by:
Dimitris earned 500 total points
ID: 24105427
try this

CREATE TRIGGER SCHEMA."TABLE1 INTERNAL  SET PRICE UPDATE DATE STAMP" NO CASCADE
BEFORE  UPDATE OF PRICE ON SCHEMA.TABLE1  
REFERENCING  OLD AS OROW  NEW AS NROW  FOR EACH ROW  
MODE DB2SQL
BEGIN ATOMIC
   SET NROW.DATE_ENETERED = CURRENT DATE

 
0
 

Author Comment

by:taylor99
ID: 24105432
Hi Tom,

In answer to your questions I am running DB2 9.5.2 under Windows XP and the first senario you suggested is the correct one.

Cheers
0
 

Author Closing Comment

by:taylor99
ID: 31567928
Spot on thanks guys
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question