473,513 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add a datestamp to an existing table

Hi all,

I have an existing table(Several actually) that I want to add a column
or two or three. I need to have, I think, at least two columns. One
for Created date and believe this is a no brainer in that I just add
the column and set the default to getdate(). Two, for tracking a
modified date of the row of data. I want to keep it simple. What do I
do to track the modify date of a row in SQL Server 2005? Anytime any
data changes on a row update the update column? Is a trigger the only
way to accomplish this task?

Another column could be used to track who changed it. Is there a neat
way to determine username, machine name, MAC address or any other
method? I pretty wide open on this one as I am not sure what I want to
use here.

TIA!!!
Dec 4 '07 #1
16 4883
On Tue, 4 Dec 2007 10:06:24 -0800 (PST), scoots987 wrote:
>Hi all,

I have an existing table(Several actually) that I want to add a column
or two or three. I need to have, I think, at least two columns. One
for Created date and believe this is a no brainer in that I just add
the column and set the default to getdate(). Two, for tracking a
modified date of the row of data. I want to keep it simple. What do I
do to track the modify date of a row in SQL Server 2005? Anytime any
data changes on a row update the update column? Is a trigger the only
way to accomplish this task?
Hi scoots987,

Yes, a trigger is the only way. There is no builtin standard out of the
box functionality for this.
>Another column could be used to track who changed it. Is there a neat
way to determine username, machine name, MAC address or any other
method? I pretty wide open on this one as I am not sure what I want to
use here.
Check out SUSER_SNAME() and HOST_NAME() in Books Online.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Dec 4 '07 #2
Hugo Kornelis (hu**@perFact.REMOVETHIS.info.INVALID) writes:
>>Another column could be used to track who changed it. Is there a neat
way to determine username, machine name, MAC address or any other
method? I pretty wide open on this one as I am not sure what I want to
use here.

Check out SUSER_SNAME() and HOST_NAME() in Books Online.
There's far too many functions for the current user for it to be
healthy.

The best to use, though, is probably original_login(), because the
others (SESSION_USER, SYSTEM_USER, suser_sname() etc) gives incorrect
information if there is an EXECUTE AS clause somewhere along the line.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Dec 4 '07 #3
On Dec 4, 4:33 pm, Erland Sommarskog <esq...@sommarskog.sewrote:
Hugo Kornelis (h...@perFact.REMOVETHIS.info.INVALID) writes:
>Another column could be used to track who changed it. Is there a neat
way to determine username, machine name, MAC address or any other
method? I pretty wide open on this one as I am not sure what I want to
use here.
Check out SUSER_SNAME() and HOST_NAME() in Books Online.

There's far too many functions for the current user for it to be
healthy.

The best to use, though, is probably original_login(), because the
others (SESSION_USER, SYSTEM_USER, suser_sname() etc) gives incorrect
information if there is an EXECUTE AS clause somewhere along the line.

--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se

Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Thanks for the replies.

Are there examples of creating a trigger for tracking table data
changes? Since I never created one before.

Thanks again...

Dec 5 '07 #4
scoots987 (sc*******@gmail.com) writes:
Are there examples of creating a trigger for tracking table data
changes? Since I never created one before.
CREATE TRIGGER mytri ON mytbl FOR INSERT, UPDATE ON
UPDATE mytbl
SET moduser = original_login(),
moddate = getdate()
FROM mytbl m
JOIN inserted i ON m.keycol1 = i.keycol1
AND m.keycol2 = i.keycol2
...
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Dec 5 '07 #5
On Wed, 5 Dec 2007 06:02:59 -0800 (PST), scoots987 wrote:
>On Dec 4, 4:33 pm, Erland Sommarskog <esq...@sommarskog.sewrote:
>Hugo Kornelis (h...@perFact.REMOVETHIS.info.INVALID) writes:
>>Another column could be used to track who changed it. Is there a neat
way to determine username, machine name, MAC address or any other
method? I pretty wide open on this one as I am not sure what I want to
use here.
Check out SUSER_SNAME() and HOST_NAME() in Books Online.

There's far too many functions for the current user for it to be
healthy.

The best to use, though, is probably original_login(), because the
others (SESSION_USER, SYSTEM_USER, suser_sname() etc) gives incorrect
information if there is an EXECUTE AS clause somewhere along the line.

--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se

Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Thanks for the replies.

Are there examples of creating a trigger for tracking table data
changes? Since I never created one before.

Thanks again...
Hi scooots987,

If you choose a design with a seperate history table that holds all
versions of the data (keyed on the primary key of the table plus a
datetime column that records the moment any change took place), use
something like this

CREATE TRIGGER ins_MyTable
ON MyTable
FOR INSERT
AS
INSERT INTO HistoryOfMyTable
(KeyCol1, KeyCol2, DataCol1, DataCol2,
TypeOfChange, MomentOfChange, Whodunnit)
SELECT KeyCol1, KeyCol2, DataCol1, DataCol2,
'Insert', CURRENT_TIMESTAMP(), ORIGINAL_LOGIN()
FROM inserted;

CREATE TRIGGER upd_MyTable
ON MyTable
FOR UPDATE
AS
INSERT INTO HistoryOfMyTable
(KeyCol1, KeyCol2, DataCol1, DataCol2,
TypeOfChange, MomentOfChange, Whodunnit)
SELECT KeyCol1, KeyCol2, DataCol1, DataCol2,
'Update', CURRENT_TIMESTAMP(), ORIGINAL_LOGIN()
FROM inserted;

CREATE TRIGGER del_MyTable
ON MyTable
FOR DELETE
AS
INSERT INTO HistoryOfMyTable
(KeyCol1, KeyCol2, DataCol1, DataCol2,
TypeOfChange, MomentOfChange, Whodunnit)
SELECT KeyCol1, KeyCol2, NULL, NULL,
'Delete', CURRENT_TIMESTAMP(), ORIGINAL_LOGIN()
FROM deleted;

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Dec 5 '07 #6
You need to look a third party audit tool that will meet the legal
requirements. Think about what happens when you do a delete or the DB
crashes. What are you going to tell the court?
Dec 6 '07 #7
--CELKO-- wrote:
You need to look a third party audit tool that will meet the legal
requirements. Think about what happens when you do a delete or the DB
crashes. What are you going to tell the court?
Given what Goldman Sachs, Morgan Stanley, Salomon Smith Barney, Deutsche
Bank and US Bancorp Piper Jaffray were told by the court perhaps:
"How large a check should we write?"

http://www.icpas.org/hc-insight.aspx?id=892
http://findarticles.com/p/articles/m...2/ai_n17809730
--
Daniel A. Morgan
Oracle Ace Director & Instructor
University of Washington
da******@x.washington.edu (replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org
Dec 6 '07 #8
On Dec 6, 11:12 am, --CELKO-- <jcelko...@earthlink.netwrote:
You need to look a third party audit tool that will meet the legal
requirements. Think about what happens when you do a delete or the DB
crashes. What are you going to tell the court?
you are jumping to conclusions without knowing anything about the
problem. What if there are no legal issues at all?
Dec 7 '07 #9
> you are jumping to conclusions without knowing anything about the problem. What if there are no legal issues at all? <<

1) I live in the Untied States where we have one lawyer for every 400
citizens. Look up the figures for Japan to get an idea how bad this
is. So, there is a always a legal issue :) And if there isn't one
now, there will next week.

2) Putting audit data in the same table is like putting the LOG file
on the same hard drive as the DB. You guarantee that recovery will be
impossible. An audit trail should require at least two "signatures"
to change an audit entry; this schema allows one user to do that if he
has access to the timestamps. This is simply good programming, even
without the lawyers.

Dec 8 '07 #10
--CELKO-- wrote:
>> you are jumping to conclusions without knowing anything about the problem. What if there are no legal issues at all? <<

1) I live in the Untied States where we have one lawyer for every 400
citizens. Look up the figures for Japan to get an idea how bad this
is. So, there is a always a legal issue :) And if there isn't one
now, there will next week.

2) Putting audit data in the same table is like putting the LOG file
on the same hard drive as the DB. You guarantee that recovery will be
impossible. An audit trail should require at least two "signatures"
to change an audit entry; this schema allows one user to do that if he
has access to the timestamps. This is simply good programming, even
without the lawyers.
Joe is absolutely correct. In the US, at least, there are always
legal issues. Anyone that pretends they are not around the corner
needs a heavy dose of reality.

An audit trail, by definition, must be unalterable by anyone with
access to the hardware or software. And in many countries that isn't
just who altered the data ... but who saw it as it is, for example,
under HIPAA.
--
Daniel A. Morgan
University of Washington
da******@x.washington.edu (replace x with u to respond)
Dec 8 '07 #11
On Dec 5, 12:35 pm, Hugo Kornelis
<h...@perFact.REMOVETHIS.info.INVALIDwrote:
On Wed, 5 Dec 2007 06:02:59 -0800 (PST), scoots987 wrote:
On Dec 4, 4:33 pm, Erland Sommarskog <esq...@sommarskog.sewrote:
Hugo Kornelis (h...@perFact.REMOVETHIS.info.INVALID) writes:
Another column could be used to track who changed it. Is there a neat
way to determine username, machine name, MAC address or any other
method? I pretty wide open on this one as I am not sure what I want to
use here.
Check out SUSER_SNAME() and HOST_NAME() in Books Online.
There's far too many functions for the current user for it to be
healthy.
The best to use, though, is probably original_login(), because the
others (SESSION_USER, SYSTEM_USER, suser_sname() etc) gives incorrect
information if there is an EXECUTE AS clause somewhere along the line.
--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Thanks for the replies.
Are there examples of creating a trigger for tracking table data
changes? Since I never created one before.
Thanks again...

Hi scooots987,

If you choose a design with a seperate history table that holds all
versions of the data (keyed on the primary key of the table plus a
datetime column that records the moment any change took place), use
something like this

CREATE TRIGGER ins_MyTable
ON MyTable
FOR INSERT
AS
INSERT INTO HistoryOfMyTable
(KeyCol1, KeyCol2, DataCol1, DataCol2,
TypeOfChange, MomentOfChange, Whodunnit)
SELECT KeyCol1, KeyCol2, DataCol1, DataCol2,
'Insert', CURRENT_TIMESTAMP(), ORIGINAL_LOGIN()
FROM inserted;

CREATE TRIGGER upd_MyTable
ON MyTable
FOR UPDATE
AS
INSERT INTO HistoryOfMyTable
(KeyCol1, KeyCol2, DataCol1, DataCol2,
TypeOfChange, MomentOfChange, Whodunnit)
SELECT KeyCol1, KeyCol2, DataCol1, DataCol2,
'Update', CURRENT_TIMESTAMP(), ORIGINAL_LOGIN()
FROM inserted;

CREATE TRIGGER del_MyTable
ON MyTable
FOR DELETE
AS
INSERT INTO HistoryOfMyTable
(KeyCol1, KeyCol2, DataCol1, DataCol2,
TypeOfChange, MomentOfChange, Whodunnit)
SELECT KeyCol1, KeyCol2, NULL, NULL,
'Delete', CURRENT_TIMESTAMP(), ORIGINAL_LOGIN()
FROM deleted;

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog:http://sqlblog.com/blogs/hugo_kornelis- Hide quoted text -

- Show quoted text -
Thank you Hugo. Among the other replies, I appreciate the example. I
will review this and make the necessary changes.

Dec 9 '07 #12
On Dec 6, 11:12 am, --CELKO-- <jcelko...@earthlink.netwrote:
You need to look a third party audit tool that will meet the legal
requirements. Think about what happens when you do a delete or the DB
crashes. What are you going to tell the court?
Thanks Joe, I own one of your books. I need to dig into it again.

May I ask why legal requirements came about in this discussion? I
don't see any in a custom app. What third party tool would you pitch?

Thanks
Dec 9 '07 #13
On Dec 8, 1:50 pm, --CELKO-- <jcelko...@earthlink.netwrote:
you are jumping to conclusions without knowing anything about the problem. What if there are no legal issues at all? <<

1) I live in the Untied States where we have one lawyer for every 400
citizens. Look up the figures for Japan to get an idea how bad this
is. So, there is a always a legal issue :) And if there isn't one
now, there will next week.
"Caesar: Pardon him, Theodotus. He is a barbarian and thinks the
customs [legal environment, SOX] of his tribe and island [Austin, TX]
are the laws of nature [entire IT community]" - Caesar and Cleopatra;
George Bernard Shaw 1898
2) Putting audit data in the same table is like putting the LOG file
on the same hard drive as the DB. You guarantee that recovery will be
impossible.
Using audit trail for recovery is a very strange idea, there is
nothing in OP to suggest such a requirement.

Dec 10 '07 #14
>May I ask why legal requirements came about in this discussion? <<

Because we live in a country with too many lawyers :) If you don't
need to reconstruct the sequence of events, why timestamp the rows at
all?
>I don't see any in a custom app. <<
In a small, *private* application probably not much need for an audit
trail. But what you need is a question to ask the lawyer and the
accountant, if you want to be safe ...
>What third party tool would you pitch? <<
I have no favorites; you might want to post a request for opinions and
experiences from people here who are working in the same industry,
same size DB, etc.
Dec 10 '07 #15
On Dec 10, 5:12 pm, --CELKO-- <jcelko...@earthlink.netwrote:
May I ask why legal requirements came about in this discussion? <<

Because we live in a country with too many lawyers :) If you don't
need to reconstruct the sequence of events, why timestamp the rows at
all?
I don't see any in a custom app. <<

In a small, *private* application probably not much need for an audit
trail. But what you need is a question to ask the lawyer and the
accountant, if you want to be safe ...
What third party tool would you pitch? <<

I have no favorites; you might want to post a request for opinions and
experiences from people here who are working in the same industry,
same size DB, etc.
Thanks Joe, I wouldn't use timestamps to reconstruct the data. But may
be I am. I am going to have 3 copies of a database. Two will be off-
site. The one on-site will be the source. When records are updated I
would like to send the changes to the other locations. I haven't
decided the whole process here just determining what needs to be done.
Time is my biggest challenge. I am only doing this for the short run
as we are discussing a rewrite down the road. I can not guarantee a
solid internet connection just one I can manage. I don't see a lot of
changes, may be 50 a week.

Without getting into the history of this too much. I inherited a
database that other departments need to see and link to. Basically
this is an event database. Showing what events the customer has
attended. Depending if the customer has met a certain set of
achievements, like numerous involvements.

Getting into it too much... sorry...
Dec 11 '07 #16
Have you looked into replication?

J
Dec 11 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4475
by: Bert Sierra | last post by:
Hello -- I have what appears to be a simple PHP+MySQL query, but Dreamweaver consistently generates PHP code which won't parse. I've stared at the PHP code for several hours now, and I can't...
5
6079
by: Zee Zop | last post by:
How would I find the month and year from something with a datestamp of 1078801582? (The day does not matter)
3
2346
by: Bennett Haselton | last post by:
I'm working on an ASP.Net project where I want to test code on a local machine using a local database as a back-end, and then export it to the production machine where it uses the hosting...
2
1669
by: Miguelito Bain | last post by:
hi everyone- i inheritrd a database that wasn't designed very well. i'm trying to decide whether or not it is a good idea to add at least 8 more fields to a table that already has existing...
1
10938
by: deko | last post by:
DoCmd.CopyObject copies data, but I only need structure. I'm trying to clone several tables in my Access 2003 mdb. The goal is to link to a series of Excel spreadsheets and then run various...
8
2124
by: Daniele Baroncelli | last post by:
How can I get the original datestamp of uploaded files? The array $_FILES doesn't seem to contain the original date. Any solution? Daniele
4
2178
by: BrighterLater | last post by:
To help users identify what version of my program they are using, I want it to have an "Identity string" available. eg "Prog: Bodgit, Version: 0.0.0, Compiled: 20061215" The version number...
17
4727
by: Phil M | last post by:
Hi all. I have am HTML web page where I upload an updated file which is always called document.pdf. Users get there, click on the icon and download the document.pdf. Below the icon/link where...
1
1834
by: jpgoeth1 | last post by:
Hi, I'm brand-new to access 2007 and I need a little help building my first database. I'm importing from a text file and I'd like to add a datestamp as a separate column. This would hold the...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.