dataplat / Invoke-SqlCmd2

PowerShell module containing Invoke-SqlCmd2
MIT License
68 stars 35 forks source link

Invoke-SQLCmd2 Getting an error in the all commands.ps1 file #23

Closed OwainEsau closed 4 years ago

OwainEsau commented 6 years ago

Hi, I have a pretty simple function that deletes a record from a table:

        function executeDelete {
            param ($id, $table, $path, $database)

            $Query_Delete = "DELETE FROM $table WHERE CandDocID = $id"
            Invoke-SqlCmd2 -Query "Update JobCandidate SET CandDocID = NULL WHERE CandDocID = $id" -ServerInstance $ServerAddress -database $Database
            Invoke-SqlCmd2 -Query $Query_Delete -ServerInstance $ServerAddress -database $Database

            Write-Verbose "[REMOVED] $path "
        }

In most cases, this works fine but I am getting the following error somewhere near the dataset being deleted:

Incorrect syntax near '='.
At C:\Program Files\WindowsPowerShell\Modules\DBATools\0.9.487\allcommands.ps1:60363 char:21
+                     [void]$da.fill($ds)
+                     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SqlException
    + FullyQualifiedErrorId : SqlException

I can't seem to just set the error action to SilentlyContinue either

niphlod commented 6 years ago

you should use sqlparameters and placeholders to be sure your query is always escaped correctly.