简体   繁体   中英

Bulk insert into SQL Server 2005

Bulk inserting 3 text files each containing 1 lac records into test1 table.

Each of the 3 files has companycode and folio. If compcode and folio already exist in the test1 table, I have to update the table with that particular record from text file else insert it.

But my query is taking lot of time. test1 table has 70 columns

Mmy logic:

  1. import data in dummy table
  2. compare each row of dummy with test1 table
  3.  if exists ( select * from #dummy , test1 where condition ) begin update test1 set col = (#dummy.col).. inner join #dummy on (condition) end 
  4.   else insert 

Since the records are in lacs more than 30 min are taken..how can I improve the query?

I assume you're using BULK INSERT to insert data in to temporary table or you could import it using Import Wizard in SQL Server. After that you could use below queries.

Even though you've if(exist) it will update only rows that exists in both the table. So remove if else and write queries directly as shown below:

update test1  
set col = (#dummy.col)..
from test1
inner join #dummy on (test1.companycode =#dummy.companycode and test1.folio = #dummy.companycode)

For inserting records that doesn't exits in test1, you could use left join as shown below:

Insert into test1
select column names 
from 
#dummy left join test1
on test1.companycode =#dummy.companycode and test1.folio = #dummy.companycode
where test1.companycode is null
and test1.folio is null

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