Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 391
Mike Avatar asked Dec 07 '25 05:12

Mike


1 Answers

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

Invoke-SqlCmd -Query @"
select 
item1, --this is a comment
item2
from myTable
"@
like image 156
AlwaysLearning Avatar answered Dec 08 '25 19:12

AlwaysLearning