IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

To maintain Audit log for Employee table


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
karthi_ind

Active User


Joined: 24 Feb 2004
Posts: 131
Location: Chennai

PostPosted: Wed Mar 10, 2010 11:59 am
Reply with quote

I have one table called Employee and detail would like below

EMP_TABLE
S.No Emp_Id EmP_Name Emp_Salary
1 123 John 100
5 123 Sunil 100
7 123 Raj 500
10 123 Joseph 101

When ever i do the update on the columns like Emp_Name or Emp_Salary a new record is inserted into the same table with new S.No
For Ex. Emp_Name "John" Changed to "Sunil" new record is inserted into the table with S.No"5".

Note:Emp_ID will remain same and it can't be changed/updated.

Now My requirement is "I should generate the report(using Cursors in SQL Stored procedure) for audit log" for the "employee_Id 123" The report should be like below

Changed Field Old Value New value
Employee Name John Sunil
Employee Name Sunil Raj
Employee Nmae Raj Joseph
Employee Salary 100 500
Employee Salary 500 101
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Wed Mar 10, 2010 4:37 pm
Reply with quote

well, good luck with that. It won't be easy.
Back to top
View user's profile Send private message
sushanth bobby

Senior Member


Joined: 29 Jul 2008
Posts: 1020
Location: India

PostPosted: Wed Mar 10, 2010 5:03 pm
Reply with quote

Karthi,

You got to be kidding me !
Quote:
a new record is inserted into the same table with new S.No

Single table holding employee records & audit records as well. Did normalization went for a vacation or what ?

Sushanth
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Wed Mar 10, 2010 5:10 pm
Reply with quote

True - you should have been using another table for audit with a trigger on the base table.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Mar 10, 2010 9:26 pm
Reply with quote

Hello,

I suspect this is the audit table. . . I may misunderstand, but i believe the requirement is to show all of the before/after values when there have been modifications.

Possibly Karthi will clarify. . .
Back to top
View user's profile Send private message
karthi_ind

Active User


Joined: 24 Feb 2004
Posts: 131
Location: Chennai

PostPosted: Thu Mar 11, 2010 12:26 pm
Reply with quote

Hi all,

Thanks for your reply.

Basically what I want is the report kind of thing which tells about what are all the changes done in the table. Like empname has been changed to new name, salary has been changed to new one. This needs to be done using sql query in stored procedure.

Sushanth,

The table contains multiple records and I want only the changes done for name and salary. so I dont think normalization comes into picture on this.

Please clarify.

Thanks.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Thu Mar 11, 2010 1:56 pm
Reply with quote

You could use this as starting point :
Code:
select old.id, old.no, old.name, old.sal, new.no,new.name, new.sal
from EMP_TABLE old
      , EMP_TABLE new
where old.id = new.id
  and (   old.Name = new.name
       or old.sal  = newt.Sal)
  and new.no = (select min(no) from EMP_table nxt
                 where old.id = nxt.id
                   and nxt.no > old.no
                )
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Thu Mar 11, 2010 4:38 pm
Reply with quote

in previous post, ofcourse it must be

(old.name <> new.name or old.sal <> new.sal)
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Load new table with Old unload - DB2 DB2 6
No new posts Pulling a fixed number of records fro... DB2 2
No new posts Multiple table unload using INZUTILB DB2 2
No new posts Check data with Exception Table DB2 0
No new posts Dynamically pass table name to a sele... DB2 2
Search our Forums:

Back to Top