将包含1个lac记录的3个文本文件批量插入test1
表。
3个文件中的每一个都有公司代码和作品集。如果test1
表中已经存在compcode和folio,我必须使用文本文件中的特定记录更新表,否则插入它。
但我的查询花了很多时间。 test1
表有70列
Mmy逻辑:
if exists ( select * from #dummy , test1 where condition )
begin
update test1
set col = (#dummy.col)..
inner join #dummy on (condition)
end
else insert
由于记录在lacs中超过30分钟。我可以改进查询吗?
答案 0 :(得分: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