June 5, 2013 at 8:45 am
I would like to ask for help with creating a stored procedure to generate below information:
1- A time stampt for record creation in a table, a time stamp shows when was updated, and which user last the record. I know that by querying sys.dm_db_index_usage_stats you can get the information, but I like to have a store procedure created in master database to run it whenever any changes are made in any database.
Thanks,
Lava
June 5, 2013 at 8:52 am
lsalih (6/5/2013)
I know that by querying sys.dm_db_index_usage_stats you can get the information
You will not get all this information by querying this table.
A better option would be to create a trigger on your table and then do some coding inside the trigger to audit the changes.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
June 5, 2013 at 8:53 am
lsalih (6/5/2013)
I would like to ask for help with creating a stored procedure to generate below information:1- A time stampt for record creation in a table, a time stamp shows when was updated, and which user last the record. I know that by querying sys.dm_db_index_usage_stats you can get the information, but I like to have a store procedure created in master database to run it whenever any changes are made in any database.
Thanks,
Lava
sys.dm_db_index_usage_stats only gives you when the index was last used, it does not tell you, on a per record basis, when things where changed.
you'd want to either enable CDC for the table in question, or add columns to the table like CREATEDDate (with a default of getdate()) and ModifiedDate, and have a trigger set the ModifiedDate value from the INSERTED virtual tableinside the trigger.
you mention any changes, so it seems maybe cdc on every table might be an option.
then your stored procedure could query the cdc tables for the latest changes.
Lowell
June 5, 2013 at 9:05 am
As I mentioned we need to have something for all databases in case we get a request about changes were made to a table. I appreciate it if you provide me with a code to do so.
June 5, 2013 at 10:22 am
Lowell -
After doing more research, I think I will go with your recommendation to enable CDC and write a store procedure to get the information users need...
Many thanks to you.
Lava
June 6, 2013 at 9:14 am
Lowell -
I did setup cdc and it is working the way I needed, I used http://www.mssqltips.com/sqlservertip/1819/using-change-tracking-in-sql-server-2008/ as a base to get started. Once again, thank you.
Lava
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply