首页 > 代码库 > 什么是 BULK INSERT

什么是 BULK INSERT

MSDN的解释:

在 SQL Server 中以用户指定的格式将数据文件导入到数据库表或视图中。

语法:

BULK INSERT    [ database_name . [ schema_name ] . | schema_name . ] [ table_name | view_name ]       FROM data_file      [ WITH     (    [ [ , ] BATCHSIZE = batch_size ]    [ [ , ] CHECK_CONSTRAINTS ]    [ [ , ] CODEPAGE = { ACP | OEM | RAW | code_page } ]    [ [ , ] DATAFILETYPE =       { char | native| widechar | widenative } ]    [ [ , ] FIELDTERMINATOR = field_terminator ]    [ [ , ] FIRSTROW = first_row ]    [ [ , ] FIRE_TRIGGERS ]    [ [ , ] FORMATFILE = format_file_path ]    [ [ , ] KEEPIDENTITY ]    [ [ , ] KEEPNULLS ]    [ [ , ] KILOBYTES_PER_BATCH = kilobytes_per_batch ]    [ [ , ] LASTROW = last_row ]    [ [ , ] MAXERRORS = max_errors ]    [ [ , ] ORDER ( { column [ ASC | DESC ] } [ ,...n ] ) ]    [ [ , ] ROWS_PER_BATCH = rows_per_batch ]    [ [ , ] ROWTERMINATOR = row_terminator ]    [ [ , ] TABLOCK ]    [ [ , ] ERRORFILE = file_name ]     )]

简单应用:

表结构如下:

CREATE TABLE [dbo].[Types] (    [Id]        INT          IDENTITY (1, 1) NOT NULL,    [TypeName]  VARCHAR (50) DEFAULT (‘‘) NULL,    [TypeValue] INT          DEFAULT ((0)) NULL,    PRIMARY KEY CLUSTERED ([Id] ASC));

要导入的文本文件(D:\a.txt)内容如下:

1,Add,12,Modify,23,Delete,4

导入命令:

BULK INSERT dbo.Types FROM D:a.txt WITH(FIELDTERMINATOR=,)

FIELDTERMINATOR : 表示字段之间的分隔符(默认值为 \t 制表符);

ROWTERMINATOR : 表示行之间的分隔符(默认值为 \r\n 换行符)。

其它参数请参考 MSDN 注解。