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 > Identify records modified based on time

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-25-07, 00:18
ggnanaraj ggnanaraj is offline
Registered User
 
Join Date: Aug 2002
Location: Chennai, India
Posts: 171
Identify records modified based on time

I have a small requirement where by I want to retrieve all the records in a table modified since last 24 hours. Can anyone please help me out with this query?

Is this possible?

TIA.
Reply With Quote
  #2 (permalink)  
Old 09-25-07, 04:19
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
add a column (datatype timestamp) and for each new row update this column as current timestamp
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #3 (permalink)  
Old 09-25-07, 04:19
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
or create trigger on that table
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #4 (permalink)  
Old 09-25-07, 04:26
ggnanaraj ggnanaraj is offline
Registered User
 
Join Date: Aug 2002
Location: Chennai, India
Posts: 171
Thanks for the input provided.

However, in absence of such a column for the table, can we also use the transaction logs to identify the inserts/updates to a table? I was thinking of using Recovery Expert for this? Is this feasible?

TIA.
Reply With Quote
  #5 (permalink)  
Old 09-25-07, 04:34
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
ggnanaraj, rahul_s80 suggestion is nice one - creating trigger is one of solution.

Sample (Trigger fires up before update of column col1):
Code:
CREATE TABLE DB2ADMIN.TAB1 (
                  COL1 INTEGER ,
                  COL2 TIMESTAMP DEFAULT CURRENT TIMESTAMP)@

INSERT INTO DB2ADMIN.TAB1 VALUES (1,CURRENT TIMESTAMP) @
INSERT INTO DB2ADMIN.TAB1 VALUES (2,CURRENT TIMESTAMP) @

CREATE TRIGGER DB2ADMIN.TRIG
BEFORE UPDATE OF COL1 ON DB2ADMIN.TAB1
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN ATOMIC
SET NEW.COL2 = CURRENT TIMESTAMP;
END @
Save commands in file.sql and execute above commands by:
Code:
db2 -td@ -f file.sql
Note: default definition at col2 column is to get the timestamp executed by insert statements.

See data before update:
Code:
SELECT * FROM DB2ADMIN.TAB1
Execute the following SQL to see the change:
Code:
UPDATE DB2ADMIN.TAB1 SET COL1=100 WHERE COL1=2
Check data one more time:
Code:
SELECT * FROM DB2ADMIN.TAB1
Have you noticed the timestamp has only changed in col1=100 row.

Hope this helps,
Grofaty

Last edited by grofaty; 09-25-07 at 04:41.
Reply With Quote
  #6 (permalink)  
Old 09-25-07, 04:43
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Quote:
Originally Posted by ggnanaraj
Thanks for the input provided.
However, in absence of such a column for the table
Hi,
create new column:
alter table schema.table_name add column new_column_name

Quote:
Originally Posted by ggnanaraj
I was thinking of using Recovery Expert for this?
Why searching for a difficult solution if there is simple one...

Hope this helps,
Grofaty
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