如何解决如何将带有游标的SQL语句转换为基于set?
我有一个带有以下游标的SQL语句:
declare @sId uniqueidentifier
declare @aId uniqueidentifier
declare @cId uniqueidentifier
declare @iId uniqueidentifier
declare @lDate datetime
declare test_cursor cursor for
select jb.actId,js.smId,jb.CustId,jb.Id,jb.DateTime
from Job jb
left join SM js on js.keyvalue = jb.Id
where jb.DateTime < DATEADD(month,-12,getdate())
open test_cursor
fetch next from test_cursor into @Id,@sId,@cId,@iId,@lDate
while @@fetch_status = 0
begin
delete from Logs
where actId = @aId
update SM
set Status = 88
where smId = @sId
insert into LAC
values (NEWID(),@lDate,getdate())
fetch next from test_cursor into @aId,@lDate
end
close test_cursor
deallocate test_cursor
如何将其更改为使用基于集合的方法,这样会更快?
解决方法
遵循这些思路可能会更快。
delete from Logs where actId =@aId;
update SM set Status = 88 where smId in(
js.smId from Job jb
left join SM js on js.keyvalue = jb.Id
where jb.DateTime < DATEADD(month,-12,getdate());
insert into LAC (a,b,c,d,e)
select newID(),jb.Id,jb.CustId,@lDate,getdate() from Job jb
left join SM js on js.keyvalue = jb.Id
where jb.DateTime < DATEADD(month,getdate());
您可以在临时表中进行初始选择,并在担心一致性或性能时使用它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。