Deleting records from child to parent table

I have ran into a situation where i will have to delete all the records of the child table and then the records of parent table and i was able to write a query to get the column,table both for parent,child and stuck up in deleting them using query and
i will not be able to use Cascade...My validation criteria is applicable for the child tables and parent table...am trying to compare last modified date of each table...Kindly help me with an approach
DECLARE @mytable VARCHAR(30) SET @mytable = 'tablea'
DECLARE @Order INT SET @Order = 0
DECLARE @Count INT
declare @tblname varchar(100),@query NVARCHAR(max),@parent varchar(100)
if OBJECT_ID('tempdb..#OrderList','U') is not null
DROP TABLE #OrderList
if OBJECT_ID('tempdb..#temp','U') is not null
DROP TABLE #temp
CREATE TABLE #TEMP
OrderNo INT,
TableName VARCHAR(50),
tid varchar(100),
childtable varchar(100),
cid varchar(100)
CREATE TABLE #OrderList
RecordNo INT,
OrderNo INT,
TableName VARCHAR(50),
tid varchar(100),
childtable varchar(100),
cid varchar(100)
create table #t
taname varchar(100)
;with tempcte (level,parent,pcol,child,ccol)
as
SELECT 0,SO2.name, SC2.name,SO1.name, SC1.name
FROM dbo.sysforeignkeys FK
INNER JOIN dbo.syscolumns SC1
ON FK.fkeyid = SC1.id AND FK.fkey = SC1.colid
INNER JOIN dbo.syscolumns SC2
ON FK.rkeyid = SC2.id AND FK.rkey = SC2.colid
INNER JOIN dbo.sysobjects SO1
ON FK.fkeyid = SO1.id
INNER JOIN dbo.sysobjects SO2
ON FK.rkeyid = SO2.id
INNER JOIN dbo.sysobjects FO
ON FK.constid = FO.id
WHERE SO2.Name = 'tablea'
union all
SELECT level+1,SO2.name, SC2.name,SO1.name, SC1.name
FROM dbo.sysforeignkeys FK
INNER JOIN dbo.syscolumns SC1
ON FK.fkeyid = SC1.id AND FK.fkey = SC1.colid
INNER JOIN dbo.syscolumns SC2
ON FK.rkeyid = SC2.id AND FK.rkey = SC2.colid
INNER JOIN dbo.sysobjects SO1
ON FK.fkeyid = SO1.id
INNER JOIN dbo.sysobjects SO2
ON FK.rkeyid = SO2.id
INNER JOIN dbo.sysobjects FO
ON FK.constid = FO.id
inner join tempcte t
on SO2.Name = t.child
insert into #TEMP
OrderNo,
TableName,
tid,
childtable,
cid
select
level,parent,pcol,child,ccol from tempcte
select * from #TEMP
INSERT INTO #OrderList
SELECT Row_number() OVER (ORDER BY OrderNo, TableName),
#TEMP.OrderNo,
#TEMP.TableName,
#TEMP.tid,
#temp.childtable,
#TEMP.cid
FROM #TEMP
SET @Count = (SELECT Max(RecordNo)
FROM #OrderList)
select * from #OrderList

Am looking for a logic to delete the records..Please provide some query to better understand..
Thats easy, refer the below link: You may check string and then execute if everything looks ok to you.
Ref:http://social.msdn.microsoft.com/Forums/en-US/bc36c6b6-1aca-43aa-bc95-7518fdb6fe4c/delete-data-from-multiple-tables?forum=transactsql
declare @Deletecommand varchar(max)
;with object_cte(tblid,tblname,rtblid,rtblname,level)
as
select distinct o.object_id as tblid,OBJECT_NAME(o.object_id) as tblname,cast(null as int),cast(null as sysname),
0 as level
from sys.objects o
inner join sys.foreign_keys f
on f.parent_object_id = o.object_id
where o.is_ms_shipped=0
and o.type='u' and OBJECT_NAME(o.object_id) like '/BIO/A%'
union all
select t.object_id as tblid,OBJECT_NAME(t.object_id) as tblname,o.tblid,o.tblname,o.level + 1
from object_cte o
inner join sys.foreign_keys f
on f.parent_object_id = o.tblid
join sys.objects t
on t.object_id = f.referenced_object_id
where t.is_ms_shipped=0
and t.type='u'
and OBJECT_NAME(o.object_id) like '/BIO/A%'
select @Deletecommand =(select 'DELETE FROM ' + tblname + ';' from
select row_Number() over (partition by tblname order by level) as rn,* from object_cte
)t
where rn=1
order by level
for xml path(''))
print @Deletecommand
--exec (@Deletecommand)

Similar Messages

  • 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.

  • 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.

  • Deleting Records from Referential Tables

    Hi,
    Can anyone help me in deleting records from tables in a hierarcial manner. For e.g C refers B and B refers A. I need to delete data from all three tables related to a where clause in A.I tried using User_constraints and User_cons_columns views. But i am not able to find a hierarchial delete statement.
    Venkatesh

    Did you look into the option of specifying ON DELETE CASCADE for your foreign key constraints. That way, if you delete a
    row from the master table Oracle will automatically go and delete the child rows. Take a look at the following script.
    Here, when I delete a row from table A for NUM = 1 the child rows from table B (WHERE num = 1) are automatically deleted.
    drop table b
    drop table a
    create table a (num number, name varchar2(40))
    create table b (num number, name varchar2(40))
    alter table a add constraint pk_a primary key(num)
    alter table b add constraint fk_b_num foreign key (num) references a(num) on delete cascade
    insert into a values (1, 'Test#1')
    insert into a values (2, 'Test#2')
    insert into b values (1, 'Referes#1')
    insert into b values (2, 'Referes#2')
    select * from a
    select * from b
    delete from a where num = 1
    select * from a
    select * from b
    Sridhar,
    Thanks a lot for your help.My exact need was to delete records in 50 tables starting with the key table for a specific condition in the key table.
    Assume table A has 100 records,B 200 records,C 150 records,D 400 records, etc
    When i want to delete some records in table A, i will not be able to do that as child records exists in table B and this structure will carry on till that last table in my application. How do i delete those specific records
    Venkatesh

  • 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

  • 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 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.

  • 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.

  • Deleting records from table MVER

    We are experiencing performance issues when adding data to table MVER.  We have data on there that is over 5 years old which could be deleted.  As we have not been able to find any standard program or process that carries out housekeeping on MVER we are considering writing an ABAP program to delete the records.   Has anyone had any problems when deleting records from MVER or does anyone know of any reason why we should not delete these records in this way ?

    Well, I wouldn't delet data from a standard SAP table based on information from the internete. I'd look at other alternatives.
    Are you maintaining custom indexes on this table?
    Are your database statistics up to date?
    Consider a databse re-org.
    I don't think simple inserts should cause a problem.
    Rob

  • Any way to restore deleted record from VBAP table.

    Hi Guru,
    Is their any way to restore deleted record from vabp.
    Back is taken but , All quality server back is taken, any way to restore only
    deleted VBAP data from all Back.
    Regards
    Durgesh

    Hi Sahu ji,
    you will not be able to get those records.
    Check this : If this issue is in Development than no need to worry.
    If in quality and production , then usually a copy of the system is there , and this can help you.
    Also , check is there any report that exports this data in some other form for backup.
    Hope it help you.

  • Delete record from the form and from the database

    hi,
    i want delete record from the form and the database ,but the record is only delete from the from !!!
    this is my code :
    if //condition then
    delete_record;
    commit;
    end if ;
    Any solutions ??
    thnx

    You have unique key field(s) on the table you are trying to insert which actually restricts you from inserting the same value again.
    When you are deleting the record and issue commit there is a record to be inserted in the table which is a duplicate that's why you are getting this unique error.
    As oracle is not able to insert your commit fails and stops your deletion of record from table

  • Insertion of records from view into a table

    hello Friends,
    I have a view LGR is build on 12 table,this view has over 1 millon records, now when i run reports based on this view are so much slower, for speeding up reports i create a table LGRT and base all the reports on it, but the problem is that after any chage to the data of the LGR view , I delete all records from LGRT table and reinsert all records from LGR view
    delete from lgrt;
    insert into lgrt
    select *
    from lgr
    friends its a very long procedure what can i do to minimaze the human work.....
    i mean that it should be done automatically.

    Hello,
    It sounds like you could use a materialised view here to speed things up, also it will save you the hassle of deleting records from the temporary table etc as they offer quite a few synchronisation options. Have a look at the documentation here:
    http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540/statements_63a.htm#SQLRF01302
    There a quite a few examples toward the bottom of the page.
    HTH

  • How to delete records from standard maintenance view

    Dear Sir/Madam,
            i want to delete records from standard view " v_mmim_rep_cust "
    This is a standard maintenance view , used in MBLB report.
    here i found the records with different report name but same fields as shown below...
    REPORT             TABLE NAME     FIELD NAME 
    RM07DOCS         MKPF                 BKTXT
    RM07DOCS         MKPF                 BLDAT
    RM07DOCS         MKPF                 BUDAT
    RM07DOCS         MSEG                 ANLN1
    RM07DOCS         MSEG                 ANLN2
    RM07DOCS         MSEG                 APLZL
    YRM07DOCS         MKPF                 BKTXT
    YRM07DOCS         MKPF                 BLDAT
    YRM07DOCS         MKPF                 BUDAT
    YRM07DOCS         MSEG                 ANLN1
    YRM07DOCS         MSEG                 ANLN2
    YRM07DOCS         MSEG                 APLZL
    I WANT TO DELETE THE RECORDS FROM THE VIEW WITH REPORT = YRM07DOCS.
    PLEASE HELP ME.
    Thanks in Advance,
    Dastagiri.

    Dear Sir,
          when i did so , it displays a message that  " entry mseg zeile  must not be deleted ".
    hense i went through writing a program to delete the records from table mmim_rep_cust
    but it says that record not found.
    program logic : 
    delete from mmim_rep_cust
        where report in report
          and TABNAME in TABNAME.
    if sy-subrc = 0.
    write ' RECORDS DELETED SUCCESSFULLY'.
    else.
    write ' RECORD NOT FOUND'.
    endif.
    please guide me to delete the records from the view.
    Thanks in advance,
    Dastagiri.

  • How to handle the deleted records from R3

    Hello,
    We have created a generic data source on a database table in R3 side and now we have a case where there is a huge volume of data gets deleted and new records get updated every day.
    By doing a delta load we are able to load the New records and also the changed ones but we are unable to identify the deleted records from that table and move them to BI for deleteing those records in BI also.
    Can any one please suggest a solution for the handling the deleted records.
    Thanks,
    Ravindra.

    we had the same requirement some time ago and had two option:
    1. ask the R/3 development team add a deletion indicator in the table (and thus not actually deleting the record). this deletion indicator could then be used like for any other standard datasource
    this option was however refused, due to huge data volume after a while
    2. at the end of the load we copied the ZTABLE1 to ZTABLE2. then in the begin of the load (day after) we compare the data of table1 to table2. entries available in table2 but not in table1 are deleted, and we put a 'D'. in deletion indicator; as we only keep the deleted entries for one day, the volume of the new table is acceptable.
    M.

Maybe you are looking for

  • Prob with iTunes on Vista! Please Help!

    hi guys, i am getting the following error message when i install iTunes 7.5 version [IMG]http://img403.imageshack.us/img403/8344/itunesprobbo9.jpg[/IMG] i also tried iTunes 7.4 version but i am getting the same error message iTunes used to work befor

  • Can't Open Photos Sent To Me In Email on iPhone 4

    Hello All, I just had my phone replaced under warranty and have a problem I didn't have with the previous phone. If I am sent an email with a photo attached to it all I see is a little square dotted line box with the name of the file and it size. It

  • Error while navigating in OIC

    All, While navigating to any screens in "Plan Administration" for OIC, i get the following error. Could some1 please explain what could be the reason & a solution for the problem. I am new to Oracle Apps, so please bear. oracle.apps.fnd.framework.OAE

  • Crystal for Eclipse intermittent problem when viewing CR in .pdf over Web

    *(I also have no idea why I'm not getting carriage returns/line feeds when I press the Enter key so that this message is readable.)* My developer is telling me that the below error is causing one of our Crystal reports to not populate with data, and

  • Is there way to point to KM repository outside the EP installation Folder

    Hi, Is there way to point to KM repository outside the EP installation Folder.  BY default Ep gives defualt folder structure like KM Content  -> Root under root we have many folders like 1. attachment 2. collaboration_sessions 3. discussiongroups 4.