Invoke-Sqlcmd and Transactions

  • Our legacy process for upgrading our product's databases is built around the use of SQLCMD. We have batch files that execute SQLCMD with -i and -o options and very fragile error handling. I would like to replace this with Powershell and at the same time make it more transactional, such that several script files can be executed sequentially, all in the context of a single transaction. Most of our scripts are generated by SQL Compare, but some are handcrafted. All have GO statements to separate batches and many have PRINT statements also.

    The Invoke-Sqlcmd cmdlet from SQLPS does not seem to support any transaction semantics. Is there a way I can affect the connection being used so that I can execute Invoke-Sqlcmd repeatedly within a single transaction? I really want to be able to continue to capture all the output from each script just the way SQLCMD -o or SSMS "Results To text" do.

    I tried experimenting directly with SMO objects and ExecuteWithResults and the ability to get messages, result sets, row counts, etc., all into a coherent text stream is difficult at best. (For instance, I have seen examples of declaring event handlers to capture messages, but am at a loss as to how to correctly associate individual messages with any datasets that might be returned.) Is there a way I can accomplish my goals using SMO objects in PowerShell?

    - Andy -

  • You can try this solution :

    Create a Wrapper.sql with a Transaction defined in it, and within that transaction use :r to call other scripts. Here is an example:

    --Wrapper.sql

    :On Error Exit

    SET XACT_ABORT ON

    GO

    Begin Transaction

    Use <DBNAme>

    :r <Script1.sql>

    :r <Script2.sql>

    Use <DBNAme2>

    :r <Script1.sql>

    :r <Script2.sql>

    If Xact_State()=1

    Begin

    Print 'Committing Tranaction...'

    Commit tran

    End

    Else If Xact_State()=-1

    Begin

    Print 'Rolling Back Transaction...'

    RollBack Tran

    End

    Then call this Wrapper.sql from SQLCMD.

    I have used this approach, and it has worked for me.

    Thanks,

  • Thanks, Amit. We have considered that option, but decided that we needed to move away from SQLCMD to get more control over the overall process. We opted to implement our own script-engine using VB .Net and SMO. We gave up the ability to return output to the program in exchange for transaction control without having to repackage our scripts as your solution would require. We also decided it would be easier to implement a checkpoint/restart strategy in VB.

    Microsoft has already built the right software in SSMS and SQLCMD, they just don't seem to have given us the ability to access it from more modern tools. If anyone knows how to access the underlying object(s) that implement script execution in SQLCMD or SSMS, I'd love to hear about it.

    Regards,

    Andy

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

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