将数据从一行解析为CSV文件中的多行

时间:2013-12-11 19:17:49

标签: c# csv ssis rows

我是SSIS的新手,需要一些帮助来确定如何解析这些数据。课程级学习目标需要分成多行,[]中的数据需要移动到它自己的列。任何帮助将不胜感激。 CSV文件包含多个记录。以下示例只是一条记录。

CSV文件的当前格式

Prefix/Code,Name,Credits,Description,Course-level Learning Objectives

ABE 095,Keys to Academic Success,3.0 ,"Basic .. assessment. ","   

Identify learn. [EXPLORE] 
Evaluate personal,  goals. [ACT] 
Utilize development. [EXPLORE] 
"

格式化文件需要

Prefix/Code,Name,Credits,Description,Course-level Learning Objectives,Type

ABE 095,Keys to Academic Success,3.0 ,"Basic .. assessment.","Identify learn.", [EXPLORE] 
ABE 095,Keys to Academic Success,3.0 ,"Basic .. assessment.","Evaluate goals.", [ACT] 
ABE 095,Keys to Academic Success,3.0 ,"Basic .. assessment.","Utilize dev.", [EXPLORE] 

2 个答案:

答案 0 :(得分:0)

在SSIS 2008中没有直接的方式来使用该文件,但可以在SSIS 2012中完成,然后转动数据,如果你想在SSIS 2008中这样做你应该使用脚本任务来格式化文件然后在DF中使用平面文件源,在脚本任务中你应该使用文件阅读器来实现它,有关拆分此文件的更多信息,请参阅此链接http://sqlbisam.blogspot.com/2013/12/parsing-data-from-one-row-into-multiple.html

答案 1 :(得分:0)

基本步骤:

  1. 使用SSIS将数据导入SQL Server中的表
  2. 杀死“课程级学习目标”龙
  3. 取消结果
  4. 以下是“第N个”索引函数的代码段:

        CREATE FUNCTION [dbo].[udf_NthIndex] 
                       (@Input     VARCHAR(8000), 
                        @Delimiter CHAR(1), 
                        @Ordinal   INT) 
    
        RETURNS INT 
        AS 
    
          BEGIN 
    
            DECLARE  @Pointer INT, 
                     @Last    INT, 
                     @Count   INT 
    
            SET @Pointer = 1 
            SET @Last = 0 
            SET @Count = 1 
    
            WHILE (2 > 1) 
              BEGIN 
                SET @Pointer = CHARINDEX(@Delimiter,@Input,@Pointer) 
                IF @Pointer = 0 
                  BREAK 
                IF @Count = @Ordinal 
    
                  BEGIN 
                    SET @Last = @Pointer 
                    BREAK 
                  END 
                SET @Count = @Count + 1 
                SET @Pointer = @Pointer + 1 
    
              END 
    
            RETURN @Last 
    
          END
    
        GO
    ;
    

    此方法使用Common Table Expressions,“第N”索引函数和UNPIVOT

    解决此问题
    WITH s1
        AS ( SELECT 'ABE 095' AS [Prefix/Code]
                  , 'Keys to Academic Success' AS Name
                  , '3.0' AS Credits
                  , 'Basic .. assessment. ' AS Description
                  , '   
    
    Identify learn. [EXPLORE] 
    Evaluate personal,  goals. [ACT] 
    Utilize development. [EXPLORE] 
    ' AS [Course-level Learning Objectives]
           ) , s2
        AS ( SELECT [Prefix/Code]
                  , Name
                  , Credits
                  , Description
                  , dbo.udf_NthIndex( [Course-level Learning Objectives] , CHAR( 13
                                                                               ) , 2
                                    ) + 2 AS Type1Start
                  , dbo.udf_NthIndex( [Course-level Learning Objectives] , CHAR( 13
                                                                               ) , 3
                                    ) - dbo.udf_NthIndex( [Course-level Learning Objectives] , CHAR( 13
                                                                                                   ) , 2
                                                        ) + 0 AS Type1Length
                  , dbo.udf_NthIndex( [Course-level Learning Objectives] , CHAR( 13
                                                                               ) , 3
                                    ) + 2 AS Type2Start
                  , dbo.udf_NthIndex( [Course-level Learning Objectives] , CHAR( 13
                                                                               ) , 4
                                    ) - dbo.udf_NthIndex( [Course-level Learning Objectives] , CHAR( 13
                                                                                                   ) , 3
                                                        ) + 0 AS Type2Length
                  , dbo.udf_NthIndex( [Course-level Learning Objectives] , CHAR( 13
                                                                               ) , 4
                                    ) + 2 AS Type3Start
                  , dbo.udf_NthIndex( [Course-level Learning Objectives] , CHAR( 13
                                                                               ) , 5
                                    ) - dbo.udf_NthIndex( [Course-level Learning Objectives] , CHAR( 13
                                                                                                   ) , 4
                                                        ) + 0 AS Type3Length
               FROM s1
           ) , s3
        AS ( SELECT s2.[Prefix/Code]
                  , s2.Name
                  , s2.Credits
                  , s2.Description
                  , RTRIM( LTRIM( SUBSTRING( s1.[Course-level Learning Objectives] , s2.Type1Start , Type1Length
                                           )
                                )
                         )AS Type1_chunk
                  , RTRIM( LTRIM( SUBSTRING( s1.[Course-level Learning Objectives] , s2.Type2Start , Type2Length
                                           )
                                )
                         )AS Type2_chunk
                  , RTRIM( LTRIM( SUBSTRING( s1.[Course-level Learning Objectives] , s2.Type3Start , Type3Length
                                           )
                                )
                         )AS Type3_chunk
               FROM s1 , s2
           ) , unpivot1
        AS ( SELECT [Prefix/Code]
                  , Name
                  , Credits
                  , Description
                  , Type_chunk
               FROM( 
                     SELECT [Prefix/Code]
                          , Name
                          , Credits
                          , Description
                          , Type1_chunk
                          , Type2_chunk
                          , Type3_chunk
                       FROM s3
                   )p UNPIVOT( Type_chunk FOR Type_descrip IN( Type1_chunk
                                                             , Type2_chunk
                                                             , Type3_chunk
                                                             )
                                                             )AS unpvt
           )
        SELECT [Prefix/Code]
             , Name
             , Credits
             , Description
             --, Type_chunk
             , LEFT( u.Type_chunk , -2 + dbo.udf_NthIndex( u.Type_chunk , '[' , 1
                                                         )
                   )AS [Learning Objectives]
             , RIGHT( u.Type_chunk , 1 + LEN( u.Type_chunk
                                            ) - dbo.udf_NthIndex( u.Type_chunk , '[' , 1
                                                                )
                    )AS Type
          FROM unpivot1 u;
    

    如果您能够使用正则表达式,则可以保存一些代码。在SQL Server 2008中使用RegEx需要CLR。 This book可以很好地向您展示如何逐步完成该操作。该解决方案足以满足每门课程少量的“类型”值。