Is there a way that I can enable DELETE CASCADE OFF and then turn it back on during a transaction?

I have four tables: exam> goals and> objective> details of objective topics. I do not use DELETE CASCADE, because during normal operation I do not want to be able to delete exams if there are goals, etc.

I created this SQL to execute DELETE in a SQL Server 2012 stored procedure.

BEGIN
   DELETE ot
   FROM ObjectiveTopic ot
   INNER JOIN ObjectiveDetail od
    ON ot.ObjectiveDetailId = od.ObjectiveDetailId
   INNER JOIN Objective o
    ON od.ObjectiveId = o.ObjectiveId
   INNER JOIN Exam e
    ON o.ExamId = e.ExamId
   WHERE e.SubjectId = @SubjectId

   DELETE od
   FROM ObjectiveDetail od
   INNER JOIN Objective o
    ON od.ObjectiveId=o.ObjectiveId
   INNER JOIN Exam e
    On o.ExamId = e.ExamId
   Where e.SubjectId = @SubjectId;

   DELETE o
   FROM Objective o
   INNER JOIN Exam e
    ON o.ExamId = e.ExamId
   WHERE SubjectId = @SubjectId

   RETURN 0;
  END

This works, but not three deletions. Is there a way to enable DELETE CASCADE during a transaction, delete all targets for a specific @SubjectId, and then disable DELETE CASCADE again? If not, can I simplify this code in some other way or do I always need to do three deletions?

+4
source share
1

- . , , ( ).

, - , ON DELETE NO ACTION. , ON DELETE CASCADE.

, , , - , .

, .

, . - :):). :)

+1

All Articles