将CSV导入表格时出现问题

时间:2015-06-29 20:03:11

标签: sql-server sql-server-2008

我尝试将CS​​V文件导入表格以进行一些查询。最终,我将使用SSIS自动执行此操作,以便每天运行。不幸的是,我收到了错误。

我已经运行了任务导入数据。然后选择我的平面文件,在高级下我根据数据库中的类型设置所有类型(这个平面文件将替换一个表,因此类型应该相同)。我确实检查了第一行是否包含列名。

我在DB中的第一列的类型,所以在数据源中,我尝试了DT_Numeric和DT_UI8。但是,当我执行导入时,出现以下错误:

  

错误0xc02020a1:数据流任务:数据转换失败。数据   列"" BLTO-NUM""返回状态值2和状态   text"由于潜在的损失,该值无法转换   数据。&#34 ;. (SQL Server导入和导出向导)

     

错误0xc0209029:数据流任务:SSIS错误代码   DTS_E_INDUCEDTRANSFORMFAILUREONERROR。 "输出列"" BLTO-NUM""   (10)"失败,因为发生错误代码0xC0209084,错误行   处理"输出栏"" BLTO-NUM"" (10)"指定失败   错误。指定的指定对象发生错误   零件。在此之前可能会发布错误消息   有关失败的信息。 (SQL Server导入和导出向导)

关于可能导致这种情况的任何想法?该字段保持数字。

谢谢, 埃里克

2 个答案:

答案 0 :(得分:2)

我首先将数据放入一个阶段,然后使用简单的强制转换语句更改数据类型。因此,例如,您创建一个包含大量varchar(100)字段的表。这将是您的舞台目的地。不是最有效但它只会填充一分钟。然后在目标目标表中使用INSERT INTO执行SELECT语句。这是ETL的 转换 部分。 CAST将您的所有字段添加到第二个目标表中描述的数据类型和数据长度

这是一个例子虽然不相关,但希望能帮助描述我的散文。

INSERT INTO [Staging].[EDITestCaseData]
           ([FileID]
           ,[ImportRecordID]
           ,[TestCaseID]
           ,[TestID]
           ,[TransmissionID]
           ,[GroupID]
           ,[SetID]
           ,[LoopID]
           ,[FileType]
           ,[FilePopulation]
           ,[DataDescription]
           ,[InterchangeReceiverID]
           ,[InterchangeDate]
           ,[InterchangeTime]
           ,[InterchangeControlNumber]
           ,[ApplicationSendersCode]
           ,[ApplicationReceiversCode]
           ,[FunctionalGroupDate]
           ,[FunctionalGroupTime]
           ,[FunctionalGroupControlNumber]
           ,[TransactionSetControlNumber]
           ,[SegmentDate]
           ,[SegmentTime]
           ,[MaintenanceTypeCode]
           ,[MaintenanceReasonCode]
           ,[EmploymentStatuscode]
           ,[RecipientID]
           ,[Site]
           ,[CaseIDNumber]
           ,[MedicaidBegin]
           ,[MedicaidEnd]
           ,[RecipientLastName]
           ,[RecipientFirstName]
           ,[TelephoneNumber]
           ,[CorrectedRecipientLastName]
           ,[CorrectedRecipientFirstName]
           ,[CaseAddress1]
           ,[CaseAddress2]
           ,[CaseCity]
           ,[CaseState]
           ,[CaseZip]
           ,[RecipientBirthdate]
           ,[RecipientGenderCode]
           ,[RaceEthnicityCode]
           ,[SpanishLanguage]
           ,[UseOfLanguageIndicator]
           ,[OldRecipientLastName]
           ,[OldRecipientFirstName]
           ,[OldRecipientBirthdate]
           ,[OldRecipientGenderCode]
           ,[CaseLastName]
           ,[CaseFirstName]
           ,[ResponsiblePersonTelephoneNumber]
           ,[InsuranceLineCode]
           ,[PlanCoverageDescr]
           ,[RetroactiveFlag]
           ,[BenefitBeginDate]
           ,[BenefitEndDate])
 SELECT
       CAST([FileID] as int)
      ,CAST([ImportRecordID] as int)
      ,CAST([TestCaseID] as varchar(50))
      ,CAST([TestID] as varchar(10))
      ,null as [TransmissionID]
      ,null as [GroupID]
      ,null as [SetID]
      ,null as [LoopID]
      ,CAST([FileType] as varchar(50))
      ,CAST([FilePopulation] as varchar(50))
      ,CAST(REPLACE([DataDescription], '"','') as varchar(5000)) as DataDescription
      ,CAST([InterchangeReceiverID] as varchar(15))
      ,CAST([InterchangeDate] as varchar(6))
      ,CAST([InterchangeTime] as varchar(4))
      ,CAST([InterchangeControlNumber] as varchar(9))
      ,CAST([ApplicationSendersCode] as varchar(15))
      ,CAST([ApplicationReceiversCode] as varchar(15))
      ,CAST([FunctionalGroupDate] as varchar(8))
      ,CAST([FunctionalGroupTime] as varchar(6))
      ,CAST([FunctionalGroupControlNumber] as varchar(9))
      ,CAST([TransactionSetControlNumber] as varchar(9))
      ,CAST([SegmentDate] as varchar(8))
      ,CAST([SegmentTime] as varchar(6))
      ,CAST([MaintenanceTypeCode] as varchar(3))
      ,CAST([MaintenanceReasonCode] as varchar(3))
      ,CAST([EmploymentStatusCode] as varchar(50))
      ,CAST([RecipientID] as varchar(50))
      ,CAST([OldSite] as varchar(50)) as Site
      ,CAST([CaseIDNumber] as varchar(50))
      ,CAST([MedicaidBegin] as DATE)
      ,CAST([MedicaidEnd] as DATE)
      ,CAST([RecipientLastName] as varchar(50))
      ,CAST([RecipientFirstName] as varchar(50))
      ,CAST([TelephoneNumber] as varchar(50))
      ,CAST([CorrectedRecipientLastName] as varchar(50))
      ,CAST([CorrectedRecipientFirstName] as varchar(50))
      ,CAST([CaseAddress1] as varchar(50))
      ,CAST([CaseAddress2] as varchar(50))
      ,CAST([CaseCity] as varchar(50))
      ,CAST([CaseState] as varchar(50))
      ,CAST([CaseZip] as varchar(50))
      ,CAST([RecipientBirthdate] as varchar(50))
      ,CAST([RecipientGenderCode] as varchar(50))
      ,CAST([RaceEthnicityCode] as varchar(50))
      ,CAST([SpanishLanguage] as varchar(50))
      ,CAST([UseOfLanguageIndicator] as varchar(50))
      ,CAST([OldRecipientLastName] as varchar(50))
      ,CAST([OldRecipientFirstName] as varchar(50))
      ,CAST([OldRecipientBirthdate] as varchar(50))
      ,CAST([OldRecipientGenderCode] as varchar(50))
      ,CAST([CaseLastName] as varchar(50))
      ,CAST([CaseFirstName] as varchar(50))
      ,CAST([ResponsiblePersonTelephoneNumber] as varchar(50))
      ,CAST([InsuranceLineCode] as varchar(50))
      ,CAST([PlanCoverageDescr] as varchar(50))
      ,CAST([RetroactiveFlag] as varchar(50))
      ,CAST([BenefitBeginDate] as DATE)
      ,CAST([BenefitEndDate] as DATE)
  FROM [GenesisStaging].[Staging].[EDITestCaseData2]

答案 1 :(得分:1)

原来问题是,逗号包含在CSV中的字段中,因此程序无法正确显示列。 感谢大家!所有好建议,特别是bcp和ETL!