Sql server 从SQL Server 2005将数据导出为固定宽度文件

Sql server 从SQL Server 2005将数据导出为固定宽度文件,sql-server,Sql Server,我认为将视图中的数据从SQLServer2005导出到固定宽度的文本文件是一项非常简单的任务。但是巫师是一种痛苦。格式不正确。有人知道怎么处理吗?或者有更好的方法吗?如果向导中的格式不适合您的需要,您需要开发自己的SSIS包。你确定你真的需要一个固定宽度的文件吗?带分隔符的文件可能更容易获得正确的结果,因为它们更常见。使用bcp with queryyout选项 如果需要固定宽度的输出,请使用格式文件 我刚刚尝试在AdventureWorks中导出,固定宽度也给了我很多问题(与列分隔符相比)

我认为将视图中的数据从SQLServer2005导出到固定宽度的文本文件是一项非常简单的任务。但是巫师是一种痛苦。格式不正确。有人知道怎么处理吗?或者有更好的方法吗?

如果向导中的格式不适合您的需要,您需要开发自己的SSIS包。你确定你真的需要一个固定宽度的文件吗?带分隔符的文件可能更容易获得正确的结果,因为它们更常见。

使用bcp with queryyout选项

如果需要固定宽度的输出,请使用格式文件

我刚刚尝试在AdventureWorks中导出,固定宽度也给了我很多问题(与列分隔符相比) 我不得不忽略GUID列,在第一行中不包括列名,等等。。最后导出销售。客户表


如果没有BCP,您可能必须查看SSI或SQLCMD,一个选项是使用OpenDataSource将行写入文本文件。这要求文本文件已经存在,但相对容易


这篇文章(为了说明问题,我写了这篇文章,重点是阅读,但也涉及写作)解释了使用OpenDataSource从文本文件读写的基础知识:

我发现SQL2005/SSIS并不是简单的任务。事情总是比应该的时间长5倍。我知道在这件事上我并不孤单

BCP应该起作用

不知道sqlcmd-似乎它不做固定宽度,除非您在sql语句中做了填充-这也会很痛苦

usage: Sqlcmd            [-U login id]          [-P password]
  [-S server]            [-H hostname]          [-E trusted connection]
  [-d use database name] [-l login timeout]     [-t query timeout]
  [-h headers]           [-s colseparator]      [-w screen width]
  [-a packetsize]        [-e echo input]        [-I Enable Quoted Identifiers]
  [-c cmdend]            [-L[c] list servers[clean output]]
  [-q "cmdline query"]   [-Q "cmdline query" and exit]
  [-m errorlevel]        [-V severitylevel]     [-W remove trailing spaces]
  [-u unicode output]    [-r[0|1] msgs to stderr]
  [-i inputfile]         [-o outputfile]        [-z new password]
  [-f <codepage> | i:<codepage>[,o:<codepage>]] [-Z new password and exit]
  [-k[1|2] remove[replace] control characters]
  [-y variable length type display width]
  [-Y fixed length type display width]
  [-p[1] print statistics[colon format]]
  [-R use client regional setting]
  [-b On error batch abort]
  [-v var = "value"...]  [-A dedicated admin connection]
  [-X[1] disable commands, startup script, enviroment variables [and exit]]
  [-x disable variable substitution]
  [-? show syntax summary]
用法:Sqlcmd[-U登录id][-P密码]
[-S服务器][-H主机名][-E可信连接]
[-d使用数据库名称][-l登录超时][-t查询超时]
[-h标题][s列分隔符][w屏幕宽度]
[-a packetsize][e echo input][I启用带引号的标识符]
[-c cmdend][L[c]列出服务器[清除输出]]
[-q“cmdline查询”][-q“cmdline查询”并退出]
[-m errorlevel][V severitylevel][-W删除尾随空格]
[-u unicode输出][-r[0 | 1]msgs到标准码]
[-i输入文件][-o输出文件][-z新密码]
[-f|i:[,o:][-Z新密码并退出]
[-k[1 | 2]删除[替换]控制字符]
[-y可变长度类型显示宽度]
[-Y固定长度类型显示宽度]
[-p[1]打印统计信息[冒号格式]]
[-R使用客户端区域设置]
[-b错误批处理中止]
[-v var=“value”…][-专用管理员连接]
[-X[1]禁用命令、启动脚本、环境变量[和退出]]
[-x禁用变量替换]
[-?显示语法摘要]
usage: Sqlcmd            [-U login id]          [-P password]
  [-S server]            [-H hostname]          [-E trusted connection]
  [-d use database name] [-l login timeout]     [-t query timeout]
  [-h headers]           [-s colseparator]      [-w screen width]
  [-a packetsize]        [-e echo input]        [-I Enable Quoted Identifiers]
  [-c cmdend]            [-L[c] list servers[clean output]]
  [-q "cmdline query"]   [-Q "cmdline query" and exit]
  [-m errorlevel]        [-V severitylevel]     [-W remove trailing spaces]
  [-u unicode output]    [-r[0|1] msgs to stderr]
  [-i inputfile]         [-o outputfile]        [-z new password]
  [-f <codepage> | i:<codepage>[,o:<codepage>]] [-Z new password and exit]
  [-k[1|2] remove[replace] control characters]
  [-y variable length type display width]
  [-Y fixed length type display width]
  [-p[1] print statistics[colon format]]
  [-R use client regional setting]
  [-b On error batch abort]
  [-v var = "value"...]  [-A dedicated admin connection]
  [-X[1] disable commands, startup script, enviroment variables [and exit]]
  [-x disable variable substitution]
  [-? show syntax summary]