【发布时间】:2015-11-01 22:02:22
【问题描述】:
我正在尝试使用非 XML bcp 格式文件将数据导入 Win7 64 位上的 LocalDB。最简单的用例。
OS Name: Microsoft Windows 7 Home Premium
OS Version: 6.1.7601 Service Pack 1 Build 7601
LocalDB version: Microsoft SQL Server 2014 (12.0.2000.8)
BCP version: 12.0.2000.8
基本上是几天前从 Microsoft SQL Server 2014 网站下载的所有内容的最新版本。
我可以通过 bcp 连接到 LocalDB 实例以创建格式文件,但是在尝试使用它重新导入最简单的数据时,生成的格式文件不起作用。无论我尝试什么,bcp 都会加载零行,静默失败,并且不会将错误信息打印到指定的错误文件中。
/* create the table */
use try_db;
create table try(num integer);
/* create the format file based on the table. */
bcp try_db.dbo.TRY format nul -n -T -f TRY.fmt -S (localdb)\default_db
/* above command creates a file TRY.fmt with the following contents */
12.0
1
1 SQLINT 1 4 "" 1 num ""
/* then I create a file data.txt, with just the number 99 in it, followed by a Windows line terminator (\r\n) */
/* then try importing the file into the table */
bcp try_db.dbo.TRY in data.txt -f TRY.fmt -T -S (localdb)\default_db -e errors.txt
结果:
Starting copy...
0 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total : 1
没有任何内容写入errors.txt。我只是无法让 bcp 使用格式文件导入任何内容!
我没有在 SQL Server 本身上尝试过(仅限 LocalDB),但对于像这样简单的东西应该没关系。
我尝试按如下方式编辑 TRY.fmt 文件行:
1 SQLINT 1 4 "\r\n" 1 num ""
但这也无济于事。
我能够使用 -c 而不是 -f 成功导入它:
bcp try_db.dbo.TRY in data.txt -c -T -S (localdb)\default_db -e errors.txt
关于 (a) 为什么 bcp 不会使用格式文件导入,以及 (b) 为什么它不会向指定的错误文件打印任何错误?一定有一些非常简单的东西我在这里弄错了。
请不要建议改用 BULK INSERT 或 SSIS(等)。 bcp 应该按照记录的方式工作!
【问题讨论】:
-
如果你在里面放两行呢?也许它认为单行是标题行
-
我尝试了多行数据(比如第一行是 99,第二行是 98)。同样的问题。添加了标题行 (num),即使您添加了标题行,您也应该使用 -F2 开关跳过它。还没有运气 - 加载了零行。
标签: sql-server bcp localdb