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

Scripting variables are not expanded in :out and :r commands #522

Closed scara closed 4 weeks ago

scara commented 2 months ago

Hello Everyone, in mssql-tools a SQL script can use variables even when composing a file path in :out and :r commands.

The go version is not able to expand variables in the filename of these commands:

:on error exit
:setvar WorkingDir ./tmp
:out $(WorkingDir)/file.sql
GO
PRINT '-- Test file.'
GO
:out stdout
GO
:r $(WorkingDir)/file.sql
GO

and leads the SQL script above to the following errors:

Sqlcmd: Error:  Error occurred while opening or operating on file $(WorkingDir)/file.sql (Reason: open $(WorkingDir)/file.sql: no such file or directory).
Sqlcmd: Error:  Error occurred while opening or operating on file $(WorkingDir)/file.sql (Reason: open $(WorkingDir)/file.sql: no such file or directory).

Tested on both v1.5.0 and v1.6.0.

TIA, Matteo

shueybubbles commented 4 weeks ago

`:r' works for me, but ':out' and ':error' are definitely broken

scara commented 4 weeks ago

Hi @shueybubbles,

`:r' works for me, but ':out' and ':error' are definitely broken

Didn't test them separately, just on top of my use case.

Thanks a ton for the PR! Matteo