View previous topic :: View next topic
|
Author |
Message |
karthi_ind
Active User
Joined: 24 Feb 2004 Posts: 131 Location: Chennai
|
|
|
|
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 |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
well, good luck with that. It won't be easy. |
|
Back to top |
|
|
sushanth bobby
Senior Member
Joined: 29 Jul 2008 Posts: 1020 Location: India
|
|
|
|
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 |
|
|
ashimer
Active Member
Joined: 13 Feb 2004 Posts: 551 Location: Bangalore
|
|
|
|
True - you should have been using another table for audit with a trigger on the base table. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
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 |
|
|
karthi_ind
Active User
Joined: 24 Feb 2004 Posts: 131 Location: Chennai
|
|
|
|
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 |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
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 |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
in previous post, ofcourse it must be
(old.name <> new.name or old.sal <> new.sal) |
|
Back to top |
|
|
|