SQL Server's BULK INSERT statement can import local or remote data files into a database in batches, very quickly. The remote file must be shared before the file path should use the Universal Convention (UNC) name, which is the form of "\ \ server name or ip\ share name \ path \ file name."
* 1. Because BULK insert is often more convenient to bulk import data with format files, here's how the BCP tool exports format files.
BCP is a command-line utility provided by SQL Server that provides the ability to export, import, and format file export of data, and the syntax for exporting a format file is as follows:
SQL code
- BCP database name. The name of the user. Table name format nul--nul must exist here, for cases where data is not exported and imported
- -F output Format filename [-X]-C---x parameter specifies the output format file is in XML format (default non-XML format); The-c parameter specifies that the data is stored as a character and by default specifies ' \ t ' as the field spacer; ' \ n ' As the line delimiter
- [-t Field spacer] [-R line spacing symbol]---T and-R parameters are optional to overwrite the default delimiter specified by-C
- -T-Specifies that the database connection is trusted, that is, log on with Windows identity
* 2. Bulk Insert
Import the data file according to the format file, the syntax format is as follows:
SQL code
- Bulk Insert database name. User name. Table name
- From ' Data file path '
- With
- (
- formatfile = ' Format file path ',
- FirstRow = 2--Specifies the number of rows to start in the data file, which is 1 by default
- )
* 3. Openrorwset (BULK) function
Sometimes, you can use the OPENROWSET (BULK) function to more flexibly select the fields you want to insert into the original table or other tables, with the syntax in the following format:
SQL code
- 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
Of course, this function can also be used:
SQL code
- 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
Here is a complete example:
1) Create a database, a table, and populate the test data with the following script:
SQL code
- --Create a database
- CREATE DATABASE [Db_mgr]
- GO
- --Create a test table
- Use Db_mgr
- CREATE TABLE dbo. T_student (
- f_id [int] IDENTITY (*) is not NULL,
- F_code varchar (10),
- F_name varchar (100),
- F_memo nvarchar (500),
- F_memo2 ntext,
- PRIMARY KEY (f_id)
- )
- GO
- --Filling test data
- Insert into T_student (F_code, F_name, F_memo, F_memo2) Select
- ' code001 ', ' name001 ', ' memo001 ', ' remarks 001 ' UNION ALL Select
- ' code002 ', ' name002 ', ' memo002 ', ' remarks 002 ' UNION ALL Select
- ' code003 ', ' name003 ', ' memo003 ', ' remarks 003 ' UNION ALL Select
- ' code004 ', ' name004 ', ' memo004 ', ' remarks 004 ' UNION ALL Select
- ' code005 ', ' name005 ', ' memo005 ', ' remarks 005 ' UNION ALL Select
- ' code006 ', ' name006 ', ' memo006 ', ' remark 006 '
2) We can use the SQL Server master: The xp_cmdshell stored procedure passes CMD commands to the system, allowing you to enter BCP commands directly in the SQL Server query processor without switching to command mode execution. SQL Server has disabled the stored procedure by default for security purposes, and is opened by the following method:
SQL code
- --Open xp_cmdshell stored procedure (security hidden after opening)
- EXEC sp_configure ' show advanced options ', 1;
- RECONFIGURE; EXEC sp_configure ' xp_cmdshell ', 1;
- EXEC sp_configure ' show advanced options ', 0;
- RECONFIGURE;
3) Use BCP to export format files:
SQL code
- EXEC Master. xp_cmdshell ' BCP db_mgr.dbo.T_Student format nul-f c:/student_fmt.xml-x-c-t '
4) Export the data file using bcp:
SQL code
- 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--emptying data from tables
Note: In the actual use of the process, the data files can be generated by the program, such as Log records!
5) Bulk import of data files using the BULK INSERT statement:
SQL code
- BULK INSERT db_mgr.dbo.T_Student
- From ' C:/student.data '
- With
- (
- FormatFile = ' C:/student_fmt.xml '
- )
6) Examples of using OPENROWSET (BULK):
SQL code
- 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 table must already exist
- 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 table can not exist
Reference:
Use BULK INSERT or OPENROWSET (BULK ...) to import bulk data (especially in the section on security, which should be particularly important when importing remote files):
Http://msdn.microsoft.com/zh-cn/library/ms175915.aspx
To create a format file:
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 utility:
Http://msdn.microsoft.com/zh-cn/library/ms162802.aspx
SQL Server Bulk Import