Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
kirangentlebreeze1987
Starting Member
14 Posts |
Posted - 2010-04-06 : 04:03:10
|
how to get last modified record from a tablekkk |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-04-06 : 04:04:20
|
...by using a column with modified date an time? No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
kirangentlebreeze1987
Starting Member
14 Posts |
Posted - 2010-04-06 : 04:12:28
|
sorry,can you tell me what are the possible ways to get this task done?kkk |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-04-06 : 04:55:53
|
Create a column LastModifiedDate in your table and at the time of insert use Getdate() as value for that column.Vaibhav T |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-04-06 : 05:14:00
|
quote: Originally posted by vaibhavktiwari83 Create a column LastModifiedDate in your table and at the time of insert use Getdate() as value for that column.Vaibhav T
Better option is to create that column with default value of getdate()MadhivananFailing to plan is Planning to fail |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-06 : 05:27:33
|
quote: Originally posted by madhivanan
quote: Originally posted by vaibhavktiwari83 Create a column LastModifiedDate in your table and at the time of insert use Getdate() as value for that column.Vaibhav T
Better option is to create that column with default value of getdate()MadhivananFailing to plan is Planning to fail
And for update write an audit trail trigger to update this field with getdate() if not explicitly passed------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
kirangentlebreeze1987
Starting Member
14 Posts |
Posted - 2010-04-06 : 12:46:00
|
sorry,i am new to sqlserver can you explain me in a clear way?thanks kkk |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-06 : 12:54:12
|
quote: Originally posted by kirangentlebreeze1987 sorry,i am new to sqlserver can you explain me in a clear way?thanks kkk
--add a new column with default getdateALTER TABLE yourTable ADD LastModifiedDate datetime NOT NULL DEFAULT GETDATE()--then create audit trail trigger for updatesCREATE TRIGGER AuditTrail_YourTableON YourTableASBEGINIF NOT UPDATE(LastModifiedDate) UPDATE y SET y.LastModifiedDate=DEFAULT FROM YourTable y INNER JOIN INSERTED i ON i.PK=y.PKEND now you're done. once creation of above are over, its just a matter of doingSELECT TOP 1 * FROM Table ORDER BY LastModifiedDate DESC to get last modified recordand in case of batch updates use SELECT TOP 1 WITH TIES * FROM Table ORDER BY LastModifiedDate DESC ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|