如何循环 select,调用存储过程并处理结果?
How can I loop through a select, call a stored procedure and process the results?
我有一个select:
SELECT AnswerGridResponses,
QuestionUId,
UserTestQuestionId
FROM UserTest,
UserTestQuestion
WHERE UserTest.UserTestId = @UserTestId
AND UserTest.UserTestId = UserTestQuestion.UserTestId
以及我需要调用的程序:
EXEC sp_get_correct_responses @QuestionUId, @AnswerGridCorrect output
有人可以举例说明如何在存储过程中调用存储过程并传入@QuestionUId。在它通过后我需要做更多的处理,但我想如果我知道如何循环那么这将给我我需要的一切。
使用Cursor
来做到这一点。
DECLARE CUR CURSOR STATIC FOR
SELECT DISTINCT QuestionUId
FROM UserTest
INNER JOIN UserTestQuestion
ON UserTest.UserTestId = UserTestQuestion.UserTestId
WHERE UserTest.UserTestId = @UserTestId
OPEN vendor_cursor
FETCH NEXT FROM CUR INTO @QuestionUId
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC Sp_get_correct_responses
@QuestionUId,
@AnswerGridCorrect output
FETCH NEXT FROM CUR INTO @QuestionUId
END
CLOSE CUR;
DEALLOCATE CUR;
注意:游标可能导致性能不佳。尝试将 RBAR 操作更改为基于集合的方法
我有一个select:
SELECT AnswerGridResponses,
QuestionUId,
UserTestQuestionId
FROM UserTest,
UserTestQuestion
WHERE UserTest.UserTestId = @UserTestId
AND UserTest.UserTestId = UserTestQuestion.UserTestId
以及我需要调用的程序:
EXEC sp_get_correct_responses @QuestionUId, @AnswerGridCorrect output
有人可以举例说明如何在存储过程中调用存储过程并传入@QuestionUId。在它通过后我需要做更多的处理,但我想如果我知道如何循环那么这将给我我需要的一切。
使用Cursor
来做到这一点。
DECLARE CUR CURSOR STATIC FOR
SELECT DISTINCT QuestionUId
FROM UserTest
INNER JOIN UserTestQuestion
ON UserTest.UserTestId = UserTestQuestion.UserTestId
WHERE UserTest.UserTestId = @UserTestId
OPEN vendor_cursor
FETCH NEXT FROM CUR INTO @QuestionUId
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC Sp_get_correct_responses
@QuestionUId,
@AnswerGridCorrect output
FETCH NEXT FROM CUR INTO @QuestionUId
END
CLOSE CUR;
DEALLOCATE CUR;
注意:游标可能导致性能不佳。尝试将 RBAR 操作更改为基于集合的方法