如何让BCP生成格式文件以将固定宽度的数据导入SQL Server表?

时间:2012-03-22 00:38:33

标签: sql sql-server xml bcp

我正在使用的bcp命令:

bcp TableName格式为nul -c -f c:\ folder \ TargetFile.xml -x -S ServerName -T -q

我想我只需要字段的类型为 xsi:type =“CharFixed”而不是 xsi:type =“CharTerm”

它创建的xml对我不起作用:

<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <RECORD>
  <FIELD ID="1" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="24" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="2" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="150" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="150" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="4" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="20" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="5" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="12" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1" NAME="UID" xsi:type="SQLNCHAR"/>
  <COLUMN SOURCE="2" NAME="FNAME" xsi:type="SQLNCHAR"/>
  <COLUMN SOURCE="3" NAME="LNAME" xsi:type="SQLNCHAR"/>
  <COLUMN SOURCE="4" NAME="PHONE" xsi:type="SQLNCHAR"/>
  <COLUMN SOURCE="5" NAME="Target" xsi:type="SQLNCHAR"/>
 </ROW>
</BCPFORMAT>

我真正需要的是:(xsi:type =“CharFixed”)

<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
  <FIELD ID="1" xsi:type="CharFixed" LENGTH="3"/>
  <FIELD ID="2" xsi:type="CharFixed" LENGTH="3"/>
</RECORD>
<ROW>
  <COLUMN SOURCE="1" NAME="Field1" xsi:type="SQLCHAR" LENGTH="3"/>
  <COLUMN SOURCE="2" NAME="Field2" xsi:type="SQLCHAR" LENGTH="3"/>
</ROW>
</BCPFORMAT>

2 个答案:

答案 0 :(得分:3)

以下是我创建的帮助我解决问题的方法......

private XmlDocument CreateFormatFile()
    {
        const string xsiURI = "http://www.w3.org/2001/XMLSchema-instance";
        var ff = new XmlDocument();
        var dec = ff.CreateXmlDeclaration("1.0", null, null);
        ff.AppendChild(dec);
        var bcpFormat = ff.CreateElement("BCPFORMAT");
        bcpFormat.SetAttribute("xmlns", "http://schemas.microsoft.com/sqlserver/2004/bulkload/format");
        bcpFormat.SetAttribute("xmlns:xsi", xsiURI);
        var record = ff.CreateElement("RECORD");
        var row = ff.CreateElement("ROW");
        for (var x = 0; x < Columns.Count; x++)
        {
            var col = Columns[x];
            var id = (col.Index + 1).ToString();
            var length = col.Length.ToString();
            var column = ff.CreateElement("COLUMN");
            column.SetAttribute("SOURCE", id);
            column.SetAttribute("NAME", col.Name);
            column.SetAttribute("type", xsiURI, "SQLCHAR");
            column.SetAttribute("LENGTH", length);


            var field = ff.CreateElement("FIELD");
            field.SetAttribute("ID", id);
            if (x != Columns.Count - 1)
            {
                field.SetAttribute("type", xsiURI, "CharFixed");
                field.SetAttribute("LENGTH", length);
            }
            else
            {
                field.SetAttribute("type", xsiURI, "CharTerm");
                field.SetAttribute("TERMINATOR", @"\r\n");
            }

            record.AppendChild(field);
            row.AppendChild(column);
        }
        bcpFormat.AppendChild(record);
        bcpFormat.AppendChild(row);
        ff.AppendChild(bcpFormat);
        return ff;
    }

答案 1 :(得分:0)

尝试使用bcp Native Format Option

  

bcp如何处理本机格式的数据

     

...

     

char或varchar数据

     

在每个char或varchar字段的开头,bcp添加前缀长度。

您使用“-n”选项而不是“-c”:

bcp TableName格式为nul -n -f c:\ folder \ TargetFile.xml -x -S ServerName -T -q