我正在努力为我的
BULK INSERT
文件编写 .dat
脚本。根据我的尝试,我收到了很多错误,但我很确定它与行分隔符有关。我可以成功使用 SQL 导入向导来导入我的 .dat
文件。在“指定分隔源文件的字符”下,我看到以下内容是我接受的默认值:
Row delimiter: {CR}{LF}
Column delimiter: Comma {,}
多亏了向导,500k 行和 158 列才完美地出现。
在编写脚本时,我尝试了各种
\r\n
变体和 0x0a
等,并搜索帮助并查看类似的帖子。我可以成功地编写一行真实数据的脚本,所以我认为列解析不是问题。当我在虚拟文件中尝试 2 行时,它失败了。我产生的错误最少发生在以下情况
bulk insert abc
from 'C:\TestDat\try2.dat'
with (FIRSTROW = 1, fieldterminator = ',', rowterminator = '\r\n')
go
消息 4863,第 16 级,状态 1,第 3 行
第 1 行第 158 列(第 157 列)的批量加载数据转换错误(截断)。
(使用 0x0a 会在 2 行文件上产生更多错误)。
.dat 内容:
xxx,20080501,xxx,xxx:175000,55008654,0178636,202307011001,001859915,OK,W,xx,1.0,00000100,48,202306290100,R0,2,202306290200,R0,0,202306290300,R0,0,202306290400,R0,0,202306290500,R0,1,202306290600,R0,1,202306290700,R0,0,202306290800,R0,0,202306290900,R0,0,202306291000,R0,0,202306291100,R0,0,202306291200,R0,1,202306291300,R0,0,202306291400,R0,0,202306291500,R0,2,202306291600,R0,1,202306291700,R0,0,202306291800,R0,0,202306291900,R0,0,202306292000,R0,0,202306292100,R0,0,202306292200,R0,0,202306292300,R0,2,202306300000,R0,3,202306300100,R0,0,202306300200,R0,0,202306300300,R0,2,202306300400,R0,0,202306300500,R0,1,202306300600,R0,1,202306300700,R0,3,202306300800,R0,0,202306300900,R0,0,202306301000,R0,0,202306301100,R0,0,202306301200,R0,3,202306301300,R0,1,202306301400,R0,0,202306301500,R0,1,202306301600,R0,1,202306301700,R0,0,202306301800,R0,0,202306301900,R0,1,202306302000,R0,0,202306302100,R0,1,202306302200,R0,0,202306302300,R0,1,202307010000,R0,0
xxx,20080501,xxx,xxx:175000,55008654,0178636,202307011001,001859915,OK,W,xx,1.0,00000100,4,202307010100,R0,1,202307010200,R0,0,202307010300,R0,1,202307010400,R0,0
好的,我创建了
try2.dat
文件,
我创建了一个表
abc
(我遗漏了column3..column157,但它们具有相同的定义......);
CREATE TABLE abc (
Column1 VARCHAR(200),
Column2 VARCHAR(200),
...
Column158 VARCHAR(200),
Column159 VARCHAR(200)
)
然后启动SQLCMD
D:\TEMP>sqlcmd
1> use test
2> go
Changed database context to 'TEST'.
1> delete from abc;
2>
3> go
(1 rows affected)
1>
2> bulk insert abc from 'd:\temp\try2.dat' with (FIRSTROW = 1, fieldterminator = ',', rowterminator = '\r\n')
3> go
(1 rows affected)
当您收到错误时,我确实看到两种可能性: