如何格式化 SQLCMD 输出 [英] How to format SQLCMD output
问题描述
我使用下面的命令行使用 SQLCMD 运行 SQL 查询
I am using below command line to run a SQL query using SQLCMD
sqlcmd -S Server -Q "select top 100 * From people" -d people -t 10
该表有 20 列,当我查看输出命令行窗口时,文本会换行并使其难以阅读.
The table has 20 columns and when i look at output command line window wraps the text and makes it difficult to read.
我希望结果的显示方式与在 SQL Server Management Studio 中的显示方式相同(格式正确).我不是在寻找任何网格,但我需要将所有列显示在第 1 行中,并将结果正确显示在下方.
I want my results to be displayed the same way it displays in SQL Server Management Studio (properly formatted). I am not looking for any grids, but i need all my columns to be displayed in row 1 and the results properly beneath.
提前致谢.
推荐答案
答案
我们可以设置每列的宽度.
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
我们也可以这样设置:sqlcmd -S my_server -y 30 -Y 30
.
We can also set it like this: sqlcmd -S my_server -y 30 -Y 30
.
SQLCMDMAXVARTYPEWIDTH (-y
)
SQLCMDMAXVARTYPEWIDTH (-y
)
它限制为大可变长度数据类型返回的字符数
It limits the number of characters that are returned for the large variable length data type
SQLCMDMAXFIXEDTYPEWIDTH (-Y
)
SQLCMDMAXFIXEDTYPEWIDTH (-Y
)
限制为以下数据类型返回的字符数
Limits the number of characters that are returned for the following data types
注意:设置 -y
对性能有严重影响.
Note: setting -y
has serious performance implications.
参见 https://docs.microsoft.com/en-us/sql/tools/sqlcmd-utility
这篇关于如何格式化 SQLCMD 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!