Sql-server – How to format the output of SQL queries on SQLCMD for SQL Server 2017 on RHEL Linux 7.3

command linelinuxsql serversql-server-2017sqlcmd

I have installed SQL Server 2017 on RHEL Linux 7.3, and using SQLCMD utility for querying the SQL Server.

When I try to query for example: 'SELECT * FROM SYS.DATABASES'. I get output like the following:

enter image description here

How to format the output for SQL commands on SQLCMD for SQL Server on RHEL Linux?

For example, in Oracle we use 'SET LINES 200 PAGES 200' to format the output / width of the output columns.

Note: I have already tried the following workaround (as per other answer on dba.stackexchange):

C:/> sqlcmd -S my_server
:setvar SQLCMDMAXVARTYPEWIDTH 30
:setvar SQLCMDMAXFIXEDTYPEWIDTH 30
SELECT * from my_table
GO

Best Answer

The sqlcmd utility is handy for executing T-SQL scripts via command-line, such as in automation scenarios, but is limited in its output formatting capabilities and interactive query editor.

Consider using mssql-cli instead for interactive terminal queries. This open source tool provides pagination, handles wide results well, includes IntelliSense ,and allows one to use emacs or vi mode. mssql-cli runs on Linux, Windows, and Mac.

enter image description here

I'll add that Azure Data Studio is a cross-platform alternative to SSMS when a full desktop experience is desired. It does not include all features available in SSMS but does include other features and provides greater extensibility.