首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查询占用太多

查询占用太多
EN

Stack Overflow用户
提问于 2013-04-09 06:34:29
回答 1查看 65关注 0票数 1

当我的条件返回结果时,我试图执行更新,问题是当我测试查询时,它永远不会完成。下面是查询;

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-09 06:54:58

你不需要循环。像这样的单个update查询就可以完成工作。

代码语言:javascript
复制
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,可能会有一些细微的变化。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15890232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档