views:

124

answers:

2

My employer has developed a utility that will run a stored procedure line by line against a DataTable, passing the fields of each row as parameters into the Stored Procedure. This is particularly useful for automated imports.

However, I now need to extend this to provide a transactional-ized version so that we can see the potential results of running the utility to provide a summary of what changes will make to the database. This could be as much as '3 rows inserted into Customer table', or '5 rows amended in Orders table'. The user could then decide whether to go ahead with the real import.

I know triggers could be set up on tables, however I'm not sure this would be possible in this case as all the tables referenced by the stored procedure would not be known.

Is there any other way of viewing changes made during a transaction, or does anyone have any other suggestions on how I could achieve this?

Many thanks.

A: 

Edited based on feedback and re-reading the question:

I agree with Remus in that no serious importer of data would want to visually inspect the data as it gets imported into the system.

As an ETL Writer, I would expect to do this in my staging area, and run queries that validate my data before it gets imported into the actual production place.

You could also get into issues with resources, deadlocks and blocks by implementing functionality that "holds" transactions until visually OK'ed by someone.

Raj More
CDC won't do what he's looking for. It will make a nice record of what happened but not provide rollback capability. If you reread his problem and still think CDC is a possible solution, please explain a bit more.
Rob Garrison
@RobGarrison: Thank you for pointing that out. I had missed the part about the rollback portion - only answered the INSPECT CHANGES part of it.
Raj More
A: 

You snapshot the current LSN, run yout 'line by line' procedure in a transaction, then use fn_dblog to read back the log after the LSN you snapshotted. The changes made are the records in the log that a are stamped with the current transaction id. The wrapper transaction can be rolled back. Of course this will only work with an import of 3 rows in Customer and 5 rows Orders, no serious employer would consider doing something like this on a real sized import job. Imagine importing 1 mil Orders just to count them, then rolling back...

This will not work with any arbitrary procedure though as often time procedure make their own transaction management and they don't work as expected when invoked under a wrapping transaction.

Remus Rusanu