sql中的Bulk 匯入txt文字

2022-03-31 07:13:04 字數 2601 閱讀 8634

通常,我們會對於乙個文字檔案資料匯入到資料庫中,不多說,上**。

首先,表結構如下.

其次,在我當前d盤中有個文字檔案名為2.txt的檔案。

在資料庫中,可以這樣通過一句**插入。

bulk

insert

t_demo

from

'd:\2.txt

'with

( fieldterminator='

,', rowterminator='

\n')

1) bulk insert:  為sql server 中乙個批量插入的操作

2)t_demo:   要插入的表

3)'d:\2.txt':   資料原始檔所在磁碟上的物理路徑

4)with 相當於一些插入的附屬條件,可以起這裡新增一些約束限制條件等

5) fieldterminator=』,』欄位的分割符為」,」一般的txt文字資料來源的字段分隔都為逗號,當然也可以為其他,比如」|」

6)rowterminator=』\n』各條資訊間的分割符為』\n』,一般的txt文字資料來源的資訊間的分割都是換行符,即一條資訊佔一行,也可以用其他格式如:」|\n」

在微軟官方給的詳細如下:

bulk

insert

[database_name . [ schema_name

] . | schema_name . ] [

table_name | view_name

]from

'data_file'[

with

( [ [ ,

] batchsize =

batch_size ]

[[ ,

]check_constraints ]

[[ ,

] codepage = ]

[[ ,

] datafiletype =

] [

[ ,

] 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 ( [

,...n

]) ]

[[ ,

] rows_per_batch =

rows_per_batch ]

[[ ,

] rowterminator =

'row_terminator

']

[[ ,

]tablock ]

[[ ,

] errorfile =

'file_name

']

)]

這裡我們主要看with中的一些引數:(部分)

1)       batchsize : 指定批處理中的行數

2)       check_constraints:啟用約束檢查,指定在大容量匯入操作期間,必須檢查所有對目標表或檢視的約束。若沒有 check_constraints 選項,則忽略所有 check 和 foreign key 約束,並在該操作後將表的約束標記為不可信。始終強制使用 unique、primary key 和 not null 約束。

3)       fire_triggers

指定將在大容量匯入操作期間執行目標表中定義的所有插入觸發器。如果為針對目標表的 insert 操作定義了觸發器,則每次完成批處理操作時均激發觸發器。

4)       keepnulls

指定空列在大容量匯入操作期間應保留 null 值,而不插入列的任何預設值

5)       order ( [ ,...n ] )

指定如何對資料檔案中的資料排序。如果根據表中的聚集索引(如果有)對要匯入的資料排序,則可提高大容量匯入的效能

6)       errorfile ='file_name'

指定用於收集格式有誤且不能轉換為 ole db 行集的行的檔案。這些行將按原樣從資料檔案複製到此錯誤檔案中。

參考:

TXT匯入資料到SQL

using system using system.collections.generic using system.linq using system.text using system.data.sqlclient using system.io namespace 03匯入資料 using s...

給Docker中的Mysql匯入sql檔案資料

新建立的mysql docker容器,想要給該mysql匯入sql檔案資料。docker cp mydata mytable.sql mysql mydata mytable.sql將sql檔案拷貝到docker容器中。docker exec it mysql bash root eb629da70...

C 實現文字TXT檔案匯入SQL

如果用程式就是將文字檔案的每行根據固定的分隔符用split函式分割,然後逐個加到引數中去.給個簡單的demo sql code create database data gouse data gocreate table custom 客戶名 varchar 20 varchar 10 住址 var...