简体   繁体   中英

Bulk insert in a specific column in SQL Server

I have a table dic that has 2 columns english and bangla . I want to insert data into only the bangla column using bulk insertion from a text file.

I tried which can insert into 2 column

BULK INSERT dic
FROM 'C:\Users\Imon-Bayazid\Desktop\wordddd\good one\test.txt'
WITH
(
   FIELDTERMINATOR = '\n',
   ROWTERMINATOR = '\n'
)  

But how can I do into one specific column???

You can map from fields in the source file to fields in the destination table using a format file .

I assume you know that bulk insert is for inserting new rows only, not for updates.

So you might have a format file like this:

<?xml version="1.0"?>
<BCPFORMAT 
     xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <RECORD>
  <FIELD ID="1" xsi:type="CharTerm" TERMINATOR="\n"/>
  ... other fields?
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1" NAME="bangla"/>
 </ROW>
</BCPFORMAT>

Any other columns in table 'dic' would need to be nullable or have default values.

You then specify the format file as an option of the BULK INSERT statement

BULK INSERT dic
FROM 'C:\Users\Imon-Bayazid\Desktop\wordddd\good one\test.txt'
WITH
(
   FIELDTERMINATOR = '\n',
   ROWTERMINATOR = '\n',
   FORMATFILE = 'path to my format file.xml'
) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM