2013-04-08 107 views
1

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

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; 
+3

您正在使用哪種rdbms? – Barranka 2013-04-08 22:35:36

回答

0

您不需要循環。類似這樣的單個更新查詢應該完成工作。

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,可能會有輕微的變化。