Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Invoke-Sqlcmd and Transactions Expand / Collapse
Author
Message
Posted Thursday, January 20, 2011 2:19 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, August 18, 2014 8:48 AM
Points: 9, Visits: 43
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 -

Post #1051185
Posted Thursday, March 31, 2011 11:59 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, February 21, 2013 7:52 PM
Points: 28, 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,
Post #1087041
Posted Thursday, March 31, 2011 12:32 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, August 18, 2014 8:48 AM
Points: 9, Visits: 43
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
Post #1087062
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse