2013-07-10 204 views
0

批量插入3个文本文件,每个文本文件包含1个lac记录到test1表中。批量插入到SQL Server 2005中

3个文件中的每一个都有公司代码和作品集。如果test1表中已经存在compcode和folio,那么我必须使用文本文件中的特定记录更新表,否则将其插入。

但我的查询花了很多时间。 test1表具有70列

MMY逻辑:

  1. 在虚设表
  2. 导入数据比较虚设的每一行与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 
    

由于记录是在超过30分钟lacs ..如何我可以证明查询?

+0

过得好比较虚拟表的行与test1表? –

+0

condition =#dummy.companycode +#dummy.folio = test1.companycode + test1.folio –

回答

0

我假设您使用BULK INSERT将数据插入到临时表中,或者您可以使用SQL Server中的导入向导导入它。之后,您可以使用下面的查询。

尽管你已经有if(exist)它只会更新表中存在的行。因此,如果删除和其他如下图所示,直接编写查询:如下图所示

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

,用来插入不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 
+0

nil我会测试解决方案,并让你知道abt的时间也.. –

+0

无与它相同的时间,我导入第一个文件它的插入速度很快,但第二个文件有一万条记录占用了无限的分钟,我在45分钟后取消了操作。因此,这个更新还有其他任何逻辑插入 –