BCP实用程序-省略导入文件中的默认值

时间:2018-08-21 18:16:41

标签: sql-server bcp

我有一张桌子:

CREATE TABLE [dbo].[EXTRACT](
    ID int IDENTITY,
    CREATE_DTTM datetime default GETDATE() NOT NULL,
    CONTRACT_ID varchar(30) NOT NULL,
    ACCOUNT_NUMBER varchar(30) NOT NULL,
    EXCEPTION_REASON varchar(max),
    PRIMARY KEY(ID ASC)
);

还有一个仅提供第3、4和5列的源文件。以下是我的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="," MAX_LENGTH="12"/>
  <FIELD ID="2" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="24"/>
  <FIELD ID="3" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="30" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="4" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="30" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="5" xsi:type="CharTerm" TERMINATOR="\r\n" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1" NAME="ID" xsi:type="SQLINT"/>
  <COLUMN SOURCE="2" NAME="CREATE_DTTM" xsi:type="SQLDATETIME"/>
  <COLUMN SOURCE="3" NAME="CONTRACT_ID" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="4" NAME="ACCOUNT_NUMBER" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="5" NAME="EXCEPTION_REASON" xsi:type="SQLVARYCHAR"/>
 </ROW>
</BCPFORMAT>

可用于此输入文件:

,,contract_2,account_2,exception_reason_2

但是可以省略输入文件中的前两个字段,使其像这样:

contract_2,account_2,exception_reason_2

1 个答案:

答案 0 :(得分:0)

From the docs,从格式文件中删除您不想填充的column行,并相应地对字段和源重新编号。

<?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="," MAX_LENGTH="30" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="2" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="30" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\r\n" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1" NAME="CONTRACT_ID" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="2" NAME="ACCOUNT_NUMBER" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="3" NAME="EXCEPTION_REASON" xsi:type="SQLVARYCHAR"/>
 </ROW>
</BCPFORMAT>

话虽如此,最安全,最干净的方法是将数据批量导入到与源文件形状相同的登台表中,然后调用将数据从登台表移至最终目的地表。