Sql server批量复制问题

时间:2017-07-06 12:39:36

标签: bcp sqlbulkcopy

我有批量复制的棘手问题。我的批量复制似乎没有将值赋值给表中的正确字段。

我的数据文件是(这只是一个例子):

3,123123123125,AB005

在我的批量复制后,我希望:

HostId = 3
Barcode = 123123123125
Destination = AB005

但是:

HostId = 123123125
Barcode = AB005
Destination = NULL

我的格式文件是:

<?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="21"/>
  <FIELD ID="2" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="30"/>
  <FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="30"/>
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1" NAME="HostID" xsi:type="SQLBIGINT"/>
  <COLUMN SOURCE="2" NAME="Barcode" xsi:type="SQLNVARCHAR"/>
  <COLUMN SOURCE="3" NAME="Destination" xsi:type="SQLNVARCHAR"/>
 </ROW>
</BCPFORMAT>

我要将表格批量复制到:

CREATE TABLE [dbo].[tblBarcode2LogicalDestination](
    [RowId] [bigint] IDENTITY(1,1) NOT NULL,
    [HostId] [bigint] NOT NULL,
    [Barcode] [nchar](12) NULL,
    [Destination] [varchar](30) NULL,
    [Updated] [datetime] NOT NULL CONSTRAINT 
[DF_tblBarcode2LogicalDestination_Updated]  DEFAULT (getdate()),
    [InsertTime] [datetime] NOT NULL DEFAULT (getdate()),
    [Error] [ntext] NULL,
 CONSTRAINT [PK_tblBarcode2LogicalDestination] PRIMARY KEY CLUSTERED 
(
    [RowId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

要执行批量复制,我使用此过程(@InputFileName和@FormatFileName是上述文件):

DECLARE @bulk_cmd varchar(1000)
set @bulk_cmd = 'bulk insert tblBarcode2LogicalDestination
from '''+@InputFileName+'''                 
WITH (FORMATFILE = '''+@FormatFileName+''' )'
EXEC(@bulk_cmd)

由于

0 个答案:

没有答案