Description
When -r (errors to stderr) is specified, server error messages are written to stderr
as bare message text only — the Msg %d, Level %d, State %d, Server %s, Line %d
header line is silently dropped. Without -r, the header prints correctly (to stdout).
ODBC sqlcmd with -r prints the same formatted output (header + message), just
redirected to stderr. So scripts/CI pipelines migrating from ODBC sqlcmd lose the error
number and line information exactly in the configuration meant for error capture.
Repro
-- repro.sql
SELECT id, badColumn FROM sys.objects WHERE object_id = 1;
GO
> sqlcmd -S myserver -E -b -i repro.sql # no -r
Msg 207, Level 16, State 1, Server MYSERVER, Line 1
Invalid column name 'id'.
> sqlcmd -S myserver -E -r -b -i repro.sql # with -r
Invalid column name 'id.' <-- stderr: message text only, no Msg/Level/State/Line
ODBC sqlcmd (16.0.1000.6) with the identical -r -b flags writes to stderr:
Msg 207, Level 16, State 1, Server MYSERVER, Line 1
Invalid column name 'id'.
Msg 207, Level 16, State 1, Server MYSERVER, Line 1
Invalid column name 'badColumn'.
Root cause (from source)
Formatter.AddError in pkg/sqlcmd/format.go formats the header correctly for
mssql.Error. But when -r is passed, cmd/sqlcmd/sqlcmd.go installs a
s.PrintError hook that receives only e.Message and writes it directly to
os.Stderr, returning true — which bypasses Format.AddError entirely:
s.PrintError = func(msg string, severity uint8) bool {
if severity >= stderrSeverity {
s.WriteError(os.Stderr, errors.New(msg+sqlcmd.SqlcmdEol))
return true
}
return false
}
Suggested fix: format the full header inside the hook (or route through the formatter
with a stderr destination) so -r only changes the destination, not the format —
matching ODBC sqlcmd behavior.
Additional observation
Combined with -b, only the first error of a batch is reported (the message loop
exits on the first qualifying error), while ODBC sqlcmd prints all errors of the
response before exiting. Together with the missing header this significantly degrades
error diagnostics for deployment scripts.
Environment
- sqlcmd v1.10.0 (sqlcmd-windows-amd64.zip from Releases)
- Windows 11, SQL Server 2022 (16.0.1190.2)
- Compared against ODBC sqlcmd 16.0.1000.6 with identical flags
- (Server messages in our environment are localized/Korean; behavior is language-independent)
Description
When
-r(errors to stderr) is specified, server error messages are written to stderras bare message text only — the
Msg %d, Level %d, State %d, Server %s, Line %dheader line is silently dropped. Without
-r, the header prints correctly (to stdout).ODBC sqlcmd with
-rprints the same formatted output (header + message), justredirected to stderr. So scripts/CI pipelines migrating from ODBC sqlcmd lose the error
number and line information exactly in the configuration meant for error capture.
Repro
ODBC sqlcmd (16.0.1000.6) with the identical
-r -bflags writes to stderr:Root cause (from source)
Formatter.AddErrorinpkg/sqlcmd/format.goformats the header correctly formssql.Error. But when-ris passed,cmd/sqlcmd/sqlcmd.goinstalls as.PrintErrorhook that receives onlye.Messageand writes it directly toos.Stderr, returningtrue— which bypassesFormat.AddErrorentirely:Suggested fix: format the full header inside the hook (or route through the formatter
with a stderr destination) so
-ronly changes the destination, not the format —matching ODBC sqlcmd behavior.
Additional observation
Combined with
-b, only the first error of a batch is reported (the message loopexits on the first qualifying error), while ODBC sqlcmd prints all errors of the
response before exiting. Together with the missing header this significantly degrades
error diagnostics for deployment scripts.
Environment