sqlcmd:协调 -W 和 -Y
我有一个自动批处理过程,它使用 sqlcmd 从数据库中提取数据并将其转储到文本文件中。该数据库中的许多字段都是 varchar(max)
类型,Sqlcmd 将这些字段限制为 256 个字符,除非我在 sqlcmd 调用中的标志中添加类似 -y 0
的内容。
这为我提供了大于 256 个字符的字段的全文,但它也添加了大量的空格;这些字段经过填充,以使每个字段根据其数据类型尽可能大,这实际上给了我带有大量填充和浪费空间的巨大文件。
我可以通过将 -W
添加到我的 sqlcmd 标志来修复此问题,但这会给我一个错误,指出 -W
和 -y
不兼容。
以前有人遇到过这个问题吗?思考如何解决它?
I have an automated batch process that uses sqlcmd to pull data from a database and dump it into a text file. Many fields in that database are of type varchar(max)
, Sqlcmd limits these fields to 256 characters unless I add something like -y 0
to the flags in the sqlcmd call.
This gives me the full text for fields larger than 256 characters, but it also adds a great deal of whitespace; the fields are padded to make each field as big as it could possibly be according to its data type, essentially giving me huge files with lots of padding and wasted space.
I could fix this by adding -W
to my sqlcmd flags, but this gives me an error saying that -W
and -y
are incompatible.
Has anyone had this problem before? Thoughts on how to solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 此线程 建议指定列使用
-s
分隔符可以修剪数据,就好像未指定数据显示固定宽度一样。如果这不起作用,您是否在
SELECT
查询中尝试过RTRIM(LTRIM(ColumnName))
?From this thread there is the suggestion that specifying the column separator using
-s
can trim the data as if it is not specified the data comes out fixed width.If that does not work have you tried
RTRIM(LTRIM(ColumnName))
in yourSELECT
query?我在使用 SQLCMD 创建 CSV 文件时遇到了这个问题,并通过欺骗 SQLCMD 解决了这个问题。我没有返回多个字段并让 SQLCMD 将逗号作为分隔符,而是自己连接所有字段并将逗号放入其中。我知道这是一个丑陋的解决方法,但这解决了我的问题。希望它可以帮助别人。
I had this issue while creating a CSV file with SQLCMD and got it solved by tricking SQLCMD. Instead of returning several fields and let SQLCMD put the comma as separator, I just concatenate all the fields putting the commas myself in it. I know it's an ugly workaround, but that solved my problem. Hope it can help someone else.