【问题标题】:Wrong data is inserted in sql serversql server 中插入了错误的数据
【发布时间】:2015-10-21 20:55:49
【问题描述】:

我想知道在 sqlserver 中导入具有类似数据的 CSV 文件的正确方法是什么

GLMAC1,GLMAC2,GLMAC3,GLYR,GLMON,GLSUB,GLTREF,GLDATE,GLDESC,GLAMT,GLSRC,GLBTCH,GLMCMP

1    ,32   ,110  ,13  ,1   ,0          ,171406200A  ,120801  ,MH YM PANT W/DRAWS     ,-.15         ,NOIA,ITCGR119,1    
1    ,32   ,110  ,13  ,1   ,13402747   ,446286      ,120801  ,URBAN 1714062     ,15.13        ,904 ,ITCGR11B,1    
1    ,32   ,110  ,13  ,1   ,0          ,172830300A  ,120801  ,OP 5+2 SOCKS       ,-.39         ,NOIA,ITCGR165,1    
1    ,32   ,110  ,13  ,1   ,13402802   ,338728      ,120801  ,INDUSTRIES 1728303     ,39.28        ,904 ,ITCGR16C,1    
1    ,32   ,110  ,13  ,1   ,0          ,171450700A  ,120801  ,FA M.3PK FASHION S     ,-.08         ,NOIA,ITCGR19Z,1    
1    ,32   ,110  ,13  ,1   ,13402845   ,121811      ,120801  ,BO & CO...      1714507     ,7.49         ,904 ,ITCGR1B0,1 

这样的行大约有 5000 万行,我想在 SQL Server 中导入这些数据,但我注意到在导入数据后,有些列会转移到另一列,这可能是因为第 9 列可能有一些逗号(,) 值,SQL Server 选择它作为 (,) 分隔符。

有没有一种方法可以在没有错误的情况下在 sql server 中插入数据,或者在插入之前可能会清理 CSV 文件。该文件大小约为 8 GB,我必须使用 010Editor 在编辑器或任何可用的软件中打开文件,这些软件可以帮助我找出第 9 列中包含 (,) 的值,以便我可以手动删除逗号.

【问题讨论】:

    标签: csv import etl ssis-2012 data-cleaning


    【解决方案1】:

    此 C# 代码将读取文件,将在第 9 个字段周围添加一对 "s 来分隔它,并将其写入新的输出文件。例如:

    这样的一行

    1    ,32   ,110  ,13  ,1   ,0          ,171406200A  ,120801  ,MH YM,PANT W/DRAWS     ,-.15         ,NOIA,ITCGR119,1    
    

    将被写入输出文件为:

    1    ,32   ,110  ,13  ,1   ,0          ,171406200A  ,120801  ,"MH YM,PANT W/DRAWS     ",-.15         ,NOIA,ITCGR119,1    
    

    正确分隔文本列后,它将适合导入 SQL Server。

    代码如下:

    using System.Text.RegularExpressions;
    
    void Main() {
        Regex regex = new Regex("(.*?,.*?,.*?,.*?,.*?,.*?,.*?,.*?,)(.*)(,.*?,.*?,.*?,)", RegexOptions.CultureInvariant | RegexOptions.Compiled);
        string regexReplace = "$1\"$2\"$3";
    
        // The file to read - change this to your location
        var iStream = new FileStream(@"R:\FILE.CSV", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
        // The file to write 
        var oStream = new FileStream(@"R:\FIXEDFILE.CSV", FileMode.Create, FileAccess.Write, FileShare.Read); 
    
        int lineNo=0;
        var sw = new System.IO.StreamWriter(oStream);
        var sr = new System.IO.StreamReader(iStream); 
        while(!sr.EndOfStream) {
            lineNo++;
            string line=sr.ReadLine();
            if (!regex.IsMatch(line)) {
                // Bad lines don't get written to the output file
                Console.WriteLine("Line {0} is bad - does not match the expected format:\n\r  {1}", lineNo, line);
            } else {
                // Write the line with the ""'s around the 9th field
                sw.WriteLine(regex.Replace(line,regexReplace));
            }
            if (lineNo%10000==0) Console.WriteLine("{0} lines processed", lineNo);
        }
        sr.Close();
        sw.Close();
        Console.WriteLine("Finished. Written {0} lines", lineNo);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多