【问题标题】:Correct BCP syntax正确的 BCP 语法
【发布时间】:2010-08-30 17:35:18
【问题描述】:

我是第一次使用 BCP。我有一个 XML 文件,需要将其导入到 SQL Server 中的新表中。这是使用 SQL Server 2008 和 BCP 版本 10.50.16。我通读了文档,但出现了 370,000 个错误! (这是一个非常大的文件)。当我打开错误日志时,我只看到一堆问号。

关于这个 XML 文件:它确实使用新行来描述行,但是,它使用空格字符来分隔字段。所以我不确定这是否是导致它失败的原因。失败信息是:

BCP 复制失败我希望它更具描述性。有没有办法获得更详细的错误消息?

否则,这是我的命令:

d:\SQL Tables\data>bcp Development.dbo.wbl_zSkywardEnrollment2 in zSkywardEnroll
ment.xml -fSE_format.fmt -m50 -eseErrorLog -b100 -t0x20 -T -F107

所有这些文件都在同一个目录中。我知道它说要指定完整的文件路径,但是由于它们都在同一个目录中,而且我还看到了另一个没有指定目录的示例,我认为这就足够了。

我也尝试过不使用格式文件,以交互方式执行此操作,但得到相同的错误。

这是我的格式文件:

10.0
32
1       SQLINT              0       4       " "    1     SCHOOL_YEAR                                ""
2       SQLNCHAR            2       20      " "    2     ENTITY_ID                                  SQL_Latin1_General_CP1_CI_AS
3       SQLINT              0       4       " "    3     TERM_NBR                                   ""
4       SQLINT              1       4       " "    4     SCHD_HST_GRAD_YEAR                         ""
5       SQLNCHAR            2       60      " "    5     OTHER_ID                                   SQL_Latin1_General_CP1_CI_AS
6       SQLNUMERIC          1       19      " "    6     GRAD_YR                                    ""
7       SQLNCHAR            2       120     " "    7     LAST_NAME                                  SQL_Latin1_General_CP1_CI_AS
8       SQLNCHAR            2       60      " "    8     FIRST_NAME                                 SQL_Latin1_General_CP1_CI_AS
9       SQLNCHAR            2       60      " "    9     MIDDLE_NAME                                SQL_Latin1_General_CP1_CI_AS
10      SQLDATETIME         1       8       " "    10    BIRTHDATE                                  ""
11      SQLNCHAR            2       4       " "    11    GENDER                                     SQL_Latin1_General_CP1_CI_AS
12      SQLNCHAR            2       20      " "    12    RACE_CODE                                  SQL_Latin1_General_CP1_CI_AS
13      SQLNCHAR            2       40      " "    13    DISTRICT_CODE                              SQL_Latin1_General_CP1_CI_AS
14      SQLBIT              1       1       " "    14    X_SPECIAL_EDUCATION                        ""
15      SQLBIT              1       1       " "    15    X_GIFTED_TALENTED                          ""
16      SQLBIT              1       1       " "    16    X_SECTION_504                              ""
17      SQLBIT              1       1       " "    17    X_MIGRANT                                  ""
18      SQLBIT              1       1       " "    18    X_IEP                                      ""
19      SQLBIT              1       1       " "    19    X_IEP_ACCOM                                ""
20      SQLBIT              1       1       " "    20    X_ESL                                      ""
21      SQLNCHAR            2       4       " "    21    STUDENT_STATUS                             SQL_Latin1_General_CP1_CI_AS
22      SQLINT              0       4       " "    22    STUDENT_ID                                 ""
23      SQLINT              1       4       " "    23    ENG_PROF                                   ""
24      SQLNCHAR            2       60      " "    24    ALPHAKEY                                   SQL_Latin1_General_CP1_CI_AS
25      SQLNCHAR            2       20      " "    25    SCHOOL_ID                                  SQL_Latin1_General_CP1_CI_AS
26      SQLNUMERIC          1       19      " "    26    MN_EDE_NBR                                 ""
27      SQLNCHAR            2       20      " "    27    LANGUAGE_CODE                              SQL_Latin1_General_CP1_CI_AS
28      SQLINT              1       4       " "    28    ADVISOR                                    ""
29      SQLBIT              1       1       " "    29    MN_LIMITED_ENGLISH                         ""
30      SQLNCHAR            2       20      " "    30    TYPE_STUDENT_ID                            SQL_Latin1_General_CP1_CI_AS
31      SQLNCHAR            2       20      " "    31    CY_TEAM_SCHD_ID                            SQL_Latin1_General_CP1_CI_AS
32      SQLNCHAR            2       20      " "    32    HOMEROOM_NUMBER                            SQL_Latin1_General_CP1_CI_AS

这是我的 SQL 表:

CREATE TABLE [dbo].[wbl_zSkywardEnrollment2](
    [SCHOOL_YEAR] [int] NOT NULL,
    [ENTITY_ID] [nvarchar](10) NOT NULL,
    [TERM_NBR] [int] NOT NULL,
    [SCHD_HST_GRAD_YEAR] [int] NULL,
    [OTHER_ID] [nvarchar](30) NULL,
    [GRAD_YR] numeric(19,0) NULL,
    [LAST_NAME] [nvarchar](60) NOT NULL,
    [FIRST_NAME] [nvarchar](30) NULL,
    [MIDDLE_NAME] [nvarchar](30) NULL,
    [BIRTHDATE] datetime NULL,
    [GENDER] [nvarchar](2) NULL,
    [RACE_CODE] [nvarchar](10) NULL,
    [DISTRICT_CODE] [nvarchar](20) NULL,
    [X_SPECIAL_EDUCATION] bit NULL,
    [X_GIFTED_TALENTED] bit NULL,
    [X_SECTION_504] bit NULL,
    [X_MIGRANT] bit NULL,
    [X_IEP] bit NULL,
    [X_IEP_ACCOM] bit NULL,
    [X_ESL] bit NULL,
    [STUDENT_STATUS] [nvarchar](2) NULL,
    [STUDENT_ID] [int] NOT NULL,
    [ENG_PROF] [int] NULL,
    [ALPHAKEY] [nvarchar](30) NOT NULL,
    [SCHOOL_ID] [nvarchar](10) NULL,
    [MN_EDE_NBR] [numeric](19, 0) NULL,
    [LANGUAGE_CODE] [nvarchar](10) NULL,
    [ADVISOR] [int] NULL,
    [MN_LIMITED_ENGLISH] bit NULL,
    [TYPE_STUDENT_ID] [nvarchar](10) NULL,
    [CY_TEAM_SCHD_ID] [nvarchar](10) NULL,
    [HOMEROOM_NUMBER] [nvarchar](10) NOT NULL
) ON [PRIMARY]

我已经尝试过从第 1 行开始。我很好奇 BCP 应该从哪一行开始?此 XML 的第 1 行以 Schema 和 ElementType 信息开头。但是第 107 行是 rs:data 部分的开始位置。

此 XML 文件的第一部分:

<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'

    xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'

    xmlns:rs='urn:schemas-microsoft-com:rowset'

    xmlns:z='#RowsetSchema'>

<s:Schema id='RowsetSchema'>

    <s:ElementType name='row' content='eltOnly'>

        <s:AttributeType name='c0' rs:name='SCHOOL_YEAR' rs:number='1'>

            <s:datatype dt:type='int' dt:maxLength='4' rs:precision='10' rs:fixedlength='true' rs:maybenull='false'/>

        </s:AttributeType>

        <s:AttributeType name='c1' rs:name='ENTITY_ID' rs:number='2'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='10' rs:maybenull='false'/>

        </s:AttributeType>

        <s:AttributeType name='c2' rs:name='TERM_NBR' rs:number='3'>

            <s:datatype dt:type='int' dt:maxLength='4' rs:precision='10' rs:fixedlength='true' rs:maybenull='false'/>

        </s:AttributeType>

        <s:AttributeType name='c3' rs:name='SCHD_HST_GRAD_YEAR' rs:number='4' rs:nullable='true'>

            <s:datatype dt:type='int' dt:maxLength='4' rs:precision='10' rs:fixedlength='true'/>

        </s:AttributeType>

        <s:AttributeType name='c4' rs:name='OTHER_ID' rs:number='5' rs:nullable='true'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='24'/>

        </s:AttributeType>

        <s:AttributeType name='c5' rs:name='GRAD_YR' rs:number='6' rs:nullable='true'>

            <s:datatype dt:type='number' rs:dbtype='numeric' dt:maxLength='19' rs:scale='0' rs:precision='15' rs:fixedlength='true'/>

        </s:AttributeType>

        <s:AttributeType name='c6' rs:name='LAST_NAME' rs:number='7'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='60' rs:maybenull='false'/>

        </s:AttributeType>

        <s:AttributeType name='c7' rs:name='FIRST_NAME' rs:number='8' rs:nullable='true'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='30'/>

        </s:AttributeType>

        <s:AttributeType name='c8' rs:name='MIDDLE_NAME' rs:number='9' rs:nullable='true'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='30'/>

        </s:AttributeType>

        <s:AttributeType name='BIRTHDATE' rs:name='BIRTHDATE' rs:number='10' rs:nullable='true' rs:write='true'>

            <s:datatype dt:type='date' dt:maxLength='6' rs:fixedlength='true'/>

        </s:AttributeType>

        <s:AttributeType name='GENDER' rs:name='GENDER' rs:number='11' rs:nullable='true' rs:write='true'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='2'/>

        </s:AttributeType>

        <s:AttributeType name='c11' rs:name='RACE_CODE' rs:number='12' rs:nullable='true'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='6'/>

        </s:AttributeType>

        <s:AttributeType name='c12' rs:name='DISTRICT_CODE' rs:number='13' rs:nullable='true'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='14'/>

        </s:AttributeType>

        <s:AttributeType name='c13' rs:name='X_SPECIAL_EDUCATION' rs:number='14' rs:nullable='true'>

            <s:datatype dt:type='boolean' dt:maxLength='2' rs:fixedlength='true'/>

        </s:AttributeType>

        <s:AttributeType name='c14' rs:name='X_GIFTED_TALENTED' rs:number='15' rs:nullable='true'>

            <s:datatype dt:type='boolean' dt:maxLength='2' rs:fixedlength='true'/>

        </s:AttributeType>

        <s:AttributeType name='c15' rs:name='X_SECTION_504' rs:number='16' rs:nullable='true'>

            <s:datatype dt:type='boolean' dt:maxLength='2' rs:fixedlength='true'/>

        </s:AttributeType>

        <s:AttributeType name='c16' rs:name='X_MIGRANT' rs:number='17' rs:nullable='true'>

            <s:datatype dt:type='boolean' dt:maxLength='2' rs:fixedlength='true'/>

        </s:AttributeType>

        <s:AttributeType name='c17' rs:name='X_IEP' rs:number='18' rs:nullable='true'>

            <s:datatype dt:type='boolean' dt:maxLength='2' rs:fixedlength='true'/>

        </s:AttributeType>

        <s:AttributeType name='c18' rs:name='X_IEP_ACCOM' rs:number='19' rs:nullable='true'>

            <s:datatype dt:type='boolean' dt:maxLength='2' rs:fixedlength='true'/>

        </s:AttributeType>

        <s:AttributeType name='c19' rs:name='X_ESL' rs:number='20' rs:nullable='true'>

            <s:datatype dt:type='boolean' dt:maxLength='2' rs:fixedlength='true'/>

        </s:AttributeType>

        <s:AttributeType name='c20' rs:name='STUDENT_STATUS' rs:number='21' rs:nullable='true'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='2'/>

        </s:AttributeType>

        <s:AttributeType name='c21' rs:name='STUDENT_ID' rs:number='22'>

            <s:datatype dt:type='int' dt:maxLength='4' rs:precision='10' rs:fixedlength='true' rs:maybenull='false'/>

        </s:AttributeType>

        <s:AttributeType name='c22' rs:name='ENGL_PROF' rs:number='23' rs:nullable='true'>

            <s:datatype dt:type='int' dt:maxLength='4' rs:precision='10' rs:fixedlength='true'/>

        </s:AttributeType>

        <s:AttributeType name='ALPHAKEY' rs:name='ALPHAKEY' rs:number='24' rs:write='true'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='22' rs:maybenull='false'/>

        </s:AttributeType>

        <s:AttributeType name='c24' rs:name='SCHOOL_ID' rs:number='25'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='10' rs:maybenull='false'/>

        </s:AttributeType>

        <s:AttributeType name='c25' rs:name='MN_EDE_NBR' rs:number='26' rs:nullable='true'>

            <s:datatype dt:type='number' rs:dbtype='numeric' dt:maxLength='19' rs:scale='0' rs:precision='15' rs:fixedlength='true'/>

        </s:AttributeType>

        <s:AttributeType name='c26' rs:name='LANGUAGE_CODE' rs:number='27' rs:nullable='true'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='6'/>

        </s:AttributeType>

        <s:AttributeType name='c27' rs:name='ADVISOR' rs:number='28' rs:nullable='true' rs:write='true'>

            <s:datatype dt:type='int' dt:maxLength='4' rs:precision='10' rs:fixedlength='true'/>

        </s:AttributeType>

        <s:AttributeType name='c28' rs:name='MN_LIMITED_ENGLISH' rs:number='29' rs:nullable='true'>

            <s:datatype dt:type='boolean' dt:maxLength='2' rs:fixedlength='true'/>

        </s:AttributeType>

        <s:AttributeType name='c29' rs:name='TYPE_STUDENT_ID' rs:number='30' rs:nullable='true'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='6'/>

        </s:AttributeType>

        <s:AttributeType name='c30' rs:name='CY_TEAM_SCHD_ID' rs:number='31' rs:nullable='true'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='6'/>

        </s:AttributeType>

        <s:AttributeType name='c31' rs:name='HOMEROOM_NUMBER' rs:number='32'>

            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='10' rs:maybenull='false'/>

        </s:AttributeType>

        <s:extends type='rs:rowbase'/>

    </s:ElementType>

</s:Schema>

<rs:data>

    <z:row c0='2004' c1='057' c2='0' c3='2010' c4='671590' c5='2010' c6='AANENSON' c7='SHELLI' c8='A' BIRTHDATE='1992-07-01'

         GENDER='F' c11='5' c12='' c13='False' c14='True' c15='False' c16='False' c17='False' c18='False' c19='False'

         c20='I' c21='12' c22='7' ALPHAKEY='AANENSHE000' c24='057' c25='624000671590' c26='011' ADVISOR='0' c28='False'

         c29='R' c30='' c31=''/>

    <z:row c0='2004' c1='057' c2='0' c3='2010' c4='671590' c5='2010' c6='AANENSON' c7='SHELLI' c8='A' BIRTHDATE='1992-07-01'

         GENDER='F' c11='5' c12='' c13='False' c14='True' c15='False' c16='False' c17='False' c18='False' c19='False'

         c20='I' c21='12' c22='7' ALPHAKEY='AANENSHE000' c24='057' c25='624000671590' c26='011' ADVISOR='0' c28='False'

         c29='R' c30='' c31=''/>

    <z:row c0='2004' c1='057' c2='0' c3='2010' c4='671590' c5='2010' c6='AANENSON' c7='SHELLI' c8='A' BIRTHDATE='1992-07-01'

         GENDER='F' c11='5' c12='' c13='False' c14='True' c15='False' c16='False' c17='False' c18='False' c19='False'

         c20='I' c21='12' c22='7' ALPHAKEY='AANENSHE000' c24='057' c25='624000671590' c26='011' ADVISOR='0' c28='False'

         c29='R' c30='' c31=''/>

    <z:row c0='2004' c1='057' c2='0' c3='2010' c4='671590' c5='2010' c6='AANENSON' c7='SHELLI' c8='A' BIRTHDATE='1992-07-01'

         GENDER='F' c11='5' c12='' c13='False' c14='True' c15='False' c16='False' c17='False' c18='False' c19='False'

         c20='I' c21='12' c22='7' ALPHAKEY='AANENSHE000' c24='057' c25='624000671590' c26='011' ADVISOR='0' c28='False'

         c29='R' c30='' c31=''/>

    <z:row c0='2004' c1='057' c2='0' c3='2010' c4='671590' c5='2010' c6='AANENSON' c7='SHELLI' c8='A' BIRTHDATE='1992-07-01'

         GENDER='F' c11='5' c12='' c13='False' c14='True' c15='False' c16='False' c17='False' c18='False' c19='False'

         c20='I' c21='12' c22='7' ALPHAKEY='AANENSHE000' c24='057' c25='624000671590' c26='011' ADVISOR='0' c28='False'

         c29='R' c30='' c31=''/>

    <z:row c0='2004' c1='057' c2='0' c3='2010' c4='671590' c5='2010' c6='AANENSON' c7='SHELLI' c8='A' BIRTHDATE='1992-07-01'

         GENDER='F' c11='5' c12='' c13='False' c14='True' c15='False' c16='False' c17='False' c18='False' c19='False'

         c20='I' c21='12' c22='7' ALPHAKEY='AANENSHE000' c24='057' c25='624000671590' c26='011' ADVISOR='0' c28='False'

         c29='R' c30='' c31=''/>

【问题讨论】:

  • 老实说,我会尝试使用 SSIS 来读取 XML 数据。 BCP 是一种相当“黑盒”的东西——你把数据放进去,你把数据拿出来,你几乎无法控制(或反馈)它为什么以及如何实际工作。
  • 谢谢菲利普,但看到我下面的评论了吗?这个文件太大了。

标签: windows sql-server-2008 bcp utility


【解决方案1】:

您不能为此使用bcpbcp 可以为您提供的最好的方法是从带有逗号/制表符/等的文件中导入。分隔值。您在bcp 文档中看到的所有对 xml 的引用都指的是格式文件,而不是要导入的实际数据。

实际上有两种方法可以做到这一点。最简单的方法是为此使用 SQL Server 集成服务。 This 可以帮到你。

您也可以通过reading the xml file 进入 SQL 服务器来执行此操作。然后使用sp_xml_preparedocument 解析文件。最后使用OPENXMLin combination withINSERT导入数据。

【讨论】:

  • 你确定我不能为此使用 BCP 吗?是的,我想最终为此使用 SSIS,但是因为这个 XML 太大会导致内存不足错误,所以我最初想使用 BCP 将其导入临时表。我还能如何读取这个 XML 文件?您是否建议批量插入?我对 sp_xml_preparedocument 不熟悉——那也导入这个文件吗?
猜你喜欢
  • 2015-05-09
  • 2014-02-20
  • 2018-01-25
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
相关资源
最近更新 更多