SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Invoke-Sqlcmd and Transactions


Invoke-Sqlcmd and Transactions

Author
Message
Andrew Schickedanz
Andrew Schickedanz
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 53
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 -
Amit Chaudhary
Amit Chaudhary
SSC-Enthusiastic
SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)

Group: General Forum Members
Points: 172 Visits: 159
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,
Andrew Schickedanz
Andrew Schickedanz
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 53
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search