日期:2014-05-19  浏览次数:20721 次

bulk insert 简单问题
表结构:
CREATE   TABLE   [dbo].[实时通讯汇总显示]   (
[矿名]   [char]   (20)     NULL   ,
[状态]   [int]   NULL   ,
[时间]   [datetime]   NULL   ,
[网络状态]   [int]   NULL   ,
[局名]   [varchar]   (20)     NULL   ,
)   ON   [PRIMARY]
GO
数据   如下:
'东周窑煤矿 ', '1 ', '2006-12-23   16:17:12 ', '1 '
'青瓷窑 ', '0 ', '2006-12-23   16:17:12 ', '0 '
'杏儿沟 ', '1 ', '2006-12-23   16:17:12 ', '0 '
'姜家湾 ', '1 ', '2006-12-23   16:17:12 ', '1 '
'马口矿 ', '1 ', '2006-12-23   16:17:12 ', '0 '
'焦矿 ', '1 ', '2006-12-23   16:17:12 ', '0 '

------解决方案--------------------
-- 示例

CREATE TABLE [dbo].[实时通讯汇总显示] (
[矿名] [char] (20) NULL ,
[状态] [int] NULL ,
[时间] [datetime] NULL ,
[网络状态] [int] NULL ,
[局名] [varchar] (20) NULL ,
) ON [PRIMARY]
GO

CREATE TABLE #(
[矿名] [char] (20) NULL ,
[状态] [int] NULL ,
[时间] [datetime] NULL ,
[网络状态] [int] NULL
)

BULK INSERT #
FROM 'c:\a.txt '
WITH(
FIELDTERMINATOR = ' ' ', ' ' ',
ROWTERMINATOR = ' ' '\n ')
INSERT [dbo].[实时通讯汇总显示](
[矿名], [状态], [时间], [网络状态])
SELECT STUFF([矿名], 1, 1, ' '), [状态], [时间], [网络状态]
FROM #
DROP TABLE #
SELECT * FROM [dbo].[实时通讯汇总显示]
GO

DROP TABLE [dbo].[实时通讯汇总显示]