转:(总结) SQL Server Bulk Insert 批量数据导入

SQL Server的Bulk Insert语句可以将本地或远程的数据文件批量导入到数据库中,速度非常的快。远程文件必须共享才行,文件路径须使用通用约定(UNC)名称,即"\\服务器名或IP\共享名\路径\文件名"的形式。

* 1. 由于Bulk Insert通常配合格式化文件批量导入数据更方便,所以这里先介绍bcp工具导出格式化文件的方法。

bcp是SQL Server提供的命令行实用工具提供了数据的导出、导入、格式文件导出等功能,导出格式化文件的语法如下:

  1. bcp 数据库名.用户名.表名 format nul -- 这里的nul必须存在,用于不是导出和导入数据的情况下
  2. -f 输出的格式化文件名 [-x] -c  -- -x参数指定输出的格式文件为xml格式(默认非xml格式); -c参数指定数据存储方式为字符,并默认指定'\t'作为字段间隔符;'\n'作为行间隔符
  3. [-t 字段间隔符] [-r 行间隔符号]  -- -t与-r参数可选,用于覆盖-c指定的默认间隔符
  4. -T -- 指定数据库连接可信,即使用Windows身份登录
bcp 数据库名.用户名.表名 format nul -- 这里的nul必须存在,用于不是导出和导入数据的情况下
-f 输出的格式化文件名 [-x] -c -- -x参数指定输出的格式文件为xml格式(默认非xml格式); -c参数指定数据存储方式为字符,并默认指定'\t'作为字段间隔符;'\n'作为行间隔符
[-t 字段间隔符] [-r 行间隔符号] -- -t与-r参数可选,用于覆盖-c指定的默认间隔符
-T -- 指定数据库连接可信,即使用Windows身份登录

* 2. Bulk Insert

根据格式文件导入数据文件,语法格式如下:

  1. Bulk insert 数据库名.用户名.表名
  2. from '数据文件路径'
  3. with
  4. (
  5. formatfile = '格式文件路径',
  6. FirstRow = 2    --指定数据文件中开始的行数,默认是1
  7. )
Bulk insert 数据库名.用户名.表名
from '数据文件路径'
with
(
formatfile = '格式文件路径',
FirstRow = 2 --指定数据文件中开始的行数,默认是1
)

* 3. OPENRORWSET(BULK)函数

有时,使用OPENROWSET(BULK)函数可以更灵活地选取想要的字段插入到原表或者其他表中,其语法格式为:

  1. INSERT INTO to_table_name SELECT filed_name_list
  2. FROM OPENROWSET(BULK N'path_to_data_file', FORMATFILE=N'path_to_format_file') AS new_table_name
 INSERT INTO to_table_name SELECT filed_name_list
FROM OPENROWSET(BULK N'path_to_data_file', FORMATFILE=N'path_to_format_file') AS new_table_name

当然,该函数也可以这么使用:

  1. SELECT field_name_list INTO temp_table_name
  2. FROM OPENROWSET(BULK N'path_to_data_file', FORMATFILE=N'path_to_format_file') AS new_table_name
SELECT field_name_list INTO temp_table_name
FROM OPENROWSET(BULK N'path_to_data_file', FORMATFILE=N'path_to_format_file') AS new_table_name

下面举一个完整的例子:

1)创建数据库、表并填充测试数据,脚本如下:

  1. -- 创建数据库
  2. CREATE DATABASE [db_mgr]
  3. GO
  4. --创建测试表
  5. USE db_mgr
  6. CREATE TABLE dbo.T_Student(
  7. F_ID [int] IDENTITY(1,1) NOT NULL,
  8. F_Code varchar(10) ,
  9. F_Name varchar(100) ,
  10. F_Memo nvarchar(500) ,
  11. F_Memo2 ntext ,
  12. PRIMARY KEY  (F_ID)
  13. )
  14. GO
  15. --填充测试数据
  16. Insert Into T_Student(F_Code, F_Name, F_Memo, F_Memo2) select
  17. 'code001', 'name001', 'memo001', '备注001' union all select
  18. 'code002', 'name002', 'memo002', '备注002' union all select
  19. 'code003', 'name003', 'memo003', '备注003' union all select
  20. 'code004', 'name004', 'memo004', '备注004' union all select
  21. 'code005', 'name005', 'memo005', '备注005' union all select
  22. 'code006', 'name006', 'memo006', '备注006'
-- 创建数据库
CREATE DATABASE [db_mgr]
GO
--创建测试表
USE db_mgr
CREATE TABLE dbo.T_Student(
F_ID [int] IDENTITY(1,1) NOT NULL,
F_Code varchar(10) ,
F_Name varchar(100) ,
F_Memo nvarchar(500) ,
F_Memo2 ntext ,
PRIMARY KEY (F_ID)
)
GO --填充测试数据
Insert Into T_Student(F_Code, F_Name, F_Memo, F_Memo2) select
'code001', 'name001', 'memo001', '备注001' union all select
'code002', 'name002', 'memo002', '备注002' union all select
'code003', 'name003', 'memo003', '备注003' union all select
'code004', 'name004', 'memo004', '备注004' union all select
'code005', 'name005', 'memo005', '备注005' union all select
'code006', 'name006', 'memo006', '备注006'

2)我们可以使用SQL Server的master..xp_cmdshell存储过程将CMD的命令传给系统,这样就可以直接在SQL Server的查询处理器中直接输入bcp的命令,而不用切换到命令模式下执行。SQL Server 出于安全目的默认将该存储过程禁用了,开启方法如下:

  1. --开启xp_cmdshell存储过程(开启后有安全隐患)
  2. EXEC sp_configure 'show advanced options', 1;
  3. RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;
  4. EXEC sp_configure 'show advanced options', 0;
  5. RECONFIGURE;
--开启xp_cmdshell存储过程(开启后有安全隐患)
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;
EXEC sp_configure 'show advanced options', 0;
RECONFIGURE;

3)使用bcp导出格式文件:

  1. EXEC master..xp_cmdshell 'BCP db_mgr.dbo.T_Student format nul -f C:/student_fmt.xml -x -c -T'
EXEC master..xp_cmdshell 'BCP db_mgr.dbo.T_Student format nul -f C:/student_fmt.xml -x -c -T'

4)使用bcp导出数据文件:

  1. EXEC master..xp_cmdshell 'BCP db_mgr.dbo.T_Student out C:/student.data -f C:/student_fmt.xml -T'
  2. truncate table db_mgr.dbo.T_Student -- 将表中数据清空
EXEC master..xp_cmdshell 'BCP db_mgr.dbo.T_Student out C:/student.data -f C:/student_fmt.xml -T'
truncate table db_mgr.dbo.T_Student -- 将表中数据清空

注意:在实际使用过程中,数据文件可以由程序生成,如日志记录等!

5)使用Bulk Insert语句批量导入数据文件:

  1. BULK INSERT db_mgr.dbo.T_Student
  2. FROM 'C:/student.data'
  3. WITH
  4. (
  5. FORMATFILE = 'C:/student_fmt.xml'
  6. )
BULK INSERT db_mgr.dbo.T_Student
FROM 'C:/student.data'
WITH
(
FORMATFILE = 'C:/student_fmt.xml'
)

6)使用OPENROWSET(BULK)的例子:

  1. INSERT INTO db_mgr.dbo.T_Student(F_Code, F_Name) SELECT F_Code, F_Name
  2. FROM OPENROWSET(BULK N'C:/student_c.data', FORMATFILE=N'C:/student_fmt_c.xml') AS new_table_name -- T_Student表必须已存在
  3. SELECT F_Code, F_Name INTO db_mgr.dbo.tt
  4. FROM OPENROWSET(BULK N'C:/student_c.data', FORMATFILE=N'C:/student_fmt_c.xml') AS new_table_name -- tt表可以不存在
INSERT INTO db_mgr.dbo.T_Student(F_Code, F_Name) SELECT F_Code, F_Name
FROM OPENROWSET(BULK N'C:/student_c.data', FORMATFILE=N'C:/student_fmt_c.xml') AS new_table_name -- T_Student表必须已存在 SELECT F_Code, F_Name INTO db_mgr.dbo.tt
FROM OPENROWSET(BULK N'C:/student_c.data', FORMATFILE=N'C:/student_fmt_c.xml') AS new_table_name -- tt表可以不存在

参考:

使用 BULK INSERT 或 OPENROWSET(BULK...) 导入大容量数据(尤其是关于安全的那部分,导入远程文件时应特别注意):

http://msdn.microsoft.com/zh-cn/library/ms175915.aspx

创建格式化文件:

http://msdn.microsoft.com/zh-cn/library/ms191516.aspx

OPENROWSET (Transact-SQL):

http://msdn.microsoft.com/zh-cn/library/ms190312.aspx

BULK INSERT (Transact-SQL):

http://msdn.microsoft.com/zh-cn/library/ms188365.aspx

bcp 实用工具:

http://msdn.microsoft.com/zh-cn/library/ms162802.aspx

BCP的更多相关文章

  1. bcp 命令实例

    set sql_flow="select Id,',',ApplierName,',',FlowStatus,',',IsApproved,',',CreateTime from *** w ...

  2. sqlserver中BCP命令导入导出

    个人自用导出文本文件命令: bcp [xxDB].[dbo].[xx_tb_name] out d:\temp\xxx.txt -c -t "\t" -T bcp是SQL Serv ...

  3. BCP 数据的导入和导出

    BCP 命令的参数很多,使用 -h 查看帮助信息,注意:参数是区分大小写的 使用BCP命令导出和导入数据常用的参数如下 bcp {[[database_name.][schema_name]].{ta ...

  4. BCP笔记整理(二)

    BCP的基础用法可以参考上一篇:http://www.cnblogs.com/Gin-23333/p/5489889.html 这篇是补充一些有可能会用到,但是出场几率并不算大的几个参数 1.首先是 ...

  5. 笔记整理之BCP

    很多时候,需要批量的导数据,可能大家想到的第一反应就是右键数据库->任务->导入导出数据.但是其实微软自身提供的大容量导入导出工具,有bcp, bulkinsert 之类的也是很好用.今天 ...

  6. BCP导出导入大容量数据实践

    前言 SQL SERVER提供多种不同的数据导出导入的工具,也可以编写SQL脚本,使用存储过程,生成所需的数据文件,甚至可以生成包含SQL语句和数据的脚本文件.各有优缺点,以适用不同的需求.下面介绍大 ...

  7. BCP 导出导入数据(SQL Server)

    BCP指令工具可通过安装SQL Server获得. 1. 根据现有的数据库生成表的format文件(导入导出数据的时候需要) bcp db_test.dbo.Table1 format nul -c ...

  8. 使用BCP导出导入数据

    bcp 实用工具可以在 Microsoft SQL Server 实例和用户指定格式的数据文件间大容量复制数据. 使用 bcp 实用工具可以将大量新行导入 SQL Server 表,或将表数据导出到数 ...

  9. Sybase 数据库bcp out备份重要表数据

    bcp相当于逻辑备份,bcp out导出的文件,bcp in可以导回去. 环境:RHEL 5.5 + Sybase客户端软件 需求:在客户端(Linux)备份服务端(HP-UX)重要配置表数据 1.b ...

随机推荐

  1. PHP代码实用片段

    一.黑名单过滤 function is_spam($text, $file, $split = ':', $regex = false){ $handle = fopen($file, 'rb'); ...

  2. KMP算法的详细解释及实现

    这是我自己学习算法时有关KMP的学习笔记,代码注释的十分的详细,分享给大家,希望对大家有所帮助 在介绍KMP算法之前, 先来介绍一下朴素模式匹配算法: 朴素模式匹配算法: 假设要从主串S=”goodg ...

  3. reactjs

    摘自阮一峰博客:http://www.ruanyifeng.com/blog/2015/03/react.html 现在最热门的前端框架,毫无疑问是 React . 上周,基于 React 的 Rea ...

  4. outscan 一键批量 get struct2 devMode (CNVD-2016-04656)

    之前写的一个玩意 下载地址:http://pan.baidu.com/s/1i5jmEwP 密码:v8v3 一键批量 get struct2 devMode 支持百度.google(google有访问 ...

  5. MFC双缓冲绘图(2015.09.24)

    问题引入: 最近在尝试编写贪吃蛇游戏时遇到这么一个问题:当系统以较快频率向窗口发送WM_PAINT消息时,调用OnPaint()函数在窗口中绘制图形就会发生闪烁现象. 问题分析: 当我们把绘图过程放在 ...

  6. tomcat7 Could not load the Tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config. The configuration may be corrupt or incomplete

    参考连接: http://lucasterdev.altervista.org/wordpress/2012/05/12/could-not-load-the-tomcat-server-config ...

  7. linux下安装安装pcre-8.32

    linux下安装安装pcre-8.32 ./configure --prefix=/usr/local/pcre 出现以下错误 configure: error: You need a C++ com ...

  8. easycwmp的编译

    原创作品,转载请注明出处,严禁非法转载. copyright:weishusheng   2015.3.18 email:642613208@qq.com 注:此处的编译指的是直接用系统自带的gcc编 ...

  9. Character类的2个定义大小写方法以及charAt(int index)方法

    API文档charAt(int index)是这样定义的: charAt(char index):Returns the char value at the specified index.在指定的索 ...

  10. 在 Web 项目中应用 Apache Shiro

    Apache Shiro 是功能强大并且容易集成的开源权限框架,它能够完成认证.授权.加密.会话管理等功能.认证和授权为权限控制的核心,简单来说,"认证"就是证明你是谁? Web ...