MS SQL audit

I have a database problem in my work. An on-site audit is currently being conducted, but its clumsy, requires a lot of attention and is not justified. So I am replacing it.

I want to do this in as general a way as possible and designed tables and how things will link and update.

This is all fine and good now, but I want to write a general way to insert records into these audit tables. (Without having to issue a command for every column in every table that you change.)

Is there anyway inside the stored procedure iterating over all the columns in the table? And I would like to write it in such a way that it will work with multiple tables and also automatically collect and check the added columns, etc.

Any ideas?

EDIT: I think I should clarify. I will check the data that is in the tables. But I will use the same table to store the verified data for every table in the database.

And I can't use triggers because usually when an update happens it happens on multiple tables, but I would like all of these updates to be part of the same changeset.

This is not a problem because I can do all updates from one stored Proc. I would just prefer some way, like a loop, so that I can get all the updated fields, figure out which ones have changed, and insert the ones that have changed into the audit table.

And I would like to do it without a long list of if statements and insertion for each column. (By doing this in a general loop, it will handle the added columns automatically and not bother with the deleted columns)

0


source to share


5 answers


Under "added columns" I think you are looking for DDL validation. If you are using SQL 2005 then you want this link .

If you are not using SQL 2005, then you probably want to either use one of the many SQL schema comparison tools, for example the Red Gates SQL toolkit is probably in there.



If you don't have $ for tools, you can simply run periodic queries using informationschema.tables and information_schema.columns. By periodically committing them to permenant tables, you can determine when they have typed or lost rows (and hence the schema change has occurred)

If you are auditing the data instead, you will want the code to generate some triggers, again using information_schema.tables and information_schema.columns.

+1


source


There would be performance considerations, but you could add add and update triggers to all of your tables and include triggers in your audit tables.



0


source


0


source


This can be done if you are using a data access layer that can mask which tables and columns are updated and create insert statements for the audit table. In a stored procedure? What is the stored procedure? Do you have one that updates? Or are you creating one for the table?

0


source


If this is an option for you, just go to sql server 2008 and enable Change Data Capture.

0


source







All Articles