Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8557741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:38:36+00:00 2026-06-11T15:38:36+00:00

Found plenty of examples online how to do the following in powershell: Get-ChildItem $databaseFolder

  • 0

Found plenty of examples online how to do the following in powershell:

Get-ChildItem $databaseFolder -Filter *.sql -Recurse | ForEach-Object { sqlcmd -S  $databaseServer -d $databaseName -E -i $_.FullName } 

However, I’d like to group this under one single transaction so if one of the sql files fails, then everything is rolled back.

One alternative that I am also trying is to put together all the file contents in to a full script variable:

foreach( $file in Get-ChildItem -path $databaseFolder -Filter *.sql | sort-object )
{
    Get-Content $file.fullName | Foreach-Object { $fullSqlScript = $fullSqlScript + $_ + "`n" } ;
}

And then execute that in the end like so:

 invoke-sqlcmd -ServerInstance $databaseServer -Database $databaseName -Query $fullSqlScript | format-table | out-file -filePath $outFile

The $fullSqlScript would also have the following inserted at the top:

:On Error Exit
 SET XACT_ABORT ON
 GO
 Begin Transaction

and end:

IF XACT_STATE() = 1
BEGIN
    PRINT 'Committing Transaction...'
    COMMIT TRANSACTION
END
ELSE IF XACT_STATE() = -1
BEGIN
    PRINT 'Scripts Failed... Rolling back'
    ROLLBACK TRAN
END

However, as soon as I make an intentional sql script fail then the entire database is locked up like the transaction cannot be rolled back. I’m assuming this has something to do with invoke-sqlcmd being used in a powershell script environment rather than sqlcmd from windows command prompt?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:38:37+00:00Added an answer on June 11, 2026 at 3:38 pm

    I believe it has been solved.
    After reading the following article about having the On Error Exit only having ability to be executed using sqlcmd, I definitely had doubts about the powershell invoke-sqlcmd.

    Here’s some documentation I found here that I think may have something to do with it:
    http://technet.microsoft.com/en-us/library/cc281720.aspx

    In particular the following:
    *

    Not all of the sqlcmd commands are implemented in Invoke-Sqlcmd.
    Commands that are not implemented include the following: :!!,
    :connect, :error, :out, :ed, :list, :listvar, :reset, :perftrace, and
    :serverlist.

    *

    In the end, I am still using the strategy of having to read the contents of each file and then having the:

    :On Error Exit
    

    at the top of the script that is finally written to an output file containing all the other sql from other files.

    Then, I do the following to invoke the actual sqlcmd:

    $command = "sqlcmd.exe -S " + $databaseServer + " -d " + $databaseName + " -i " + $outFile
    Invoke-Expression $command
    

    This seems to work just fine and everything does get rolled back 🙂
    If anyone can think of a better solution please let me know!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've found plenty of examples how to do encryption in C#, and a couple
I have found plenty of information out there about this error: 'ERROR: Could not
I've found plenty of tutorials / questions on Stackoverflow that deal with copying char
I've found plenty of other topics similar to this one, but I they are
I've looked everywhere, found plenty of articles, explaining work a rounds for IE 7,8
I'm sure this is asked plenty and i've found some questions on here similar
Is this possible? I've spent quite a while googling and found plenty of people
I've done a bit of searching on this. Found plenty of scripts that counted
I can find plenty of examples that show how to configure a URL to
Wondering how to open many new windows with Javascript. I have found plenty of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.