Search code examples
sql-serverpowershellinvoke-sqlcmd

Keep commented text in SQL query when using Invoke-Sqlcmd in PowerShell?


For example, I have the following SQL query that I want to run in PowerShell:

select 
item1, --this is a comment
item2
from myTable

If I tried to run this query in PowerShell using Invoke-Sqlcmd, I get a syntax error because it some how can't parse the comment as a comment:

Invoke-Sqlcmd -Query 'select item1, --this is a comment item2 from myTable'

Is there a way to keep my query comments?


Solution

  • Have you tried using PowerShell here-strings? e.g.:

    Invoke-SqlCmd -Query @"
    select 
    item1, --this is a comment
    item2
    from myTable
    "@