sqlcmd - 如何在没有空格的情况下绕过列长度限制?
我正在尝试在运行 SQL Server 2005 的 Windows 计算机上使用 sqlcmd 将查询写入 csv 文件。我们通常使用的命令行选项是:
-l 60 -t 300 -r 1 -b -W -h -1
但是,列被截断为 256 字节。为了避免这种情况,我尝试使用此命令行选项代替 -W:
-y 8000
这会捕获整个字段,但此方法的问题是,由于所有额外的空间,文件从 1mb 多一点膨胀到大约 200mb空间(我意识到 8000 可能有点过大,但可能至少需要 4000,而且我目前只处理一小部分数据)。 -W 选项通常会消除所有这些额外的空间,但是当我尝试将它们一起使用时,它告诉我它们是互斥的。
有没有办法让 sqlcmd 绕过这个限制,或者有人知道另一个程序(例如 bcp 或 osql)是否会让这变得更容易?
编辑: 以下是我们用来获取被截断的字段的代码片段(类似的代码用于一堆字段):
SELECT ALIASES.AliasList as complianceAliases,
...
LEFT OUTER JOIN (Select M1.ID, M1.LIST_ID,stuff((SELECT '{|}' + isnull(Content2,'')+' '+isnull(Content3,'')+' '+isnull(Content4,'')+' '+isnull(Content5,'')+' '+isnull(Content6,'')+' '+isnull(Content7,'')
FROM fs_HOST3_TEST_web.ISI_APP_COMP_MULTI M2 with (nolock)
WHERE M1.LIST_ID = M2.LIST_ID and M1.ID = M2.ID and M1.TYPE = M2.TYPE
FOR XML PATH('')
),1,1,'') as AliasList
FROM fs_HOST3_TEST_web.ISI_APP_COMP_MULTI M1 with (nolock)
WHERE M1.LIST_ID = 2001 AND M1.TYPE = 'Aliases'
GROUP BY m1.list_id,m1.ID,m1.Type) as ALIASES
ON ALIASES.LIST_ID = PAIR.COMP_LIST_ID AND ALIASES.ID = PAIR.COMP_ID
I'm trying to use sqlcmd on a windows machine running SQL Server 2005 to write a query to a csv file. The command line options we typically use are:
-l 60 -t 300 -r 1 -b -W -h -1
However, the columns are getting truncated at 256 bytes. In an attempt to circumvent this, I tried using this command line option in place of -W:
-y 8000
This captures the entire fields, but the problem with this method is that the file balloons up from just over 1mb to about 200mb due to all the extra space (I realize 8000 is probably overkill, but it will probably have to be at least 4000 and I'm currently only working with a small subset of data). The -W option typically eliminates all this extra space, but when I try to use them together it tells me they're mutually exclusive.
Is there a way to get sqlcmd around this limit, or does anyone know if another program (such as bcp or osql) would make this easier?
Edit:
Here are the code snippets we're using to get the field that's being truncated (similar code is used for a bunch of fields):
SELECT ALIASES.AliasList as complianceAliases,
...
LEFT OUTER JOIN (Select M1.ID, M1.LIST_ID,stuff((SELECT '{|}' + isnull(Content2,'')+' '+isnull(Content3,'')+' '+isnull(Content4,'')+' '+isnull(Content5,'')+' '+isnull(Content6,'')+' '+isnull(Content7,'')
FROM fs_HOST3_TEST_web.ISI_APP_COMP_MULTI M2 with (nolock)
WHERE M1.LIST_ID = M2.LIST_ID and M1.ID = M2.ID and M1.TYPE = M2.TYPE
FOR XML PATH('')
),1,1,'') as AliasList
FROM fs_HOST3_TEST_web.ISI_APP_COMP_MULTI M1 with (nolock)
WHERE M1.LIST_ID = 2001 AND M1.TYPE = 'Aliases'
GROUP BY m1.list_id,m1.ID,m1.Type) as ALIASES
ON ALIASES.LIST_ID = PAIR.COMP_LIST_ID AND ALIASES.ID = PAIR.COMP_ID
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终通过使用“-y0”参数解决了这个问题。它仍然留下了一堆空白,但看起来它只到达了每个字段中最长的数据的末尾。
然后我通过一个程序运行输出,该程序删除了重复空格并解决了所有问题。
I ended up solving this by using the "-y0" argument. It still left a bunch of whitespace but it looks like it only went to the end of the longest piece of data in each field.
I then ran the output through a program that removed repeating spaces and that solved all of the problems.