SQL 服务器 BCP 导出 SQL 字段中的逗号

SQL Server BCP Export where comma in SQL field

我已成功将文件导出为 CSV。我使用以下代码导出文件:

exec xp_cmdshell 'bcp "[DC_TrainEnvironment].[dbo].[HAFacilities_Master]" out "\fspincdc01\data$\HIMSSAnalytics\Data Analyst\HG Data\Technical Documentation\HA Facilities\HAFacilities_Master.csv" -f "\fspincdc01\data$\HIMSSAnalytics\Data Analyst\HG Data\Technical Documentation\HA Facilities\HAFacilities_Master_FORMAT.fmt" -o "\fspincdc01\data$\HIMSSAnalytics\Data Analyst\HG Data\Technical Documentation\HA Facilities\HAFacilities_Master_LOG.txt" -T -S "HAPDBCDC02,2112"'

这很好用,但是有一个问题。某些字段中有逗号。我正在使用此格式文件:

我可以在格式文件中做些什么来避免必须更改整个格式文件?有没有一种快速的方法来指示字段终止符不会在那些特定字段中以逗号结尾? IE。 “','“ 要么 ”'',''”?

感谢任何帮助。谢谢

好不容易。您需要 select bcp 命令中的列并替换逗号。

exec xp_cmdshell 'bcp "SELECT ''ColumnHeader'' UNION ALL SELECT REPLACE(Column1, '','', '''') FROM [DC_TrainEnvironment].[dbo].[HAFacilities_Master]" out "\fspincdc01\data$\HIMSSAnalytics\Data Analyst\HG Data\Technical Documentation\HA Facilities\HAFacilities_Master.csv" -f "\fspincdc01\data$\HIMSSAnalytics\Data Analyst\HG Data\Technical Documentation\HA Facilities\HAFacilities_Master_FORMAT.fmt" -o "\fspincdc01\data$\HIMSSAnalytics\Data Analyst\HG Data\Technical Documentation\HA Facilities\HAFacilities_Master_LOG.txt" -T -S "HAPDBCDC02,2112"'

如果您要导出的字段可以包含您用来分隔文件中字段的字符,您的选择是:

  1. 使用不同的分隔符 - 这似乎是最简单的方法。只需使用“|”或“~”作为分隔符。将“,”全局替换为“|”在几乎任何文本编辑器的格式文件中。目前尚不清楚为什么很难修改 "the whole" 文件。也许您的文件收件人需要逗号分隔?

  2. 如果必须使用逗号作为分隔符,则必须将列分隔符从逗号 (,) 更改为引号-逗号-引号 (",")。为此,您需要使用转义字符让 BCP 程序忽略要在输出文件中用作定界符的引号,这样它就不会将它们视为格式文件中包含定界符的引号.所以...

代替“,”...使用...“\”,\"

这将导致以下结果

col1,col2,"col,3",col4

对于 col1,分隔符是:,在格式文件中使用:","

对于 col2,分隔符是:, 在格式文件中使用:",\"

对于 col3,分隔符是:",在格式文件中使用:"\","

对于 col4,分隔符是:,在格式文件中使用:","

希望对您有所帮助。

这是我的 C# 解决方案,您必须变形才能在 SQL 服务器中使用,但是模板 none the less:

private static void EliminateCommasInFields(List<string> files, string path)
{
    foreach (var f in files)
    {
        string filename = path + f.Substring(0, f.Length - 4);

        StreamReader sr = new StreamReader(filename);
        String[] rows = Regex.Split(sr.ReadToEnd(), "\r\n");
        sr.Close();
        StreamWriter sw = new StreamWriter(filename);

        for (int i = 0; i < rows.Length; i++)
        {
            //search the row for the dreaded ," (indicating a comma in the field)
            while (rows[i].IndexOf(",\"") > 0) 
                {
                    //find the position of the first ," and it's ending ",
                    int start = rows[i].IndexOf(",\""); 
                    int end = rows[i].IndexOf("\",");
                    //find the position of the first comma within this field
                    int comma = rows[i].IndexOf(",", start + 1, end - start);
                    while (comma > 0) //eliminate all the commas within this cell
                    {
                        //Replace the offending comma with a space 
                        rows[i] = rows[i].Substring(0, comma) + " " + rows[i].Substring(comma + 1, rows[i].Length - (comma + 1)); 
                        //Search for next comma
                        comma = rows[i].IndexOf(",", start + 1, end - start);
                    }
                    //Save the rest of the row eliminating the double quotes for this cell
                    rows[i] = rows[i].Substring(0, end ) + rows[i].Substring(end + 1, rows[i].Length - (end + 1));
                    rows[i] = rows[i].Substring(0, start + 1) + rows[i].Substring(start+ 2, rows[i].Length - (start + 2));

                }

            sw.WriteLine(rows[i]);
        }
        sw.Close();
    }
}