我有一个包含多个问卷的数据库,每个问卷都有相关的(多项选择)问题,每个问题都有相关的答案。
我正在尝试创建一个存储过程,该过程将删除所有问题和相关答案,仅包含调查问卷的 ID。我的存储过程如下所示:
ALTER PROCEDURE [dbo].[DeleteQuestionnnaire]
@EnqueteID bigint
AS
BEGIN
SET NOCOUNT ON
DECLARE @QID bigint
DECLARE questionCursor CURSOR FOR
SELECT ID FROM questions WHERE EnqueteID = @EnqueteID
OPEN questionCursor;
FETCH questionCursor INTO @QID
WHILE(@@fetch_status=0)
BEGIN
-- this is not working correct, the related answers are NOT being deleted
FETCH questionCursor INTO @QID
DELETE FROM answers WHERE QuestionID=@QID
END
CLOSE questionCursor
-- this works fine, the questions are being deleted
DELETE FROM questions WHERE EnqueteID=@EnqueteID
END
相关问题正在被删除,但相关答案没有,所以我在循环中做错了什么,有没有人看到它是什么?
提前致谢