2013-04-08 2 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

답변

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에 따라 약간의 차이가있을 수 있습니다.

관련 문제