Is there any way to log the changes made in Schema of a Table whenever I do the schema changes?
I was reading an article here about DDL Triggers. But it does not tell about the specific changes made in schema of a table.
this would be very difficult as quite often in SSMS the table is actually dropped and rebuilt in the background (depending on the complexity of the schema change & whether or not you enabled the "Prevent saving changes that require the table to be re-created " option in SSMS) - logging all the different types of changes would be a nightmare. (constraints being dropped, only to be re-created - bulk re-inserts, renames etc when all you might have done is re-arranged columns in joined table)
If you're serious about tracking schema changes i'd strongly recommend you script the schema (using the generate scripts option in MSSMS) & check the resulting file into SVN / SourceSafe / TFS & use the many comparison tools available for those systems.
OR, you can use 3rd party products that do all this for you, such as Red Gates SQL Source Control:
http://www.red-gate.com/products/sql-development/sql-source-control/
Edit: You may find this useful - it makes use of the Service Broker (SQL 2005+) and SSB queues:
http://www.mssqltips.com/sqlservertip/2121/event-notifications-in-sql-server-for-tracking-changes/
For this issue i would probably use Event Notifications. Although DDL trigger's in my opinion do tell about specific changes made to table, just trigger definition:
Create Trigger tr_DDLNotikums
On DataBase
For **DDL_DATABASE_LEVEL_EVENTS**
Use DDL Trigger In Below Format
CREATE TRIGGER tr_DDL_Database ON DATABASE
FOR DDL_SCHEMA_EVENTS
AS Begin
Insert Into LogTable (XmlColumn)
SELECT EVENTDATA()
End
Related
It's my first time creating an audit log for a PoS WPF application and was wondering on how exactly do I implement an auditing system because it seems like each option available has its ups and downs. So far from reading numerous articles/threads, I've narrowed down a few common practices on audit logs:
1. Triggers - Unfortunately, due to the nature of my app, I can't make use of triggers as it has no way of knowing which user has done the action. So what I did instead was to create a Stored Procedure which will handle the customer insert along with the customer log insert and its details. The Customer_Id will be provided by the application when using the Stored Procedure.
2. Have an old and new value - My initial plan was to only include the latter since I can reference its old value with the new value from the row before it but storing the the old and new value seemed more sensible, complexity-wise.
3. Use a separate database for the log / 4. Foreign Keys - This is probably my main concern, if I decide to use a separate database for the audit table, then I couldn't setup foreign keys for the customer and employee involved.
I created a mock-up erd with a master-detail table result to be shown on the wpf app to display the log to an admin and would really like your thoughts on possible problems that may arise (There's also an employee table but I forgot to put it):
https://ibb.co/dheaNK
Here's a few info that might be of help:
The database will reside together with the wpf app, which is a single computer.
The amount of customers will be less than 1000.
The amount of regular employees will be 3.
The amount of admins will be 2.
You can enable CDC Change Data Capture on SQL Server database for a specific table
This will enable you to collect all data changes on the database table logged in special tables.
You can also refer to official documents too
Here is a list of DML commands and how data changes are logged in the CDC table created for the source database table
What is good about CDC is it comes default with SQL Server and you don't have to do anything for logging. The only requirement is SQL Server Agent should be running so that the changes can be reflected on log table.
I am developing a product which uses an Oracle database (11g). The product will most likely have to be customised for each client - this may involve changing PLSQL functions slightly or modifying a view for instance. My question is: is there any way of automatically keeping a record of structural changes like this, and perhaps saving it to a table for future reference? Or will it have to be a more manual process e.g just remembering to insert a row into a log table describing any changes that have been made?
Thanks!
If you are talking about DDL commands, those can be tracked with DDL triggers. Create DDL trigger which inserts to some audit table.
More info:
http://psoug.org/reference/ddl_trigger.html
http://www.dba-oracle.com/t_ddl_triggers.htm
Is there a way to see the history or any other information of insertions into a specific table of an SQL Server database?
Unless you are recording this information somewhere using a trigger, you would need some way of looking at the information in the transaction log. There are commercial tools like Lumigent for this.
You could use a trigger
Create a trigger on the table watching for inserts, updates, and deletes). The trigger would insert into another table (a history table).
This adds extra overhead, though, so I wouldn't do this on a really heavily updated table.
Look at this page for an example of how this is done.
This page has some code that generates the audit trail code for you.
Here is another SOF question about doing this using triggers.
If you are using SQL Server 2008, you can use the new Change Data Capture feature. This saves you from having to write triggers on all your tables.
For 2005 use triggers, for 2008 you can use the change data capture.
Aside from using a trigger, you could do something like add a column named "InsertedDate" and record the current date there. This would require you do your insertions through a stored procedure though.
I need to create in my DB a log, that every action in the program should be written there.
I will also want to store additional data to it for example have the table and row the action was applied to.
In other words I want the log to be dynamic and should be able to refer to the other tables in the database.
The problem is, I don't know how to relate all the tables to this log.
Any ideas?
You have two choices here:
1) modify your program to add logging for every db access
2) add triggers to each table in your db to perform logging operations.
I don't recommend one logging table for all tables. You will have locking issues if you do that (every insert, update and delete in every table woudl have to hit this one, bad idea). Create a table for each table that you want to audit. There are lots of possible designs for the table, but they usually include some variant of old vlaue, new value, date changed, and user who did the change.
Then create triggers on each table to log the changes.
I know SQL Server 2008 also has a systemic way to set up auditing, this would be easier to set up than manual auditing and might be enough to lure your company into using 2008.
I need to create a SQL Server database that will recieve updates by some replication mechanism from another database. I need to write insert, update and delete triggers that will execute when this replilcation occurs.
I have experience with triggers but not with replication.
Should I use Transactional or Merge replication, or does it matter?
Will a trigger designed to run when a simple SQL insert statement is executed also run when replication occurs?
The CREATE TRIGGER syntax on MSDN:
CREATE TRIGGER
...
[ NOT FOR REPLICATION ]
This indicates that executing on replication is the default behaviour for triggers, and can be disabled by specifying NOT FOR REPLICATION.
Well it depends.
If the updates that you intend to apply are to isolated tables i.e. all the data for a given table comes from the publisher only, then you can use transactional replication.
If on the other hand you are looking to combine table content i.e. an orders table, with orders being placed at both sites, then you would want to look into using merge replication.
With regard to triggers, there is a "not for replication" configuration that you can apply to control their behaviour. See the following article for reference.
http://msdn.microsoft.com/en-us/library/ms152529.aspx
Cheers, John
It's hard to answer your question with the information you've provided. I added a few comments to your question asking for clarifying information.
Here is an article on MSDN that should help: http://msdn.microsoft.com/en-us/library/ms152529.aspx
By default, triggers will fire during replication unless "NOT FOR REPLICATION" is specified. They work the same way as they do for simple insert statements.
Transactional and Merge replication are very different, but triggers behave similarly for both options.
There are a few alternative options open to you instead of triggers.
You could modify the replication procedures on the subscriber (destination) database.
If using 2008 you can use Change Tracking on the subscriber for tables you want to "do something with" and then create a batch process to deal with "set based" data instead of invididual rows. E.g. an SSIS package that runs every X.