Deleting records from a recursive table query

Hi All,
I have the follow query, I would like to ask for your help, please: we use recursive tables for various purposes and one of them may have 100,000's records.
What would be the best approach to delete records from such a table?
I was thinking about two below, but any additional one will be more than welcome as well:
(1) I can DELETE FROM my_table WHERE my_table_id IN (SELECT my_table_id FROM my_table START WITH my_table_parent_id = X CONNECT BY PRIOR my_table_id = my_table_parent_id, but it means that I will run on my_table twice, no?
(2) I thought also to create a Foreign Key of my_table_id REFERENCES my_table_parent_id, but then I am afraid it will effect DML commands on that table.
Thank you in advance

Hi,
kdwolf wrote:
... (1) I can DELETE FROM my_table WHERE my_table_id IN (SELECT my_table_id FROM my_table START WITH my_table_parent_id = X CONNECT BY PRIOR my_table_id = my_table_parent_id, but it means that I will run on my_table twice, no?Sorry, I'm not sure whqt you mean by "I will run on my_table twice". Assuming you don't already have a foreign key constraint (as you described below) I don;t know any other way, let alone a more efficient way, to get the same results.
(2) I thought also to create a Foreign Key of my_table_id REFERENCES my_table_parent_id, but then I am afraid it will effect DML commands on that table.You're right; a foreign key constraint always costs a little whenever you do DML. Someone who knows your application and your system as well as you do will have to decide if the benefit of having accurate, consistent data justifies that additional cost. If you're not sure, I suggest you add the constraint.
One advantage of having a foreign key constraint is that you can create it with the "ON DELETE CASCADE" option. Then, if you simply say "DELETE FROM my_table WHERE my_table_patrent_id = X", all the descendants of those rows will automatically be deleted, exactly as you requested in (1) above.

Similar Messages

  • How to delete records from dynamic internal table.

    Hi Experts,
    Need urgent help!!!
    Issue is with Dynamic internal tables.
    Below is code written by me :
    FORM select_query USING Lw_tabnam
                      TYPE  t682i-kotabnr.
      DATA :  lw_line  TYPE REF TO data,
              lw_line1 TYPE REF TO data.
        CREATE DATA Lw_line    TYPE (lw_TABNAM).
        ASSIGN      Lw_line->* TO   <WA_tbl>.
        CREATE DATA LW_LINE    TYPE STANDARD TABLE OF (Lw_tabnam)
                               WITH NON-UNIQUE DEFAULT KEY.
        ASSIGN      Lw_line->* TO <TBL>.
        SELECT * FROM  (Lw_tabnam)
                 INTO CORRESPONDING FIELDS OF TABLE <TBL>
                 WHERE (t_keys).
    Endform.
    code is working fine.
    here even the table name and where condition are dynamic,everything is fine upto this point.
    Now i have to delete some record from <TBL> based on some conditons.
         for ex : ( here lc_fieldname is KUNNR)
          loop at t_kunnr.
              lw_tabix = sy-tabix.
            Read table <tbl>
                    with key (lc_fieldname) = t_kunnr-kunnr ASSIGNING <wa_tbl>.
            If sy-subrc = 0.
            *Delete
            delete <tbl> from <wa_tbl>
    delete <tbl> index  lw_tabix.
            Endif.
         Endloop.
    The above delete statement doesn't work ,even we can't use index as it gives a syntax error " something related to "index is not allowed in standard table or hash table.
    Can you help me ab't how to delete records in Dynamic internal table?
    Other option that i am thinking of is to create a static table of type dynamic table.
    means, data itab type standard table of <tbl> .I know the syntax is wrong ,however is there any way to do this?
    Thanks in advance ,
    If you have any suggestion ab't this then do let me know.
    bye,
    Gaurav.

    Hi
    I wrote this code and it works fine:
    DATA LW_TABNAM(10) VALUE 'LFA1'.
    DATA : LW_LINES TYPE REF TO DATA,
           LW_LINE  TYPE REF TO DATA.
    FIELD-SYMBOLS: <TABLE> TYPE TABLE,
                   <WA>    TYPE ANY.
    CREATE DATA LW_LINES TYPE TABLE OF (LW_TABNAM)
    WITH NON-UNIQUE DEFAULT KEY.
    ASSIGN LW_LINES->* TO <TABLE>.
    CREATE DATA LW_LINE TYPE (LW_TABNAM).
    ASSIGN LW_LINE->* TO <WA>.
    DO 10 TIMES.
      APPEND INITIAL LINE TO <TABLE>.
    ENDDO.
    SY-TABIX = 4.
    DELETE <TABLE> INDEX SY-TABIX.
    WRITE SY-SUBRC.
    I hope it help you
    Max

  • Deleting records from Self-Referencing Table

    I am using this query to dodge the foreign key error when trying to delete a record from a table that has primary key and referencing foreign key in the same table. It works with smaller set of data but for a table with 400,000 records it fails with error
    : maximum recursion limit of 500 reached. I changed maxrecursion to 3267 and even 0. but no records were returned it infact went to infinite loop..
    Please help if u have an alternative solution othr than a cascade trigger(which I am already considering)
    WITH    q AS
            SELECT  id, siteUrl
            FROM    TestComposite
            WHERE   id = 42
                    AND siteUrl = 'site1'
            UNION ALL
            SELECT  tc.id, tc.siteUrl
            FROM    q
            JOIN    TestComposite tc
            ON      tc.parentID = q.id
                    AND tc.siteUrl = q.siteUrl
    select * 
    FROM    TestComposite
    WHERE   EXISTS
            SELECT  id, siteUrl
            INTERSECT
            SELECT  id, siteUrl
            FROM    q
    Thanks,
    J

    I am using this query to dodge the foreign key error when trying to delete a record from a table that has primary key and referencing foreign key in the same table. It works with smaller set of data but for a table with 400,000 records it fails with error
    : maximum recursion limit of 500 reached. I changed maxrecursion to 3267 and even 0. but no records were returned it in fact went to infinite loop..
    That is a questionable approach. Don't dodge table constraints. They have a reason to be there. Deep recursion is also doubtful approach.
    USE tempdb;
    GO
    SELECT * INTO Employee FROM AdventureWorks.HumanResources.Employee;
    GO
    ALTER TABLE Employee ADD constraint PK PRIMARY KEY (EmployeeID);
    ALTER TABLE Employee ADD constraint fkPK FOREIGN KEY (ManagerID) REFERENCES Employee(EmployeeID);
    GO
    SELECT * FROM Employee;
    GO
    -- Staff - no child records
    DELETE Employee WHERE EmployeeID = 283; -- (1 row(s) affected)
    -- Manager - child records
    DELETE Employee WHERE EmployeeID = 284;
    Msg 547, Level 16, State 0, Line 19
    The DELETE statement conflicted with the SAME TABLE REFERENCE constraint "fkPK". The conflict occurred in database "tempdb", table "dbo.Employee", column 'ManagerID'.
    The statement has been terminated.
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Design & Programming
    New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

  • Deleting Records from engine DB tables

    Hi,
    In DEV ENV,the bam-dwupdater log file, the datawarehouse service is not able to find the instance with in: 737.
    It is constantly throwing error as : "Process with in [737] not found ", thus increasing the size of the log file.
    Is 737 is the project id of the application in FDI table
    From the engine db, the pprocinstance table is still containg 16K instances with processid as 737.
    We are thinking of clearing these records by runnning query manuallly.
    From which other tables we need to delete the records for the instances with the same processid?
    As we will use cascade delete, what is the parent-child relationship between the engine db tables??
    Is it advisable to clear the above records from pprocinstance table by running query?
    From where can i get the relationship, is there any oracle document on this?
    From where is this error coming repeteadly??
    Please advise.
    Regards,
    Shantanu
    Edited by: 789755 on Sep 15, 2011 6:53 AM
    Edited by: 789755 on Sep 15, 2011 6:55 AM

    The relevant code with comments:// Find out where the file pointer is pointing now
      long point = book_database.getFilePointer();
    // Read in everything beyond that point in the file
    // (You seem to assume it was pointing at record number row+1.)
    // (I see nothing to convince me that is true.)
      byte[] b = new byte[(int)book_database.length()-(int)point];
      book_database.readFully(b);
    // Move the file pointer to point at the record you want to delete
      book_database.seek(rowstart);
    // Write the "everything" over that records and the ones after it.
      book_database.write(b);
    // Set the length of the file to the right value, at least the right
    // value of what you actually wrote there.
    book_database.setLength(book_database.length()-(point-rowstart) );If you say that code is deleting the selected row and everything after it, then it follows that the file pointer is pointing at the end of the file when this code runs. A little bit of debugging code would test that hypothesis; you should see that the byte array contains zero elements.

  • Deleting records from a versioned table slow.

    Hi,
    I have a problem the sql statement delete * from version_table slows down considerably as the number of history entries increase. 10g advises that an index should be created on the primary key column and creation time to improve performance. My question is this a viable option or is there an alternative. This has become apparent as java developer write units test to clear out data before and after running tests.
    Cheers
    Mark

    Hi,
    How significant is the change in performance as the number of history entries increase ? Since the delete statement is running without a where clause, I would expect a full table scan to be performed in both cases. If this is the case, then the amount of time to delete the same number of rows should remain fairly constant with only a slight increase in time. This is of course unless the size of the base table is also growing considerably from those extra historical rows, in which case the extra time would make sense in some cases.
    Also, are the number of rows that are being deleted in both cases the same, or are the additional history entries due to inserts? If the extra rows are from inserts that I would expect the amount of time for the deletion to occur to increase, but only in proportion to the number of additional rows.
    There is not too much else I can say without additional information. Feel free to file a TAR on this issue if this does not answer your questions.
    Regards,
    Ben

  • Deleting records from internal tabl

    Hi All,
    Here i need delete records from one internal table, from another internal table. both contains same field as name1, so here , i need to delete records from t_itab , the records which are existed in t_itab1.
    first internal table t_itab contains
    vinesh01
    vinesh02
    vinesh03
    vinesh04
    second internal table t_itab1 contains
    vinesh01
    vinesh02
    here i need to delte t_itab1 entries from t_itab.
    regards,
    vinesh.

    Hi,
    try this code.
    first internal table t_itab contains
    vinesh01
    vinesh02
    vinesh03
    vinesh04
    second internal table t_itab1 contains
    vinesh01
    vinesh02
    loop at t_itab1.
    loop at t_itab.
    if t_itab-name1 = t_itab1-name1.
    delete t_itab.
    (OR)
    delete itab where t_itab-name = t_itab1-name.
    endif.
    endloop.
    endloop.
    regards.
    sriram.

  • ADF UIX - Deleting records from expert-query view

    Hi gang
    Does anybody have any suggestions how to do this?
    I'll use the HR schema as an example.
    We have default EOs defined against the departments and employees tables. Those familiar with the HR schema know there is fk between departments and employees, as well as employees to itself (eg. employees to managers).
    We have created a complex VO, with a hierarchical join between employee records, as well as a join to the departments table. To create such a complex VO we have no choice but to create the VO with an "expert-query" as the standard wizard doesn't support hierarchical joins.
    One important fact to note that in the VO create wizard, when you use an "expert-query" you do not select the entities used by the VO; you go straight to the query page and enter the "expert-query". As such no EOs are defined against this type of complex VO.
    The VO output looks something like the following:
    Department Level Employee
    Finance    1     Alexandar Hunold
    Finance    2     Bruce Ernst
    Finance    2     David Austin
    Finance    2     Valli Patabella
    Purchasing 1     Den Rapaely
    Purchasing 2     Alexandar Khoo....and so on.
    We have created a UIX web page displaying this VO as a read-only-table.
    We wish to provide a delete button such that when the user selects a record, we may delete that employee AND all relating employees in a "cascade-delete" fashion. Note that we don’t wish to delete the associated department.
    In implementing this delete button the delete functionality works correctly in the UIX web page by removing the record from the UIX table (assume I've also commited my changes). However a forced requery of the VO shows that the record hasn't really been deleted. I assume this is because the complex VO has no explicitly stated underlying EOs.
    Does somebody have a suggestion on how I can catch the delete operation on the VO, and tell my application to instead delete a particular employee and perform a cascading delete on all other employees?
    Should I create a custom method in the VO row class, to perform the deletes on the database? Or can I force the VO to perform a cascade delete on the employees EO? I just don't know what to do.....?
    Any help greatly appreciated!
    My environment is JDev 9.0.5.2.
    Thanks!
    CM.

    I came across the help topic in JDeveloper 'Making an Association into a Composition' that talks about modifying an association to trigger cascade-delete. Does this help ?
    - Udupa

  • Delete records from tableA which are not in tableB

    Table A contains milions of records which is the best way to delete records from tableA which are not in tableB
    delete from tableA where empno not in (select empno from tableb)
    or
    delete from tableA where empno not exists (select empno from tableb
    where b.empno=a.empno)
    any help

    Hi
    If you can do this, do with this:
    create table tableC
    as select a.*
    from tableA a,
    (select empno from tableA
    minus
    select empno from tableB) b
    where a.empno = b.empno;
    drop table tableA;
    rename table tableC to tableA;
    Ott Karesz
    http://www.trendo-kft.hu

  • Delete records from internal table

    hi all,
    i want to delete records from intenal table which are starting with a particular starting number .
    eg internal table
    10000
    20000
    90000
    91000
    92000
    88880
    i want delete the records starting with 9 i.e. 90000 91000 92000.
    Thanks in Adv
            RAJ

    You can test this piece of code.
    DATA:
    i_tab TYPE STANDARD TABLE OF mara,
    wa_tab TYPE mara.
    wa_tab-matnr = '1000'.
    APPEND wa_tab TO i_tab.
    CLEAR wa_tab.
    wa_tab-matnr = '1001'.
    APPEND wa_tab TO i_tab.
    CLEAR wa_tab.
    wa_tab-matnr = '1002'.
    APPEND wa_tab TO i_tab.
    CLEAR wa_tab.
    wa_tab-matnr = '1003'.
    APPEND wa_tab TO i_tab.
    CLEAR wa_tab.
    wa_tab-matnr = '2001'.
    APPEND wa_tab TO i_tab.
    CLEAR wa_tab.
    wa_tab-matnr = '3001'.
    APPEND wa_tab TO i_tab.
    CLEAR wa_tab.
    wa_tab-matnr = '4010'.
    APPEND wa_tab TO i_tab.
    CLEAR wa_tab.
    <REMOVED BY MODERATOR>
    Edited by: Alvaro Tejada Galindo on Aug 8, 2008 4:49 PM

  • Delete records from multiple table

    Hi,
    I need to delete records from multiple tables using a single delete statement. Is it possible ? If so please let me know the procedure.
    Kindly Help.
    Thanks,
    Alexander.

    Hi Tim,
    Syntax of DELETE statement does not allow for multiple tables to be specified in this way. Infact, none of the DMLs allow you to specify table names like this.
    Technically, there are other ways of deleting from multiple tables with one statement.
    1. "Use a trigger":
    What was probably meant by this is that you have a driving-table on which you create a on-delete trigger. In this trigger, you write the logic for deleting from other tables that you want to delete from.
    This does mean a one-time effort of writing the trigger. But the actual DML operation of deleting from all the tables would be simply triggered by a delete on driving-table.
    2. Dynamic SQL:
    Write a PL/SQL code to open a cursor with table-names from which you want the data to be deleted from. In the cursor-for loop, write a dynamic SQL using the table-name to delete from that table.
    3. Using Foreign-Key constraint with Cascade-Delete:
    This I feel is a more 'cleaner' way of doing this.
    Having to delete data from multiple tables means that there is some kind of parent-child relationship between your tables. These relationships can be implemented in database using foreign-key constraints. While creating foreign-key constraint give the 'on delete cascade' clause to ensure that whenever data is deleted from parent-table, its dependent data is deleted from child-table.
    Using foreign-key constraint you can create a heirarchy of parent-child relationships and still your DELETE would be simple as you would only have to delete from parent-table.
    IMPORTANT: Implementing foreign-key constraints would also impact other DML operations that you should keep in mind.

  • Query regarding the data type for fetcing records from multiple ODS tables

    hey guys;
    i have a query regarding the data type for fetcing records from multiple ODS tables.
    if i have 2 table with a same column name then in the datatype under parent row node i cant add 2 nodes with the same name.
    can any one help with some suggestion.

    Hi Mudit,
    One option would be to go as mentioned by Padamja , prefxing the table name to the column name or another would be to use the AS keyoword in your SQL statement.
    AS is used to rename the column name when data is being selected from your DB.
    So, the query  Select ename as empname from emptable will return the data with column name as empname.
    Regards,
    Bhavesh

  • Delete records from z-table

    Hi ,
    delete statement is not working .
    i am using the logic like this .
    loop at itab into work_area.
    delete z-table from work_area .
    endloop.
    but i am unable to delete the records from the z-table .
    could you please look into this.
    Thanks,
    kumar.

    hi suresh,
    <b>DELETE  dbtab FROM wa.</b>
    After the statement is executed, the sytem field SY-DBCNT contains the number of lines that were deleted (0 or 1).
    The Return Code is set as follows:
    SY-SUBRC = 0:
    The line was deleted.
    SY-SUBRC = 4:
    No line could be deleted since there was no line with the primary key specified.
    example:
    <b>
    DATA wa TYPE sbook.
    wa-carrid = 'LH'.
    wa-connid = '0400'.
    wa-fldate = '20010228'.
    wa-bookid = '00000003'.
    DELETE sbook FROM wa.</b>
    check this link:
    http://help.sap.com/saphelp_47x200/helpdata/en/a6/0c723c590a9f6fe10000000a11405a/frameset.htm
    http://help.sap.com/saphelp_47x200/helpdata/en/a4/35ff395ef548e5add909b73ca12878/frameset.htm
    reward me if useful...
    Harimanjesh AN

  • I HAVE A SOURCE TABLE WITH 10 RECORDS AND TARGET TABLE 15 RECORDS. MY WUESTION IS USING WITH THE TABLE COMPARISON TRANSFORM I WANT TO DELETE UNMATCHED RECORDS FROM THE TARGET TABLE ??

    I HAVE A SOURCE TABLE WITH 10 RECORDS AND TARGET TABLE 15 RECORDS. MY QUESTION IS USING WITH THE TABLE COMPARISON TRANSFORM .I WANT TO DELETE UNMATCHED RECORDS FROM THE TARGET TABLE ?? HOW IT IS ??

    Hi Kishore,
    First identify deleted records by selecting "Detect deleted rows from comparison table" feature in Table Comparison
    Then Use Map Operation with Input row type as "delete" and output row type as "delete" to delete records from target table.

  • How do you delete records from table with data in a select option

    how do you delete records from table with relevant to data in a select option..how to write coding

    Hi,
    Try
    if not s_select_option [ ] is initial.
    delete * from table
    where field in s_select_option.
    endif.
    commit work.
    Be careful though. If select option is emty, you will delete the entire table.
    Regards,
    Arek

  • Deleting records from Z table via an ABAP

    Hello hoping someone can help me
    I am about to start writing an abap that will delete records from three seperate z tables that have been in there for over 18 months.
    Hoping that someone can give me a shove in the right direction to start me off as never had to do anything like this before.
    Iv had a search through the forum but cant seem to find what im after
    Thanks

    Hi,
    The below statement will do the purpose.
    DELETE zcustom FROM TABLE ITAB.

Maybe you are looking for

  • How to pass the data to a value attribute

    Hi, I am having a value attribute in a model node, It should have the values along with the modal attributes as its used in a table view can anyone explain me how to achieve this, It would be more helpful for me if someone gives sample code, Thanks i

  • PDF Thumnbnail preview PROBLEM (Windows 7 x32)

    I have problems now with my adobe reader thumbnail preview. This started when I had unistall my Adobe Photoshop cs5 white rabbit to replace with Adobe Photoshop CS5 Extended. When I uninstalled it with Revounistaller, the thumbnail preview for my pdf

  • Issue with Evaluate_Aggr Funtion

    Hi All, I am having issue with Evaluate_Aggr function in RPD, i am using OBIEE 10g EVALUATE_AGGR('SUM(COUNT(%1)) OVER ( PARTITION BY (%2) ) ' AS VARCHAR ( 200 ), "BMM"."Table1"."Col1", "BMM"."Table2"."Col2") The wierd thing is the above calculation i

  • Cross Tab keeps suppressing rows with no data

    I have a Crystal Report that is using a Cross Tab.  I have 2 rows in the cross tab the first row is the Month name and the second row is the Day Number of the month, and the report will print the Month Name on the left followed by all the Calendar Da

  • Can't make hyperlinks in CS 5

    I can't make hyperlinks in ID CS5. I highlight some text, select new hyperlink in the hyperlink panel, type in a url, cllick ok. The linked text shows up in the panel, but in the document it's still just text. It's not a link. What am I doing wrong?