[英]SQL Server BULK INSERT error
This is my source data in CSV format: 这是我的CSV格式的源数据:
4,23,2AY5623,7235623
4,23,2GP1207,1451207
4,23,2GQ6689,4186689
Table: 表:
CREATE TABLE [dbo].[Table1](
[idCodeLevel] [int] NOT NULL,
[idFirm] [int] NOT NULL,
[valCodeFrom] [varchar](15) NOT NULL,
[valCodeTo] [varchar](15) NOT NULL
) ON [PRIMARY]
This the code I am using to bulk import: 这是我用来批量导入的代码:
USE Test
GO
TRUNCATE TABLE Table1
GO
BULK INSERT Table1
FROM 'C:\Temp\test.csv'
WITH (
FIELDTERMINATOR = ',',
MAXERRORS=0,
ROWTERMINATOR = '\n'
)
GO
Error I am getting is: 我得到的错误是:
Msg 4864, Level 16, State 1, Line 2
消息4864,第16级,州1,第2行
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (idCodeLevel).第1行第1列(idCodeLevel)的批量加载数据转换错误(类型不匹配或指定代码页的字符无效)。
Can you please someone tell me why is it failing? 能否请您告诉我为什么失败了?
I googled and found out that I might have to use the format .fmt. 我搜索了一下,发现可能必须使用.fmt格式。 But how can I convert a csv file to fmt.
但是如何将csv文件转换为fmt。 I have seen code to create fmt file from sql table.
我已经看到了从sql表创建fmt文件的代码。
Thanks a lot for your help! 非常感谢你的帮助!
Does the csv have a row at the top of field names? csv在字段名称顶部是否有一行? If so you'll need to add "FIRSTROW = 2" to your bulk statement.
如果是这样,则需要在您的批量声明中添加“ FIRSTROW = 2”。 If not, try creating a new table that is all VARCHAR fields, then check the data: you probably have something strange in your data that you aren't expecting, like a non-printing character.
如果不是,请尝试创建一个所有VARCHAR字段的新表,然后检查数据:您的数据中可能有一些您不期望的奇怪东西,例如非打印字符。 Import as text and then try something like "SELECT ISNUMERIC([FIELD1]) FROM NEWTABLE".
导入为文本,然后尝试类似“从NEWTABLE中选择ISNUMERIC([FIELD1])”。
use the sql import wizard to import data from external file. 使用sql导入向导从外部文件导入数据。
Right click on database--->task--->import----> specify the flat file as source and select the destination server. 右键单击数据库--->任务--->导入---->将平面文件指定为源,然后选择目标服务器。
for more information please visit Import CSV data to SQL 有关更多信息,请访问将CSV数据导入SQL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.