0

将 3 个文本文件批量插入test1表中,每个文件包含 1 条 lac 记录。

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

但是我的查询需要很多时间。test1表有 70 列

妈咪逻辑:

  1. 在虚拟表中导入数据
  2. 将每一行 dummy 与 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 
    

由于记录在 lacs 中超过 30 分钟。我该如何改进查询?

4

1 回答 1

0

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

即使您已经if(exist)更新,它也只会更新两个表中都存在的行。所以删除 if else 并直接编写查询,如下所示:

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
于 2013-07-10T09:59:57.437 回答