![]() ![]() I’ve posted the script above, along with the setup scripts on my GitHub. That’s really all we need for this blog post, but in order to set this up for a demo I did use a few other dbatools commands. Invoke-DbaQuery -SqlInstance $sqlInst -Database $destinationDatabase -File $psitem.FullName $SqlInstance = 'mssql1' $destinationDatabase = 'AdventureWorks2021' $folderPath = '.\output\AdventureWorks2017' # Create a connection to the server that we will reuse - can use SqlCredential for alternative creds $sqlInst = Connect-DbaInstance -SqlInstance $SqlInstance This will loop through each file running the SQL scripts. With this we can use the -File parameter to pass in the SQL file and that’s really all we need. Second, I’m using the foreach method which takes each script file returned from the Get-ChildItem call, and executes Invoke-DbaQuery. This means that we’re efficiently reusing the connection rather than opening a new one for each file we want to execute. The code for this example is pretty short, but there are a couple of things to point out.įirst, I used Connect-DbaInstance to create a server object to use to run the queries. Can you imagine having to open each file in Management Studio to be able to execute it? Thank goodness we have PowerShell and dbatools on our side. Last week at work I was given a folder of 1,500 scripts – each containing a create table statement. Another week and another useful dbatools snippet for you today.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |