1

There is a particular email address that look like this in my csv file

"WZM","WZMDSDF","Kevin.O'Neil@gmail.com","True","mb"

I'm inserting it like this to my sql database using like this

 $queryII = "insert into Hold_Inv (Hold,GID,Source,Type,CreatedDate) VALUES ('$($item.Hold)','$($item.GID)','$($item.Source)','$($item.Type)','$CurDate')"

I'm getting this error so just wondering how can I fix this unclosed quotation mark

Invoke-Sqlcmd : Incorrect syntax near 'Neil@gmail'.
Unclosed quotation mark after the character string ')

I have other emails in the same csv file like this jonathan.sparkle@gmail.com but I have no issue. Any help or suggestion on how to fix above error will be really appreciated

aasenomad
  • 405
  • 2
  • 11
  • 1
    I think [`-Variable`](https://learn.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps#example-3--invoke-a-script-and-pass-in-variable-values-from-a-string) also auto escapes for you – Santiago Squarzon Apr 19 '22 at 04:21
  • 1
    In short: Programmatically double any `'` chars,. inside the expanded values, along the lines of `"... $($item.Email -replace "'", "''") ..."` – mklement0 Apr 19 '22 at 04:22
  • 1
    also, you might want to reconsider and use a safer alternative. see https://powershellstation.com/2016/01/05/invoke-sqlcmd-considered-harmful/ – Santiago Squarzon Apr 19 '22 at 04:40

0 Answers0