Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity log of database MS SQL Server

I have a database with more than a hundred tables. I am continuously adding columns to existing tables (if required) and I also added few new tables.

Now I want to check what changes I have made in last 3 months. Is there any activity log in MS SQL Server 2012 for that specific database to track changes.

like image 605
trighati Avatar asked Sep 19 '16 08:09

trighati


People also ask

How do I monitor change logs in SQL Server?

You’ll want to deploy this solution on each individual database that you want to monitor. To get started, log into SQL Management Studio and connect to the SQL Server containing the desired database. The first step is to create the database table that will store the change log data. Execute the following script against the desired database.

What is a transaction log in SQL?

The transaction log is a critical component of the database. If there is a system failure, you will need that log to bring your database back to a consistent state. For information about the transaction log architecture and internals, see the SQL Server Transaction Log Architecture and Management Guide.

How does the log reader agent work with SQL Server?

For more information, see Restore and Recovery Overview (SQL Server). The Log Reader Agent monitors the transaction log of each database configured for transactional replication and copies the transactions marked for replication from the transaction log into the distribution database. For more information, see How Transactional Replication Works.

How do I create a change log in SQL management studio?

To get started, log into SQL Management Studio and connect to the SQL Server containing the desired database. The first step is to create the database table that will store the change log data. Execute the following script against the desired database.


2 Answers

Perhaps this can get you partway. sys.objects has create and modify dates but unfortunately sys.columns does not. However the latest columns added will have higher column_ids. I don't know that you would be able to pick out deleted columns that easily. Note that changes other than column changes can be reflected by the modify date.

select  s.name [schema], o.name [table], o.modify_date [table_modify_date], c.column_id, c.name
from    sys.schemas s
join    sys.objects o on o.schema_id = s.schema_id
left    join sys.columns c on c.object_id = o.object_id
where   o.type = 'U'    --user tables only
and     o.modify_date >= dateadd(M,-3, getdate())
order   by s.name, o.name, column_id;

To make this audit easier in the future you can create a DDL trigger that will log all schema changes to a table or in source control if you use something like a SSDT data project to manage your changes.

like image 53
SMM Avatar answered Oct 07 '22 21:10

SMM


Right now,your options are limited ,going forward you can try below and also check to see if they help you, now..

1.If you have enabled Audit,you can track the changes

To check ,if you have enabled Audit,You can use below query..

select * from sys.dm_server_audit_status

If you have not enabled Audit,you can enable it ,following this :An Introduction to SQL Server Audit..I would not recommend enabling Audit ,unless you need to capture other than what is mentioned in your question

2.Default trace also captures tables created ,but this uses, roll over files mechanism to override last files when the space is full,so you may be out of luck(since you are asking for three months range),but try this:What event information can I get by default from SQL Server? to find out to know all the things,which are offered by default trace

I would go with this option and try backing up those files ,depending on when they rollup(since you need only to check table changes)

3.Finally One final option is to query Tlog

select * from fn_dblog(null,null) where [transaction name]='CREATE TABLE'

the above Tlog option works only if you have Tlog backups for over three months and also you need to restore them

like image 28
TheGameiswar Avatar answered Oct 07 '22 22:10

TheGameiswar