SQL游标进入无限循环
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL游标进入无限循环相关的知识,希望对你有一定的参考价值。
此sql进入无限循环。
我找不到问题所在。我搜索了其他帖子,但是大多数帖子都错过了end标签之前的最后一个fetch语句。我敢肯定我看错了什么,但我不知道是什么。那我错过了什么吗?
下面是我的代码
DECLARE
@counter INT = 1042,
@overwrite_text NVARCHAR(250),
@sequence INT,
@is_correct BIT,
@score INT,
--@form_question_id INT,
@question_answer_id INT;
DECLARE cursor_form_answer CURSOR
FOR SELECT
[overwrite_text]
,[sequence]
,[is_correct]
,[score]
--,[form_question_id]
,[question_answer_id]
FROM [form_question_answer];
OPEN cursor_form_answer;
FETCH NEXT FROM cursor_form_answer INTO
@overwrite_text,
@sequence,
@is_correct,
@score,
--@form_question_id,
@question_answer_id;
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO [form_question_answer] (overwrite_text, sequence, is_correct, score, form_question_id, question_answer_id)
VALUES (@overwrite_text, @sequence, @is_correct, @score, @counter, @question_answer_id);
SET @counter = @counter + 1;
FETCH NEXT FROM cursor_form_answer INTO
@overwrite_text,
@sequence,
@is_correct,
@score,
--@form_question_id,
@question_answer_id;
END;
CLOSE cursor_form_answer;
DEALLOCATE cursor_form_answer;
答案
这里完全不需要CURSOR
。使用INSERT
只需SELECT
数据:
INSERT INTO [form_question_answer] (overwrite_text,
[sequence],
is_correct,
score,
form_question_id,
question_answer_id)
SELECT [overwrite_text],
[sequence],
[is_correct],
[score],
--,[form_question_id]
ROW_NUMBER() OVER (ORDER BY [form_question_id]),
[question_answer_id]
FROM [form_question_answer];
但是我必须说,[[真的从表form_question_answer
捕获数据,然后将其重新插入所述表中,这似乎很奇怪。
以上是关于SQL游标进入无限循环的主要内容,如果未能解决你的问题,请参考以下文章