• I have already tried to use 'truncate table' but it raises an error if there are any foreign key constraints. I would have to drop them before truncating table unless I find some way to disable and enable them after cleaning the database.

    By the way this script will not work if relationships are guarded by triggers (some old database may still use this solution).

    And You are right: I should not have used cursors.