August 3, 2009 at 9:56 am
Hi,
I was wondering if there is a better way to do this....
We have a 'Patient' table with a bunch of columns along with audit columns like 'date_created', 'date_modified', 'user_created', 'user_modified'
CREATE TABLE dbo.Patient(
Name VARCHAR(200)
...
date_created DATETIME DEFAULT GETDATE()
date_modified DATETIME
user_created VARCHAR(200) DEFAULT SYSTEM_USER
user_modified VARCHAR(200)
)
So when a record is INSERTed, the DEFAULTs for 'date_created' and 'user_created' are populated.
On Update, a trigger is fired and with in the trigger (along with other things), 'date_modified' and 'user_modified' are updated.
Is this possible to do this in a different way, to avoid an UPDATE within an UPDATE transaction?
(second update is from with in the UPDATE TRIGGER)
thanks,
_UB
August 3, 2009 at 10:02 am
I would typically use a stored procedure and set the columns in the stored procedure. You could also have the application pass the values using ad hoc sql. I'd use a trigger for a backup with code that checks the value in the inserted table before issuing the update.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
August 3, 2009 at 10:28 am
Thanks for the response.
All our queries hit the DB directly, no Stored Procedures. (I tried to convince otherwise, but ....)
UPDATE queries only supply values to the main columns not the audit columns. So a trigger or some kind of a DEFAULT has to provide values.
August 3, 2009 at 10:56 am
Put those audit columns in a separate table. Each row would have a date_time, a userID and a type (create, updated etc.) and a column (ID probably) to link back to the main table.
This will be more efficient because all the audit rows are inserts (faster) and you will avoid fragmentation/page splits due to the varchar() update .
That's my $02.
The probability of survival is inversely proportional to the angle of arrival.
August 7, 2009 at 10:46 am
Looks like UPDATE TRIGGER is the only way to perform UPDATES on the 'Date_modified' column.
There is an entry in the Microsoft Connect site already on this. For people who are interested, please vote on this:
https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=203570&wa=wsignin1.0
thanks for all the responses,
_UB
August 7, 2009 at 10:49 am
There are better options for auditing. Take a look at these (shameless self-promotion, I know):
http://www.sqlservercentral.com/articles/Auditing/63247/
http://www.sqlservercentral.com/articles/Auditing/63248/
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
August 7, 2009 at 11:31 am
GSquared (8/7/2009)
There are better options for auditing. Take a look at these (shameless self-promotion, I know):
Nothing shameless about it. Good articles, regardless of who wrote them are good articles.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
August 7, 2009 at 2:12 pm
Thankyou GSquared. I always greatly appreciate your response to my posts.
I'll be sure to read your articles.
_UB
August 10, 2009 at 12:02 pm
You're welcome.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply