我编写了这个SQL Server 2008代码,用于在临时表中的每个值上调用存储过程。不幸的是,临时表中有超过660K的值,因此执行它会导致“内存不足”和“内存不足”。例外。有没有办法在不使用游标的情况下执行相同的任务?:
DECLARE @cons_to_delete uniqueidentifier
DECLARE @DeleteCursor CURSOR
SET @DeleteCursor = CURSOR FOR
SELECT consumer_uuid FROM tmp_UuidsOfClientsToDELETE
OPEN @DeleteCursor
FETCH NEXT From @DeleteCursor
INTO @cons_to_delete
WHILE @@FETCH_STATUS=0
BEGIN
exec sp_DeleteConsumer @cons_to_delete
FETCH NEXT FROM @DeleteCursor
END
CLOSE @DeleteCursor
DEALLOCATE @DeleteCursor
感谢您的帮助。
答案 0 :(得分:1)
如果Management Studio is giving you a Out of Memory Exception,则可能是因为您的存储过程正在输出内容并向客户端发送过多数据。我不认为是光标导致客户端出现错误消息。
虽然回答你的问题...为了消除对游标的需要,需要重写存储过程,以便它使用基于集合的逻辑。例如,如果您的程序sp_DeleteConsumer
看起来像这样:
delete ConsumerTable
where ConsumerID = @cons_to_delete
您可以将ConsumerTable
直接加入临时表或传递表值参数并将语句更改为:
delete ConsumerTable
from ConsumerTable
join tmp_UuidsOfClientsToDELETE
on ConsumerTable.ConsumerID = tmp_UuidsOfClientsToDELETE.consumer_uuid