希望你一切都好。我有一些問題在SQL Server查詢。使用光標更新
我想更新記錄在一個表中有534,000行。如果我創建一個更新所有記錄的while循環,那麼它需要時間。
然後我應該創建一個遊標更新記錄。在此之後,我使用Fetch創建了更新的遊標。這個遊標在10,000行中快速完成更新過程,但是當我使用超過30,000行的表時,需要5分鐘才能執行查詢。我不知道是什麼問題。
其我的代碼
DECLARE @RNo INT --Declaring the Variable @id
DECLARE @id INT --Declaring the Variable @id
set @RNo=1
DECLARE @MyCursor CURSOR -- Declaring the Cursor
SET @MyCursor = CURSOR --Assigning the cursor
FOR
SELECT Col1 FROM MyTable --Query related to Cursor
for update of Col2
OPEN @MyCursor -- Opening the Created cursor
FETCH NEXT FROM @MyCursor --Retrieving the record one by one
INTO @id --Assigning the value in declared Variable
WHILE @@FETCH_STATUS = 0
BEGIN
update MyTable
set [email protected]
where current of @MyCursor
set @[email protected]+1
print @id
FETCH NEXT
FROM @MyCursor INTO @id
END
CLOSE @MyCursor -- Closing the Cursor
DEALLOCATE @MyCursor -- Deallocating the Cursor Memory
注:我的查詢執行與正確的數據,但問題是,它需要5分鐘再予我只是想在10秒內執行這個查詢。
由於提前
**擺脫光標**那是你的問題.... ** ** RBAR加工 - !**行到痛苦的排**。在C#中很好,在SQL中非常糟糕......根本不需要,這只是一個不必要的放緩.....只是一個'UPDATE'語句可以工作,我相信... –