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 57 forks source link

Make -q and -i work together #389

Open shueybubbles opened 1 year ago

shueybubbles commented 1 year ago

Discussed in https://github.com/microsoft/go-sqlcmd/discussions/206

Originally posted by **0x7FFFFFFFFFFFFFFF** February 2, 2023 Right now, both `sqlcmd` and `go-sqlcmd` don't allow you to specify `-q` and `-i` options together. Below is the output of both commands. ``` c:\temp>sqlcmd -S . -E -q "SET PARSEONLY ON" -i test.sql Sqlcmd: The i and the -Q/-q options are mutually exclusive. c:\temp>sqlcmdgo -S . -E -q "SET PARSEONLY ON" -i test.sql sqlcmdgo: error: --input-file and --initial-query can't be used together c:\temp> ``` If this is possible, it will be very useful. In my case, I have a huge file `test.sql` that is not practical to open in SSMS. What I want is to check its syntax, just like what we normally do by clicking the Parse toolbar button in SSMS. What SSMS actually does is run the command `SET PARSEONLY ON` before your actually SQL code. If `go-sqlcmd` can allow us to execute a custom command and then an SQL file, it will basically allow us to parse huge SQL files without having to prepend the literal text `SET PARSEONLY ON` at the beginng of the file. This feature might be useful in some other cases, too. Hope you can consider adding this. Thanks.

Note there are 2 ways to accomplish the same result today, but they both require saving your boilerplate script to a file.

  1. Use multiple -i values and include your custom command file first
  2. Set the environment variable SQLCMDINI to the path of your custom command file and sqlcmd will run that script upon launch.

Enabling -q to work alongside -i would provide a more flexible solution.