Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9323773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:25:42+00:00 2026-06-19T04:25:42+00:00

I have a database with multiple questionnaires, each questionnaire has related (multiplechoice) questions, and

  • 0

I have a database with multiple questionnaires, each questionnaire has related (multiplechoice) questions, and each question has related answers.

I’m trying to create a stored procedure that will delete all questions and related answers with only the ID of the questionnaire.
My stored procedure looks like this:

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

the related questions are being deleted but the related answers not, so I’m doing something wrong in my loop, is there anybody out there who sees what it is?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T04:25:43+00:00Added an answer on June 19, 2026 at 4:25 am

    Your cursor is slightly odd, in that FETCH NEXT is usually the last statement in the loop (see the example from the documentation), so in fact the first value you fetch is immediately replaced by the second one. Adding some PRINT statements into the cursor to check the current value of @QID would show this.

    But the real answer is not to use a cursor at all, it’s unnecessary here:

    delete from dbo.answers where questionid in (
        select questionid from dbo.questions where EnqueteID = @EnqueteID
    )
    delete from dbo.questions where EnqueteID = @EnqueteID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple websites run on single database. My question related table Structure is
I have a database storing multiple columns and I am trying to create efficient
I have multiple database, each has table authentication. In each table I want to
I have a database with multiple schemas: Security, Trade, etc. Each shema has multiple
I have a MySQL database with multiple tables, each of which have the same
Currently I have a database with the following relationships: One Client has multiple Entities
I have MySQL database with multiple URL's. Each URL is displayed in one iframe,
I'm using Access 2010. I have a database with multiple tables, each containing different
I have built a database on multiple tables. One of these tables has a
I have a database in which there are multiple posts. Each post can be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.