如何使用批量插入在SQL Server 2005和BCP 9中保存分号分隔文件中的数据?

问题描述:

这是我的示例数据:如何使用批量插入在SQL Server 2005和BCP 9中保存分号分隔文件中的数据?

1;a;b;c;; 2;d;e;f;; 3;g;h;i;; 
4;j;k;l;; 5;m;n;o;; 
6;p;q;r;; 

这是我的样本格式文件(BCP 9):

9.0 
7 
1 SQLCHAR 0 0 "" 0 x Latin1_General_CI_AS 
2 SQLCHAR 0 0 ";" 2 i Latin1_General_CI_AS 
3 SQLCHAR 0 0 ";" 3 s Latin1_General_CI_AS 
4 SQLCHAR 0 0 ";" 4 t Latin1_General_CI_AS 
5 SQLCHAR 0 0 ";" 5 u Latin1_General_CI_AS 
6 SQLCHAR 0 0 ";" 6 v Latin1_General_CI_AS 
7 SQLCHAR 0 0 "\r\n" 0 x Latin1_General_CI_AS 

下面是表结构:

create table bcp 
(
    id int identity, 
    i int, 
    s varchar(2), 
    t varchar(2), 
    u varchar(2), 
    v varchar(2), 
    dte datetime default getdate() 
) 

问题是,当我用“\ r \ n”,只有1,4,6是保存不全,但是当我尝试使用“\ 0”时,只有第一条记录是保存。 我该如何解决这个问题?

另一个问题,分号后的空格是什么是rowterminator?

好的,我得到了这个为我工作。我用下面...

数据文件(根据你的例子)......

1;a;b;c;; 
2;d;e;f;; 
3;g;h;i;; 
4;j;k;l;; 
5;m;n;o;; 
6;p;q;r;; 

每行被终止与返回字符 - 不连续线在你的榜样

然后,使用的格式文件...

9.0 
7 
1 SQLCHAR 0 0 "" 0 x "" 
2 SQLCHAR 0 0 ";" 2 i Latin1_General_CI_AS 
3 SQLCHAR 0 0 ";" 3 s Latin1_General_CI_AS 
4 SQLCHAR 0 0 ";" 4 t Latin1_General_CI_AS 
5 SQLCHAR 0 0 ";" 5 u Latin1_General_CI_AS 
6 SQLCHAR 0 0 ";" 6 v Latin1_General_CI_AS 
7 SQLCHAR 0 0 "" 0 x "" 

,并得到了结果集...

1 1 a b c NULL 20/01/2009 10:17:51 
    2 2 d e f NULL 20/01/2009 10:17:51 
    3 3 g h i NULL 20/01/2009 10:17:51 
    4 4 j k l NULL 20/01/2009 10:17:51 
    5 5 m n o NULL 20/01/2009 10:17:51 
    6 6 p q r NULL 20/01/2009 10:17:51 

我相信这里的关键在于如何跳过标识字段的列和目标表中的默认值。有一个很好的MSDN文章here

asiaenforcer,我很抱歉离线周!

使用您的数据上面我想我知道了......

首先,我与你的目标表中的问题 - 语句“CREATE TABLE [DBO] .SansayRawInfo NOT NULL,”不正确的语法所以从看你以前的格式文件,我相信你错过了一列(RawDataId),所以我想出了为目标表...

CREATE TABLE [dbo].[SansayRawInfo](
    [RawDataId] [int] IDENTITY(1,1) NOT NULL, 
    [Sequence] [varchar](5) NULL, 
    [Version] [varchar](5) NULL, 
    [RecordType] [varchar](5) NULL, 
    [ConnectionType] [varchar](5) NULL, 
    [SessionID] [varchar](5) NULL, 
    [ReleaseCause] [varchar](5) NULL, 
    [StartTime] [varchar](5) NULL, 
    [AnswerTime] [varchar](5) NULL, 
    [ReleaseTOD] [varchar](5) NULL, 
    [WestofGMT] [varchar](5) NULL, 
    [RelCauseTxt] [varchar](5) NULL, 
    [RelCauseBin] [varchar](5) NULL, 
    [1strelease] [varchar](5) NULL, 
    [OrgTrunkId] [varchar](5) NULL, 
    [OrgProtocol] [varchar](5) NULL, 
    [OrgSrcNo] [varchar](5) NULL, 
    [OrgSrcHost] [varchar](5) NULL, 
    [OrgDestNo] [varchar](5) NULL, 
    [OrgDestHost] [varchar](5) NULL, 
    [OrgCallID] [varchar](5) NULL, 
    [OrgRemPayIPAdd] [varchar](5) NULL, 
    [OrgRemPayUDPAdd] [varchar](5) NULL, 
    [OrgLocPayIPAdd] [varchar](5) NULL, 
    [OrgLocPayUDPAdd] [varchar](5) NULL, 
    [OrgCodecList] [varchar](5) NULL, 
    [OrgIngrPck] [varchar](5) NULL, 
    [OrgEgrPck] [varchar](5) NULL, 
    [OrgIngrOct] [varchar](5) NULL, 
    [OrgEgrOct] [varchar](5) NULL, 
    [OrgIngrPckLoss] [varchar](5) NULL, 
    [OrgIngrDelay] [varchar](5) NULL, 
    [OrgIngrPckJitter] [varchar](5) NULL, 
    [TermTrunkId] [varchar](5) NULL, 
    [TermProtocol] [varchar](5) NULL, 
    [TermSrcNo] [varchar](5) NULL, 
    [TermSrcHost] [varchar](5) NULL, 
    [TermDestNo] [varchar](5) NULL, 
    [TermDestHost] [varchar](5) NULL, 
    [TermCallID] [varchar](5) NULL, 
    [TermRemPayIPAdd] [varchar](5) NULL, 
    [TermRemPayUDPAdd] [varchar](5) NULL, 
    [TermLocPayIPAdd] [varchar](5) NULL, 
    [TermLocPayUDPAdd] [varchar](5) NULL, 
    [TermCodecList] [varchar](5) NULL, 
    [TermIngrPck] [varchar](5) NULL, 
    [TermEgrPck] [varchar](5) NULL, 
    [TermIngrOct] [varchar](5) NULL, 
    [TermEgrOct] [varchar](5) NULL, 
    [TermIngrPckLoss] [varchar](5) NULL, 
    [TermIngrDelay] [varchar](5) NULL, 
    [TermIngrPckJitter] [varchar](5) NULL, 
    [FinRouteInd] [varchar](5) NULL, 
    [RoutingDigits] [varchar](5) NULL, 
    [CallDuration] [varchar](5) NULL, 
    [PostDialDelay] [varchar](5) NULL, 
    [RingTime] [varchar](5) NULL, 
    [Duration] [varchar](5) NULL, 
    [ConfID] [varchar](5) NULL, 
    [RPIDANI] [varchar](5) NULL, 
    [Status] [bit] NULL CONSTRAINT [DF__SansayRaw__Statu__7C8480AE] DEFAULT ((0)) 
) ON [PRIMARY] 

请注意

  • 在上面创建表脚本的所有 VARCHAR处理是5个(根据 您的样本数据不是你的格式 文件)长度虽然这不应该有 区别,因为所有的长度都 更大(且没有被规定 你的创建表脚本)。
  • 有我的剧本没有整理报表 因为 SQL_Latin1_General_CP1_CI_AS是我 数据库的默认,我只是照本宣科的SSMS表。再次,这不应该导致任何问题。

所以,我的下一步是要经过根据你提供的,我想出了下面的格式文件的样本原始数据格式文件...

9.0 
61 
1 SQLINT 0 0 "" 0 RawDataId "" 
2 SQLCHAR 0 20 ";" 2 Sequence SQL_Latin1_General_CP1_CI_AS 
3 SQLCHAR 0 30 ";" 3 Version SQL_Latin1_General_CP1_CI_AS 
4 SQLCHAR 0 44 ";" 4 RecordType SQL_Latin1_General_CP1_CI_AS 
5 SQLCHAR 0 26 ";" 5 ConnectionType SQL_Latin1_General_CP1_CI_AS 
6 SQLCHAR 0 42 ";" 6 SessionID SQL_Latin1_General_CP1_CI_AS 
7 SQLCHAR 0 14 ";" 7 ReleaseCause SQL_Latin1_General_CP1_CI_AS 
8 SQLCHAR 0 42 ";" 8 StartTime SQL_Latin1_General_CP1_CI_AS 
9 SQLCHAR 0 42 ";" 9 AnswerTime SQL_Latin1_General_CP1_CI_AS 
10 SQLCHAR 0 42 ";" 10 ReleaseTOD SQL_Latin1_General_CP1_CI_AS 
11 SQLCHAR 0 42 ";" 11 WestofGMT SQL_Latin1_General_CP1_CI_AS 
12 SQLCHAR 0 42 ";" 12 RelCauseTxt SQL_Latin1_General_CP1_CI_AS 
13 SQLCHAR 0 28 ";" 13 RelCauseBin SQL_Latin1_General_CP1_CI_AS 
14 SQLCHAR 0 22 ";" 14 1strelease SQL_Latin1_General_CP1_CI_AS 
15 SQLCHAR 0 64 ";" 15 OrgTrunkId SQL_Latin1_General_CP1_CI_AS 
16 SQLCHAR 0 16 ";" 16 OrgProtocol SQL_Latin1_General_CP1_CI_AS 
17 SQLCHAR 0 138 ";" 17 OrgSrcNo SQL_Latin1_General_CP1_CI_AS 
18 SQLCHAR 0 138 ";" 18 OrgSrcHost SQL_Latin1_General_CP1_CI_AS 
19 SQLCHAR 0 138 ";" 19 OrgDestNo SQL_Latin1_General_CP1_CI_AS 
20 SQLCHAR 0 138 ";" 20 OrgDestHost SQL_Latin1_General_CP1_CI_AS 
21 SQLCHAR 0 138 ";" 21 OrgCallID SQL_Latin1_General_CP1_CI_AS 
22 SQLCHAR 0 26 ";" 22 OrgRemPayIPAdd SQL_Latin1_General_CP1_CI_AS 
23 SQLCHAR 0 16 ";" 23 OrgRemPayUDPAdd SQL_Latin1_General_CP1_CI_AS 
24 SQLCHAR 0 26 ";" 24 OrgLocPayIPAdd SQL_Latin1_General_CP1_CI_AS 
25 SQLCHAR 0 16 ";" 25 OrgLocPayUDPAdd SQL_Latin1_General_CP1_CI_AS 
26 SQLCHAR 0 138 ";" 26 OrgCodecList SQL_Latin1_General_CP1_CI_AS 
27 SQLCHAR 0 20 ";" 27 OrgIngrPck SQL_Latin1_General_CP1_CI_AS 
28 SQLCHAR 0 20 ";" 28 OrgEgrPck SQL_Latin1_General_CP1_CI_AS 
29 SQLCHAR 0 20 ";" 29 OrgIngrOct SQL_Latin1_General_CP1_CI_AS 
30 SQLCHAR 0 20 ";" 30 OrgEgrOct SQL_Latin1_General_CP1_CI_AS 
31 SQLCHAR 0 20 ";" 31 OrgIngrPckLoss SQL_Latin1_General_CP1_CI_AS 
32 SQLCHAR 0 20 ";" 32 OrgIngrDelay SQL_Latin1_General_CP1_CI_AS 
33 SQLCHAR 0 20 ";" 33 OrgIngrPckJitter SQL_Latin1_General_CP1_CI_AS 
34 SQLCHAR 0 64 ";" 34 TermTrunkId SQL_Latin1_General_CP1_CI_AS 
35 SQLCHAR 0 16 ";" 35 TermProtocol SQL_Latin1_General_CP1_CI_AS 
36 SQLCHAR 0 138 ";" 36 TermSrcNo SQL_Latin1_General_CP1_CI_AS 
37 SQLCHAR 0 138 ";" 37 TermSrcHost SQL_Latin1_General_CP1_CI_AS 
38 SQLCHAR 0 138 ";" 38 TermDestNo SQL_Latin1_General_CP1_CI_AS 
39 SQLCHAR 0 138 ";" 39 TermDestHost SQL_Latin1_General_CP1_CI_AS 
40 SQLCHAR 0 138 ";" 40 TermCallID SQL_Latin1_General_CP1_CI_AS 
41 SQLCHAR 0 26 ";" 41 TermRemPayIPAdd SQL_Latin1_General_CP1_CI_AS 
42 SQLCHAR 0 16 ";" 42 TermRemPayUDPAdd SQL_Latin1_General_CP1_CI_AS 
43 SQLCHAR 0 26 ";" 43 TermLocPayIPAdd SQL_Latin1_General_CP1_CI_AS 
44 SQLCHAR 0 16 ";" 44 TermLocPayUDPAdd SQL_Latin1_General_CP1_CI_AS 
45 SQLCHAR 0 138 ";" 45 TermCodecList SQL_Latin1_General_CP1_CI_AS 
46 SQLCHAR 0 20 ";" 46 TermIngrPck SQL_Latin1_General_CP1_CI_AS 
47 SQLCHAR 0 20 ";" 47 TermEgrPck SQL_Latin1_General_CP1_CI_AS 
48 SQLCHAR 0 20 ";" 48 TermIngrOct SQL_Latin1_General_CP1_CI_AS 
49 SQLCHAR 0 20 ";" 49 TermEgrOct SQL_Latin1_General_CP1_CI_AS 
50 SQLCHAR 0 20 ";" 50 TermIngrPckLoss SQL_Latin1_General_CP1_CI_AS 
51 SQLCHAR 0 20 ";" 51 TermIngrDelay SQL_Latin1_General_CP1_CI_AS 
52 SQLCHAR 0 20 ";" 52 TermIngrPckJitter SQL_Latin1_General_CP1_CI_AS 
53 SQLCHAR 0 44 ";" 53 FinRouteInd SQL_Latin1_General_CP1_CI_AS 
54 SQLCHAR 0 74 ";" 54 RoutingDigits SQL_Latin1_General_CP1_CI_AS 
55 SQLCHAR 0 16 ";" 55 CallDuration SQL_Latin1_General_CP1_CI_AS 
56 SQLCHAR 0 16 ";" 56 PostDialDelay SQL_Latin1_General_CP1_CI_AS 
57 SQLCHAR 0 16 ";" 57 RingTime SQL_Latin1_General_CP1_CI_AS 
58 SQLCHAR 0 20 ";" 58 Duration SQL_Latin1_General_CP1_CI_AS 
59 SQLCHAR 0 46 ";" 59 ConfID SQL_Latin1_General_CP1_CI_AS 
60 SQLCHAR 0 74 ";" 60 RPIDANI SQL_Latin1_General_CP1_CI_AS 
61 SQLCHAR 0 0 " " 0 Status "" 

我认为关键位这里是在最后一行行分隔符的单一空间61 SQLCHAR 0 0“” 0状态“”

我然后跑下面的语句...

BULK INSERT dbo.SansayRawInfo 
FROM 'C:\MYPATH\data.txt' 
WITH (FORMATFILE = 'C:\MYPATH\fmt.fmt') 

并正确导入前7行。最后一行没有导入,因为在我的数据文件中没有后续的空格行终止符,因为在所有其他行上都有。当我在这个最后的空间添加数据文件时,我得到了全部8行。如果您的源数据是系统生成的,我希望它会打印出尾随空格 - 如果不是,则需要重新访问源数据,因为SQL Server要求它在整个集合中保持一致,并可能错过不一致的行。

在我的结果数据看起来像下面的结束......

8 1 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 a31 a32 a33 a34 a35 a36 a37 a38 a39 a40 a41 a42 a43 a44 a45 a46 a47 a48 a49 a50 a51 a52 a53 a54 a55 a56 a57 NULL 0 
9 2 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26 b27 b28 b29 b30 b31 b32 b33 b34 b35 b36 b37 b38 b39 b40 b41 b42 b43 b44 b45 b46 b47 b48 b49 b50 b51 b52 b53 b54 b55 b56 b57 NULL 0 
10 3 c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 c25 c26 c27 c28 c29 c30 c31 c32 c33 c34 c35 c36 c37 c38 c39 c40 c41 c42 c43 c44 c45 c46 c47 c48 c49 c50 c51 c52 c53 c54 c55 c56 c57 NULL 0 
11 4 d1 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11 d12 d13 d14 d15 d16 d17 d18 d19 d20 d21 d22 d23 d24 d25 d26 d27 d28 d29 d30 d31 d32 d33 d34 d35 d36 d37 d38 d39 d40 d41 d42 d43 d44 d45 d46 d47 d48 d49 d50 d51 d52 d53 d54 d55 d56 d57 NULL 0 
12 5 e1 e2 e3 e4 e5 e6 e7 e8 e9 e10 e11 e12 e13 e14 e15 e16 e17 e18 e19 e20 e21 e22 e23 e24 e25 e26 e27 e28 e29 e30 e31 e32 e33 e34 e35 e36 e37 e38 e39 e40 e41 e42 e43 e44 e45 e46 e47 e48 e49 e50 e51 e52 e53 e54 e55 e56 e57 NULL 0 
13 6 ef1 ef2 ef3 ef4 ef5 ef6 ef7 ef8 ef9 ef10 ef11 ef12 ef13 ef14 ef15 ef16 ef17 ef18 ef19 ef20 ef21 ef22 ef23 ef24 ef25 ef26 ef27 ef28 ef29 ef30 ef31 ef32 ef33 ef34 ef35 ef36 ef37 ef38 ef39 ef40 ef41 ef42 ef43 ef44 ef45 ef46 ef47 ef48 ef49 ef50 ef51 ef52 ef53 ef54 ef55 ef56 ef57 NULL 0 
14 7 g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 g11 g12 g13 g14 g15 g16 g17 g18 g19 g20 g21 g22 g23 g24 g25 g26 g27 g28 g29 g30 g31 g32 g33 g34 g35 g36 g37 g38 g39 g40 g41 g42 g43 g44 g45 g46 g47 g48 g49 g50 g51 g52 g53 g54 g55 g56 g57 NULL 0 
15 8 h1 h2 h3 h4 h5 h6 h7 h8 h9 h10 h11 h12 h13 h14 h15 h16 h17 h18 h19 h20 h21 h22 h23 h24 h25 h26 h27 h28 h29 h30 h31 h32 h33 h34 h35 h36 h37 h38 h39 h40 h41 h42 h43 h44 h45 h46 h47 h48 h49 h50 h51 h52 h53 h54 h55 h56 h57 NULL 0 

请注意

我RawDataId的8开始,因为该表之前被清除并重新填充。

希望这有些帮助,你不必重新访问你的源数据,因为这可能是正确的疼痛:)