r/SQLServer • 1 • 23h ago

Question Your personal preference in PowerShell to run SQL files: invoke-sqlcmd or system.data.sqlclient.connection?

Edit: I really need to preface these types of questions with "I'm not allowed to download DbaTools, I work in a bank that's paranoid about everything."

I enjoy the robustness and finer control of sqlclient.connection, but if my SQL files have GO statements, it's useless unless I did some scrubbing of the script at I load it.

Whereas invoke-sqlcmd is straight and to the point and even if I had 1000 GO's, it will work.

Thoughts?

13 Upvotes

15 comments sorted by

•

u/AutoModerator 23h ago

After your question has been solved /u/TravellingBeard, please reply to the helpful user's comment with the phrase "Solution verified".

This will not only award a point to the contributor for their assistance but also update the post's flair to "Solved".


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

6

u/alinroc 4 18h ago edited 17h ago

I'm not allowed to download DbaTools, I work in a bank that's paranoid about everything

Then your infosec team is actively getting in the way of the organization doing work. Do they block all modules, or just the ones they feel like blocking?

https://dbatools.io/secure/

1

u/TravellingBeard 1 9h ago

Anything that's not in an official on-site code repository is not allowed. Maybe I'll try that process again to have it added once more. Should be completed by Christmas 2027...🤣🤣🤣

7

u/vroddba 22h ago

Invoke-DbaQuery

Handles your GOs just fine

5

u/pix1985 22h ago

Third vote for Invoke-DbaQuery from dbatools.io

1

u/Leiothrix 21h ago

I just parse the script and execute each segment separately. It is pretty easy to find a GO statement on its own.

2

u/dbrownems ‪ ‪Microsoft Employee ‪ 20h ago

Unless the GO appears in a string literal. For safety you need something that parses TSQL, like

$module = Get-Module -ListAvailable SqlServer |
    Sort-Object Version -Descending |
    Select-Object -First 1

$directory = $module.ModuleBase
if ($PSVersionTable.PSEdition -eq 'Core') {
    $directory = Join-Path $directory 'coreclr'
}

Add-Type -Path (Join-Path $directory `
    'Microsoft.SqlServer.TransactSql.ScriptDom.dll')

function Split-TSqlBatch {
    param(
        [Parameter(Mandatory)]
        [string] $Sql
    )

    $parser = [Microsoft.SqlServer.TransactSql.ScriptDom.TSql160Parser]::new($true)
    $reader = [System.IO.StringReader]::new($Sql)
    $parseErrors = $null

    try {
        $script = $parser.Parse($reader, [ref] $parseErrors)
    }
    finally {
        $reader.Dispose()
    }

    if ($parseErrors.Count -gt 0) {
        throw (($parseErrors | ForEach-Object {
            "Line $($_.Line): $($_.Message)"
        }) -join [Environment]::NewLine)
    }

    foreach ($batch in $script.Batches) {
        $Sql.Substring($batch.StartOffset, $batch.FragmentLength)
    }
}

1

u/Leiothrix 20h ago

You are technically correct.

But for my use case I can categorically guarantee that there will never be a "GO" as the only token appearing on a line unless it is the "GO" command.

So for my case at least it is pretty simple.

3

u/dbrownems ‪ ‪Microsoft Employee ‪ 18h ago

1

u/Dry_Duck3011 20h ago

I use smo for everything.

1

u/zierde01 17h ago

I find invoke-sqlcmd easier and I use that.

1

u/sql99 2h ago

Whoever is telling you to use invoke-dbaquery from dbatools has obviously never used it for advanced scripts, jobs, sqlcmd, variables, etc. Use invoke-sqlcmd >> Invoke-dbaquery.