microsoft / go-sqlcmd

The new sqlcmd, CLI for SQL Server and Azure SQL (winget install sqlcmd / sqlcmd create mssql / sqlcmd open ads)
https://learn.microsoft.com/sql/tools/sqlcmd/go-sqlcmd-utility
MIT License
323 stars 56 forks source link

go-sqlcmd reports incorrect line numbers (because it strips blank lines) #395

Closed sommarskog closed 1 year ago

sommarskog commented 1 year ago

slask.txt If you attempt to load the attached file in the Northwind database, you will get this error:

Changed database context to 'Northwind'.
Msg 102, Level 15, State 1, Server SERVERNAME, Line 34
Incorrect syntax near '='.

If you account for the initial batch, you add 2 to 34 and you to line 36 in file to look at:

      JOIN   Products P ON P.ProductID = OD.ProductID

and you ask yourself, what's wrong with that?

Answer: absolutely nothing. But if you move to line 41 in the file, you find:

   IF @debug == 1

And that is not legal T-SQL.

If you remove the extraneous = and load the file again, it succeeds. If you run

sp_helptext ProductCountrSales_sp

You find why the line number was not reported correctly; All blank lines have been stripped out.

Lest you think this is a very small thing, it is not. This is a complete show-stopper for me to start using go-sqlcmd. I use SQLCMD to load stored procedures from my Editor, and having correct line numbers is absolutely essential.

shueybubbles commented 1 year ago

i think there was some reason for the current behavior of only preserving empty lines in comments and strings but I don't remember what it was. Let's see if always appending the empty lines doesn't break any tests.