简体   繁体   English

批量插入SQL Server 2005

[英]Bulk insert into SQL Server 2005

Bulk inserting 3 text files each containing 1 lac records into test1 table. 将3个文本文件批量插入到test1表中,每个文本文件包含1个lac记录。

Each of the 3 files has companycode and folio. 3个文件中的每个文件都有公司代码和作品集。 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. 如果test1表中已经存在compcode和folio,则必须使用文本文件中的特定记录更新表,否则将其插入。

But my query is taking lot of time. 但是我的查询要花很多时间。 test1 table has 70 columns test1表具有70列

Mmy logic: Mmy逻辑:

  1. import data in dummy table 在虚拟表中导入数据
  2. compare each row of dummy with test1 table 将虚拟行的每一行与test1表进行比较
  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? 由于记录是在lacs中进行的,因此要花费30分钟以上的时间。.如何改进查询?

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. 我假设您正在使用BULK INSERT将数据插入到临时表中,或者可以使用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. 即使您拥有if(exist)它也只会更新两个表中都存在的行。 So remove if else and write queries directly as shown below: 因此,删除if else并直接编写查询,如下所示:

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: 要插入在test1中不退出的记录,可以使用左连接,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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