3

我有一个 txt 文件,其中包含 1600 行和 82 列逗号分隔的数据,我试图将其导入表中。我在最后一个字段的每一行都收到以下错误:

消息 4864,级别 16,状态 1,第 1 行第 1 行第 81 列 (DB252D20C8) 的批量加载数据转换错误(指定代码页的类型不匹配或无效字符)。

导入语句是

BULK
INSERT  [ENERGY].[dbo].[READINGS1]
       from 'c:\readings2.txt'
with
(
DATAFILETYPE='widechar',
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO

表结构如下,脚本的顶部和底部:

USE [ENERGY]
GO
/****** Object:  Table [dbo].[READINGS1]    Script Date: 05/13/2013 20:00:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[READINGS1](
[DateAndTime] [datetime] NOT NULL,
[DB240D4C7] [float] NULL,
[DB240D8C7] [float] NULL,
[DB240D12C7] [float] NULL,
[DB240D16C7] [float] NULL, 



[DB252D12C8] [float] NULL,
[DB252D16C8] [float] NULL,
[DB252D20C8] [float] NULL,
 CONSTRAINT [READINGS1DataTimeStamp] PRIMARY KEY CLUSTERED 
(
[DateAndTime] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

文本文件如下:

2013-02-19 00:00:00.000,6,945,1886,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,22,2040,6299,0,0,6,567,1248,0,0,251,8859,8655,0,0,10,316,1786,0,0,7,180,1206,0,0,1,16,56,0,0,368,18953,36949,0,0,NULL,NULL
2013-02-19 01:00:00.000,6,147,1886,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,22,1516,6299,0,0,3,115,1248,0,0,250,5077,8655,0,0,9,219,1786,0,0,5,147,1206,0,0,1,15,56,0,0,362,8907,36949,0,0,NULL,NULL
4

2 回答 2

1

好的,所以您需要做的是更改您的语句,以便在文件结束后使用KEEPNULLS. 这会通知 SQL 服务器您希望保留空值。目前它正在尝试将NULL字符串转换为您的FLOAT COLUMN. 改变你的陈述看起来像这样。

BULK
INSERT  [ENERGY].[dbo].[READINGS1]
       from 'c:\readings2.txt'
with
(
DATAFILETYPE='widechar',
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
KEEPNULLS
)
GO

BOL上有一篇关于此的文章。.

否则,您始终可以构建一个集成服务包来处理这个问题。这是从平面文件源导入信息的一种简单快捷的方法。

于 2013-05-13T19:51:09.310 回答
0

事实证明,表格的输入文本文件中有太多字段。

于 2013-05-15T09:40:11.567 回答