How to format SQLCMD output

三世轮回 提交于 2019-12-04 02:22:57

Answer

We can set the width of each column.

C:/> sqlcmd -S my_server

> :setvar SQLCMDMAXVARTYPEWIDTH 30
> :setvar SQLCMDMAXFIXEDTYPEWIDTH 30
> SELECT * from my_table
> go

We can also set it like this: sqlcmd -S my_server -y 30 -Y 30.

Details

SQLCMDMAXVARTYPEWIDTH (-y)

It limits the number of characters that are returned for the large variable length data type

SQLCMDMAXFIXEDTYPEWIDTH (-Y)

Limits the number of characters that are returned for the following data types

Note: setting -y has serious performance implications.

See https://docs.microsoft.com/en-us/sql/tools/sqlcmd-utility

Formatting issues usually pop up due to your console window. One solution is to output to the file and use notepad/your favorite editor:

sqlcmd -S myServer -d myDB -E -Q "select top 100 * From people" 
     -o "output.txt"

This is how I isolated a scalar.

sqlcmd -S xxx.xxx.xxx.xxx,xxxxx -d MyDb -U myUser -P MyPassword -h -1 -W -Q "set NOCOUNT ON; select a from b where b.id='c'" 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!