If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > updating Timestamp field

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-08-04, 14:17
ricgre ricgre is offline
Registered User
 
Join Date: Aug 2004
Posts: 5
updating Timestamp field

Does DB2 have the ability to automatically update a timestamp field as does MY SQL or does this field need to be manipulated through a SQL statement. I have declared a timestamp field with default currenttimestamp and is initialized on inserts. Now I would like to have it automatically adjusted on updates. If this possible?

Ric
Reply With Quote
  #2 (permalink)  
Old 09-08-04, 14:26
n_i n_i is online now
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
You could use a trigger for that.
Reply With Quote
  #3 (permalink)  
Old 09-08-04, 14:27
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You can do that with a before update trigger.

Andy
Reply With Quote
  #4 (permalink)  
Old 09-08-04, 15:45
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Andy, Are you sure it is a before update trigger ... If i remember right, you cannot do INSERT Operation in a before trigger ..

Cheers
Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #5 (permalink)  
Old 09-08-04, 15:53
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
The way I read the original post was that every time that an update occurs to a row of the table, he wants a timestamp column set to the current timestamp (presumably the column name is "updated_on" or something similar).
This scenario calls for a before update trigger.

Andy
Reply With Quote
  #6 (permalink)  
Old 09-08-04, 16:39
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Yes ... you r right

Cheers
sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #7 (permalink)  
Old 09-09-04, 10:09
ricgre ricgre is offline
Registered User
 
Join Date: Aug 2004
Posts: 5
Thanks for the responses. I am new to DB2 and just doing some testing. Do I have actually specify a column?

CREATE TRIGGER TriggerName
BEFORE UPDATE ON TABLENAME
fOR EACH ROW MODE DB2SQL
UPDATE TABLENAME SET LASTUPDATED = curenttimestamp
END

Does this look look like the correct logic? I am using DB2 8.1 on V5r3 with the Navigator contol. I have heard that there is a Windows Control Center similiar to Navigator. Has anyone used this? I am primarily a Visual Studio developer who is migrating to database responsibilites. Any recommendations on resource db2 books or training classes.

Ric
Reply With Quote
  #8 (permalink)  
Old 09-09-04, 10:28
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Try setting the transition variable instead of updating the table:

CREATE TRIGGER TriggerName
NO CASCADE BEFORE UPDATE ON TABLENAME
REFERENCING OLD AS OLD
NEW AS NEW
fOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
SET NEW.LASTUPDATED = curent timestamp;
END

(Don't forget to set your SQL terminator to something besides semicolon when creating the trigger.)
Reply With Quote
  #9 (permalink)  
Old 09-13-04, 11:36
ricgre ricgre is offline
Registered User
 
Join Date: Aug 2004
Posts: 5
Trigger Problem

I have tried the trigger above and the one listed below and keep getting an error 4207. The library exists, I have created tables and populated them. I have full admin rights on all objects. Any ideas what the issue may still be?

CREATE TRIGGER RACK_UPDATE
BEFORE UPDATE OF FLDSLOT1 ON CSSDATA.TBL_RACKSLOT
FOR EACH ROW MODE DB2SQL
WHEN (NEW.FLDSLOT1 <> OLD.FLDSLOT1)
BEGIN ATOMIC
UPDATE CSSDATA.TBL_RACKSLOT SET FLDUPDATED = CURRENT TIMESTAMP;
END

Is the terminator correct? This is what I have used in MY SQL and MS SQL script.

When I insert the referencing statements I get a different error. Number 42899. I have tried removing the WHEN statement.

Ric

Last edited by ricgre; 09-13-04 at 11:40.
Reply With Quote
  #10 (permalink)  
Old 09-13-04, 12:19
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
The TERMINATOR is what the SQL program uses to end an SQL statement. In general, the terminator is a semicolon. Since the trigger has a semicolon in the code, you have to tell the DBMS that you want to terminate the CREATE TRIGGER STATEMENT with something else, like $ or ~.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On