Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DB2 SQL Trigger - Date Stamp single row update

Posted on 2009-04-08
10
Medium Priority
?
1,407 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 46

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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 46

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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

971 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