将.xls LongText中的数据导入MS SQL

时间:2013-10-03 09:10:36

标签: sql sql-server excel ssis

我在将.xls导入SQL时遇到问题。 Excel文件包含一些列,其中一些单元格包含超过2000个文本字符,因此在导入时我为每列更改了nvarchar到4000。导入工作但只有一列将单元格截断为255个字符。我检查了,列是nvarchar(4000,null)但SELECT MAX(LEN(列))= 255,这很奇怪。我尝试将nvarchar更改为varchar(4000),但不接受此操作。消息是:

[Source Information]
Table: `Sheet1$`
Column: q1
Column Type: VarChar
SSIS Type: Unicode string [DT_WSTR]

[Destination Information]
Table: [dbo].[Sheet1$]
Column: q1
Column Type: varchar
SSIS Type: string [DT_STR] 

奇怪的是,一列具有源类型:LongText和目标类型:nvarchar,默认情况下显示最大字符,在导入时,一切都适用于该特定列(超过255个字符)。对于所有其他列nvarcharnvarchar,但即使更改为4000或3000个字符,也会在255处截止。

我尝试将Excel文件重新保存为.csv格式,但SQL显示我执行(错误):

Task 1: Data conversion failed. The data conversion for column "Column 13" returned status value 4 and status text "Text was truncated or one or more characters had no match in the target code page

Task 1: The "output column "Column 13" (62)" failed because truncation occurred, and the truncation row disposition on "output column "Column 13" (62)" specifies failure on truncation. A truncation error occurred on the specified object of the specified component.

Task 1: An error occurred while processing file

The component returned a failure code when the pipeline engine called PrimeOutput(). The meaning of the failure code is defined by the component, but the error is fatal and the pipeline stopped executing.  There may be error messages posted before this with more information about the failure 

我不能要求客户重新导入数据并将其保存为.csv格式。

是否可以以SQL可接受的方式格式化Excel版本,例如源类型:LongText和目标类型:nvarchar? (我不知道为什么varchar对我不起作用)

我已经将Excel的简单任务(如已更改的单元格)用于文本或通过一列复制粘贴到文本格式。

2 个答案:

答案 0 :(得分:1)

当导入尝试确定Excel文件中的列类型时,它只查找8个第一行。如果没有超过255个字符的文本,则该列将被标记为VarChar,否则将标记为LongText(如果更合适,则为数字类型)。

有两种解决方法:

  1. 在要成为LongText的列的前8行中放入一个长文本。

  2. 更改注册表设置,以确定在确定列类型之前应探测的行数(TypeGuessRows)。

  3.   

    要更改TypeGuessRows的值,请使用以下步骤:

         

    在“开始”菜单上,单击“运行”   在“运行”对话框中,键入Regedt32,然后单击“确定”。

         

    在注册表编辑器中打开以下项:HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Jet \ 4.0 \ Engines \ Excel
      注意对于64位系统,相应的键如下所示:HKLM \ SOFTWARE \ wow6432node \ microsoft \ jet \ 4.0 \ engines \ excel

         

    双击TypeGuessRows   在“DWORD编辑器”对话框中,单击“基数”下的“十进制”   为值数据键入0到16之间的值(包括0和16)   单击“确定”,然后退出注册表编辑器。

    Source: Microsoft

答案 1 :(得分:0)

请参阅Fix

您需要在连接字符串或注册表设置中指定“扫描行”设置。