簡體   English   中英

查詢占用過多

[英]Query taking too much

我試圖在條件返回結果時執行更新,問題是當我測試查詢時它永遠不會完成。 這是查詢;

While(select COUNT(*) from Agreement as agr where agr.Id in (
  select toa.Id from Agreement_TemporaryOnceAgreement as toa where toa.Executed =1)
and agr.EndingDate is null) > 0
begin
DECLARE @AgreementID int;
SET @AgreementID = 
(
select top 1 agr.id from Agreement as agr where agr.Id in (
  select toa.Id from Agreement_TemporaryOnceAgreement as toa where toa.Executed =1)
and agr.EndingDate is null
)
update Agreement SET EndingDate = (
  select tado.Date from TemporaryAgreementsDateOfExecution tado
    where tado.AgreementId = CAST(@AgreementID AS INT))
where Agreement.Id = CAST(@AgreementID AS INT);
end;

您不需要循環。 與此類似的單個更新查詢應該可以完成工作。

update a
set EndingDate = tado.date
from Agreement a join TemporaryAgreementsDateOfExecution tado
on a.AgreementId = tado.AgreementId

join Agreement_TemporaryOnceAgreement toa
on a.Id = toa.id

where EndingDate is null
and toa.Executed = 1

根據您使用的RDBMS,可能會有細微的變化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM