Update table only if all records commit, all or none.

  • I have a job that runs automatically every night that updates Table A from Table B.

    Last night we had a network hiccup, and only 10 records made it into Table A from Table B.

    Question: How do I tell the job to only update Table A, if it has all the records from Table B, otherwise, dont update ?

    Thank you !

  • What is the structure of your SSIS package?

    • This reply was modified 4 years, 12 months ago by  Phil Parkin.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Hmm, not sure what you mean by structure, but it's very simple: Its a data flow task in SSIS, where all columns in Table A are populated from Table B rows.

    My goal is that Table A is ONLY updated if it has all rows from Table B

     

    Last night, only 10 out of 1000 records were put into Table A, due to the network having a hiccup and losing connection between Table B (which is found in another database).

  • Are you using an OLEDB destination in your data flow?

    What are the values of 'Rows Per Batch' and 'Maximum Insert Commit Size'?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • My general approach to this type of problem is:

    1. Prepare a stage table - which is a copy of the final table with minimal (or no) indexes.  This step truncates the table to prepare for the load process.
    2. Extract the data from the source - insert into the staging table
    3. Use a stored procedure on the destination to load the data from the stage table to the final destination.

    If there is a network error - then step 2 will fail and the load to the final table will not be executed.  In the code for step 3 - this is performed in an explicit transaction and if an error occurs during the load process it is rolled back leaving the destination table in the same state it was prior to attempting the load process.

    I also include in the load procedure steps that disable non-clustered indexes on the final table, an index rebuild after the new data has been loaded and a final step to truncate the stage table.

    The load process can be built using MERGE to handle inserts/updates/deletes in a single transaction - or you can build separate insert/update/delete steps in the explicit transaction, whichever work best for you.

    Jeffrey Williams
    Problems are opportunities brilliantly disguised as insurmountable obstacles.

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Thanks Jeffrey. One question: why even have step 2 ? Cant you just have the stored procedure get data from original source ? If a network error occurs, a rollback occurs and leaves destination table as is ?

  • Can you wrap the entire job in a TRY CATCH blocks?

    In the try block begin and commit the transaction

    In the catch block rollback and re-throw the error.

  • Jonathan AC Roberts wrote:

    Can you wrap the entire job in a TRY CATCH blocks?

    In the try block begin and commit the transaction

    In the catch block rollback and re-throw the error.

    I don't think that TRY/CATCH is available in SSIS.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil Parkin wrote:

    Jonathan AC Roberts wrote:

    Can you wrap the entire job in a TRY CATCH blocks?

    In the try block begin and commit the transaction

    In the catch block rollback and re-throw the error.

    I don't think that TRY/CATCH is available in SSIS.

    It is if you are executing the script as an SSIS SQL Task

  • koln wrote:

    Thanks Jeffrey. One question: why even have step 2 ? Cant you just have the stored procedure get data from original source ? If a network error occurs, a rollback occurs and leaves destination table as is ?

    Because the stored procedure would have to pull the data across a linked server - which introduces a whole other layer of issues for the process.

    By using a staging table - your process can perform extra validation and verification of the data before loading to the final table (if needed).

    Jonathan AC Roberts wrote:

    Can you wrap the entire job in a TRY CATCH blocks?

    In the try block begin and commit the transaction

    In the catch block rollback and re-throw the error.

    This would only be available if you use a data flow to move the data from the source to a staging table - then use a stored procedure to load the data to the final table.  I would recommend using TRY/CATCH in that procedure to handle any errors and rollback...but if you have a single statement (e.g. MERGE) then an explicit transaction wouldn't be necessary as any errors would roll back the full insert/update/delete.

    Jeffrey Williams
    Problems are opportunities brilliantly disguised as insurmountable obstacles.

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Jeffrey Williams wrote:

    Jonathan AC Roberts wrote:

    Can you wrap the entire job in a TRY CATCH blocks?

    In the try block begin and commit the transaction

    In the catch block rollback and re-throw the error.

    This would only be available if you use a data flow to move the data from the source to a staging table - then use a stored procedure to load the data to the final table.  I would recommend using TRY/CATCH in that procedure to handle any errors and rollback...but if you have a single statement (e.g. MERGE) then an explicit transaction wouldn't be necessary as any errors would roll back the full insert/update/delete.

    I hadn't read the information about the source table being on another database. A good way to go is to first copy the data onto a Staging table on the target database that has the same structure as the source table. This should be fast as it is only copying 1000 rows across so less likely to fail with a network error, the batch size of the insert should also be set to a value larger than the number of rows on the table so it's done in one step. If it does fail the restart instructions would be to truncate the target staging table then rerun from the failed step.

  • Jonathan AC Roberts wrote:

    I hadn't read the information about the source table being on another database. A good way to go is to first copy the data onto a Staging table on the target database that has the same structure as the source table. This should be fast as it is only copying 1000 rows across so less likely to fail with a network error, the batch size of the insert should also be set to a value larger than the number of rows on the table so it's done in one step. If it does fail the restart instructions would be to truncate the target staging table then rerun from the failed step.

    Be careful with the batch and commit sizes - especially the commit size.  If this is too large and there are millions of rows you can easily cause the transaction log to fill a drive and the process will fail.  This can also cause the load to take longer - as committing millions of rows in a single transaction can take a lot longer.

     

    Jeffrey Williams
    Problems are opportunities brilliantly disguised as insurmountable obstacles.

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Jeffrey Williams wrote:

    Jonathan AC Roberts wrote:

    I hadn't read the information about the source table being on another database. A good way to go is to first copy the data onto a Staging table on the target database that has the same structure as the source table. This should be fast as it is only copying 1000 rows across so less likely to fail with a network error, the batch size of the insert should also be set to a value larger than the number of rows on the table so it's done in one step. If it does fail the restart instructions would be to truncate the target staging table then rerun from the failed step.

    Be careful with the batch and commit sizes - especially the commit size.  If this is too large and there are millions of rows you can easily cause the transaction log to fill a drive and the process will fail.  This can also cause the load to take longer - as committing millions of rows in a single transaction can take a lot longer. 

    Yes, there has to be enough space on the transaction log to store all the necessary data, so the OP would have to make sure there is enough disk space to do this. I'm not sure it will take any longer to commit the transaction, a commit is normally very fast.

  • Thank you everybody. I will play around with a staging table and some other ideas !

Viewing 14 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic. Login to reply