SQL 导入管道,其中一些数据字段在记录中有管道

SQL import pipe delimited with some data fields that have have pipe in record

我目前在 SQL 中使用批量插入来导入管道 (|) 分隔,这非常简单。我 运行 遇到的问题是有时有些记录包含管道 (|),然后在 return 批量插入中将其分成两个不同的记录。下面是一个例子

12343|First Name|Last Name| Address field|Location
63494|Second First Name|Second Last Name| Address Field with | in it |location

我上面的例子,第二条记录,批量插入会拆分地址字段,因为它包含一个|。有什么建议可以用来避免这样的问题吗?

谢谢

我以前遇到过同样的问题,根据我的经验,在导入过程中您无能为力。显然,如果您在从源头导出过程中有任何控制权,那么您可以在此时处理数据清理,购买很可能不是您的情况。您至少可以做一件事来防止导入过程中的失败,那就是在批量插入之前验证您的输入文件,就像我用这样一个简单的代码所做的那样:

    public class ValidateMigrationFile {

    private static final String REGEX = "^([^|]*\|){50}[^|]*$";

    public static void testFile(String fileName) {

        int lineCounter = 1;
        int totalErrors = 0;

        try {

            BufferedReader br = new BufferedReader(new FileReader(fileName));

            String line = null;

            while ((line=br.readLine())!=null) {

                // Validate the line is formatted correctly based on regular expressions                
                if (!line.matches(REGEX)){
                    System.out.println("Invalid format on line " + lineCounter + " (" + line + ")");
                    totalErrors++;
                }

                lineCounter++;
            }
            br.close();
            System.out.println("Total rows processed: " + --lineCounter);
            System.out.println("Total errors found: " + totalErrors);


        } catch (Exception ex) {
            System.out.println("Exception occurred: " + ex.toString());
        }
    }
}

这样您就可以提前检测您的文件格式是否正确,并准确检测出哪些行有问题。