I have simple SSIS package which reads data from flat file and insert into SQL database. The file has 90K rows and sometimes because of bad data package fails but it insert the partial records before it fails. What I need is if insertion fails at any time between, no records should be inserted into DB, rollback everything. how can I put it in transaction?
Have you set the TransactionOption on the Package or the Container you want to be in a transaction? Click on the container (or the control surface to select the entire package) and press F4. Ensure that either the container in question is set to Required for TransactionOption, or that the package is set to Required and the containers in it are set to Supported. Available TransactionOption values are as follows:
NotSupported Specifies that no transaction will be started for this container, and consequently, the outcome of the current transaction, if one has been started by a parent container, will not affect the data that may be altered during execution of this container. This means that changes will not roll back, even if the parent container started a transaction.
Required Specifies that this container will cause a new transaction to be started unless the parent container already has a transaction, in which case, the parent’s transaction will be joined.
Supported Specifies that this container will not start a transaction. However, it will participate in a transaction if the parent container started one. This is the default.
Above TransactionOption values are at:
http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dts.runtime.dtstransactionoption.aspx
A better way, (Or at least how I would do it, not necessarily better) is to add in some data checking at the flat file reading section. Make sure the variables are correctly defined for VarChar, numeric, etc. That way it catches it on the read and not the insert.
You can also add in some script checking between the read and insert.
Another option would be to insert the data into a temp table and then run a SQL statement after, that uses transactions when doing the insert to the production table.
Thanks everyone. Finally I got the answer. There are two ways as far as I experimented on 1. Use transaction option, but I faced issue here. This need your MSDTC service to be running and because of some issues my SQl admin couldn't make it running for me so I had think of alternative. 2. Use SQL servers Transaction. I used two SQL tasks one for BEGIN TRANSACTION and another for COMMIT TRANSACTION. To make this working I required to set my SQL connection manager's property "RetainSameConnection" to True. This solved my problem.