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]
请注意
- 在上面的创建表脚本中,所有 varchars 的长度都是 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 Status ""
然后我运行了以下语句...
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 开始,因为该表之前已被清除并重新填充。
希望这会有所帮助,并且您不必重新访问源数据,因为这可能是正确的痛苦:)