BULK INSERT in Transaction

  • Errors from BULK INSERT within a transaction bring everything to a halt. For example, if the file path given in the BULK INSERT is incorrect, it throws an error and stops processing, rather than doing the "IF @@error > 0 ROLLBACK TRAN" code that is right after the BULK INSERT statement.

    Does BULK INSERT not play nice with TRANSACTIONS?

    Doug Brashear


    Doug Brashear

  • The BULK INSERT statement can be executed within a user-defined transaction. Rolling back a user-defined transaction that uses a BULK INSERT statement and BATCHSIZE clause to load data into a table or view using multiple batches rolls back all batches sent to SQL Server.

    Maybe you should check that the path is correct before executing the bulk insert.

    DECLARE @Exists AS INT

    EXEC @Exists = master.dbo.xp_cmdshell 'DIR path', no_output

    IF @Exists <> 0 --the path doesn't exists

    EXEC master.dbo.xp_cmdshell 'MKDIR path', no_output --create the path.

Viewing 2 posts - 1 through 1 (of 1 total)

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