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 760437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:43:04+00:00 2026-05-14T15:43:04+00:00

Is it possible via script/tool to generate authomatically many delete statements based on the

  • 0

Is it possible via script/tool to generate authomatically many delete statements based on the tables fk relations, using Oracle PL/SQL?

In example: I have the table: CHICKEN (CHICKEN_CODE NUMBER) and there are 30 tables with fk references to its CHICKEN_CODE that I need to delete; there are also other 150 tables foreign-key-linked to that 30 tables that I need to delete first.

Is there some tool/script PL/SQL that I can run in order to generate all the necessary delete statements based on the FK relations for me?

(by the way, I know about cascade delete on the relations, but please pay attention: I CAN’T USE IT IN MY PRODUCTION DATABASE, because it’s dangerous!)

I’m using Oracle DataBase 10G R2.

Please pay attention to this:

Generate Delete Statement From Foreign Key Relationships in SQL 2008?

Another user has just written it in SQL SERVER 2008, anyone is able to convert to Oracle 10G PL/SQL?
I am not able to… 🙁

Please assume that V_CHICKEN and V_NATION are the criteria to select the CHICKEN to delete from the root table: the condition is: “where COD_CHICKEN = V_CHICKEN AND COD_NATION = V_NATION” on the root table.

  • 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-05-14T15:43:04+00:00Added an answer on May 14, 2026 at 3:43 pm

    (My first answer became too long and difficult to edit, and it got Community Wikified, which is really annoying. Here is the latest version of the script.)

    This script attempts to perform a cascading delete through recursion. It should avoid infinite loops when there are circular references. But it requires that all circular referential constraints have ON DELETE SET NULL or ON DELETE CASCADE.

    CREATE OR REPLACE PROCEDURE delete_cascade(
        table_owner          VARCHAR2,
        parent_table         VARCHAR2,
        where_clause         VARCHAR2
    ) IS
        /*   Example call:  execute delete_cascade('MY_SCHEMA', 'MY_MASTER', 'where ID=1'); */
    
        child_cons     VARCHAR2(30);
        parent_cons    VARCHAR2(30);
        child_table    VARCHAR2(30);
        child_cols     VARCHAR(500);
        parent_cols    VARCHAR(500);
        delete_command VARCHAR(10000);
        new_where_clause VARCHAR2(10000);
    
        /* gets the foreign key constraints on other tables which depend on columns in parent_table */
        CURSOR cons_cursor IS
            SELECT owner, constraint_name, r_constraint_name, table_name, delete_rule
              FROM all_constraints
             WHERE constraint_type = 'R'
               AND delete_rule = 'NO ACTION'
               AND r_constraint_name IN (SELECT constraint_name
                                           FROM all_constraints
                                          WHERE constraint_type IN ('P', 'U')
                                            AND table_name = parent_table
                                            AND owner = table_owner)
               AND NOT table_name = parent_table; -- ignore self-referencing constraints
    
    
        /* for the current constraint, gets the child columns and corresponding parent columns */
        CURSOR columns_cursor IS
            SELECT cc1.column_name AS child_col, cc2.column_name AS parent_col
              FROM all_cons_columns cc1, all_cons_columns cc2
             WHERE cc1.constraint_name = child_cons
               AND cc1.table_name = child_table
               AND cc2.constraint_name = parent_cons
               AND cc1.position = cc2.position
            ORDER BY cc1.position;
    BEGIN
        /* loops through all the constraints which refer back to parent_table */
        FOR cons IN cons_cursor LOOP
            child_cons   := cons.constraint_name;
            parent_cons  := cons.r_constraint_name;
            child_table  := cons.table_name;
            child_cols   := '';
            parent_cols  := '';
    
            /* loops through the child/parent column pairs, building the column lists of the DELETE statement */
            FOR cols IN columns_cursor LOOP
                IF child_cols IS NULL THEN
                    child_cols  := cols.child_col;
                ELSE
                    child_cols  := child_cols || ', ' || cols.child_col;
                END IF;
    
                IF parent_cols IS NULL THEN
                    parent_cols  := cols.parent_col;
                ELSE
                    parent_cols  := parent_cols || ', ' || cols.parent_col;
                END IF;
            END LOOP;
    
            /* construct the WHERE clause of the delete statement, including a subquery to get the related parent rows */
            new_where_clause  :=
                'where (' || child_cols || ') in (select ' || parent_cols || ' from ' || table_owner || '.' || parent_table ||
                ' ' || where_clause || ')';
    
            delete_cascade(cons.owner, child_table, new_where_clause);
        END LOOP;
    
        /* construct the delete statement for the current table */
        delete_command  := 'delete from ' || table_owner || '.' || parent_table || ' ' || where_clause;
    
        -- this just prints the delete command
        DBMS_OUTPUT.put_line(delete_command || ';');
    
        -- uncomment if you want to actually execute it:
        --EXECUTE IMMEDIATE delete_command;
    
        -- remember to issue a COMMIT (not included here, for safety)
    END;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 488k
  • Answers 488k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think that that is not a good way to… May 16, 2026 at 8:40 am
  • Editorial Team
    Editorial Team added an answer There's a race condition between your two threads. Basically, there… May 16, 2026 at 8:40 am
  • Editorial Team
    Editorial Team added an answer It could be useful if the size of the buffer… May 16, 2026 at 8:40 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Is it possible via script/tool to generate a delete statement based on the tables
Is it possible to change the Windows command prompt working directory via Python script?
I need to access COM port (console) via script to access our DSL modem.
I am triggering an ant script (via cruise control), and would like to be
I added some users from an excel via a script to my drupal database.
In Perl, is it possible to determine if a script is being executed within
What are some tools (commercial or OSS) that provide a GUI-based mechanism for creating
Assume SQL Server 2005+. Part A: What is the canonical way to query from
I'm trying to pass a value to an input box via json. I've been
I would like to know if it is possible (and if so how) to

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.