Sqlcmd formatting for better vizualization


To keep when possible the same display as SSMS GUI has for the output of a SELECT statement.

My way of achieving it

By creating a SP that will generate a dynamic tsql for the select statement or by rewriting the select statement.

While working in SQLCMD we face the problems whenever we issue a select statement or we run an internal stored procedure. The problem is that the output is not displayed or aligned in an way that is easy to read. Below are some outputs of connections using different switches in sqlcmd. As you can see in the below picture I tried the same query with no switch, with -W, -Y and -y.



No matter of the switch used the display is either truncating the columns or is hard to read. The last display is the one that I believe is easier to read. Of course that this simple example is only the tip of the iceberg and depending on the number of the columns that we want to select the generating of the dynamic tsql might be impossible but even so it is worth to add this type of output to our internal stored procedure for a better visualization in sqlcmd.

The SP for converting the select is not yet provided in the scripts below but I provided the code used to format the output.

The scripts used to create the output above and the difference between running xp_fixeddrives and the code for generating what I call a better display can be downloaded from here .



Leave a Reply

Your email address will not be published. Required fields are marked *