我正在使用此嵌套遊標。 major_cursor中的我的更新語句不更新表。我知道所有循環都在工作,因爲打印語句顯示期望值,但edu_suffix_stage_test中沒有更新。我希望有人有一個建議!嵌套遊標,代碼不會更新遊標以外的表
對於每個id,有一個專業的數據行和獲得學位的年份。我需要將它們串起來。
例如REID 10013有兩個記錄兩個學位:
土木工程在'38, 和土木工程中'41。
我需要構建edusuffix,因此它等於「土木工程」38,土木工程「41」。 我最後需要一個edusuffix,在edu_suffix_stage_test中。
謝謝。
這裏是我的代碼:
DECLARE @reid_outside nvarchar(20), @major nvarchar(50), @classof nvarchar(5),
@edusuffix_inside varchar(80), @note nvarchar(50), @reid_inside nvarchar(20),
@edusuffix_outside varchar(80)
DECLARE education_cursor CURSOR FOR
SELECT reid, edusuffix
FROM edu_suffix_stage_test
where reid < 1005
--ORDER BY reid
OPEN education_cursor
FETCH NEXT FROM education_cursor
INTO @reid_outside, @edusuffix_outside
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @note = 'building edusuffix for '
PRINT @note
PRINT @reid_outside
-- Declare an inner cursor based
-- on reid from the outer cursor.
DECLARE major_cursor CURSOR FOR
SELECT v.reid,
v.EduMajor, v.EduClassOF
FROM re_education v
WHERE v.REID = @reid_outside -- Variable value from the outer cursor
--order by v.REID
FOR UPDATE of edusuffix
OPEN major_cursor
FETCH NEXT FROM major_cursor INTO @reid_inside, @major, @classof
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @edusuffix_inside = @major +' '+ @classof +', '
PRINT @edusuffix_inside
update edu_suffix_stage_test
set edusuffix = case when @edusuffix_outside = null then ''+ @edusuffix_inside
else @edusuffix_outside + ', '[email protected]_inside end
from edu_suffix_stage_test a
where -- a.reid = @reid_inside
current of education_cursor
Print ' updated edusuffix'
FETCH NEXT FROM major_cursor INTO @reid_inside, @major, @classof
END
CLOSE major_cursor
DEALLOCATE major_cursor
FETCH NEXT FROM education_cursor
INTO @reid_outside, @edusuffix_outside
END
CLOSE education_cursor
DEALLOCATE education_cursor
我從來沒有使用可更新的遊標,所以,我可能是錯的,但不應該'FOR UPDATE的edusuffix'成爲第一個(外部)遊標的一部分?此外,您正在更新內部循環('major_cursor'循環)中的'edu_suffix_stage_test',這意味着您可能會多次更新同一個'edu_suffix_stage_test'行(因爲條件是'where current of education_cursor'和'education_cursor '在內循環中不會移動) - 是通過設計嗎? –