我有一個使用遊標的sql程序,但它確實很慢。我期待提高利用基於集的操作或這樣的事情的速度,但我不知道如何將其應用到這個具體程序:這個SQL遊標的改進 - 它真的很慢!
declare @isMulti bit
declare @QuestionID int
declare db_cursor cursor FAST_FORWARD for
select distinct QuestionID
from tblQuestions (nolock)
where [email protected]
open db_cursor
fetch next from db_cursor into @QuestionID
while @@FETCH_STATUS = 0
begin
--check if @isMulti is true or not for the current question
if(@isMulti=1)
begin
update tblAnswers
set col1 = 1, col2 = 1, col3 = (select count(*) from tblAnswers where [email protected] and [email protected])
end
else if(@isMulti=0)
begin
update tblAnswers
set col1 = AnswerID, col2 = 1, col3 = (select LEN(count(*)) from tblAnswers where [email protected] and [email protected])
end
fetch next from db_cursor into @QuestionID
end
close db_cursor
deallocate db_cursor
任何幫助的感謝提供!
你在用這個程序試着做什麼?也許一個更新查詢就足夠了。 – vdrmrt 2010-11-26 10:50:40
我在檢查一個問題是否是多個問題,然後根據這些信息更新問題答案。問題表不包含指定它是否爲多的列,我必須調用光標中當前問題標識的函數來檢查。 – Bob 2010-11-26 11:26:47
多重更新之間的差異是多少? – vdrmrt 2010-11-26 11:51:11