Deleting record on basis of master table ID

Hello,
SQL> desc news
Name                                      Null?    Type
NEWS_ID                                   NOT NULL NUMBER(14)
NEWS_DATE                                          TIMESTAMP(0)
SL_ID                                              NUMBER(2)
HEADING                                            VARCHAR2(3120)
DESCRIPTION                                        VARCHAR2(3900)
SQL> desc news_location
Name                                      Null?    Type
NEWS_ID                                            NUMBER(14)
COUNTRY                                            VARCHAR2(32)
REGION                                             NUMBER(2)
SQL> desc news_product
Name                                      Null?    Type
NEWS_ID                                            NUMBER(14)
PRODUCT_CATEGORY_ID                       NOT NULL NUMBER(14)
PRODUCT                                            VARCHAR2(27)
SQL> desc news_service
Name                                      Null?    Type
NEWS_ID                                            NUMBER(14)
SERVICE_ID                                NOT NULL NUMBER(14)
SRVIS                                              VARCHAR2(16)
SQL> desc news_info
Name                                      Null?    Type
NEWS_ID                                            NUMBER(14)
SOURCE                                             VARCHAR2(203)
ORIGIONAL_NEWS                                     VARCHAR2(3900)
HEADING                                            VARCHAR2(3110)
SQL> select count(*) from news where TO_CHAR(news_date,'YYYY') <  2012;
  COUNT(*)
      8759I am trying to delete news that are published before 2012 but detail tables consists child record on basis of news_id. Certainly simple delete query will not work
Please advise and thanks in anticipation

Thanks again for favorable replies
SQL> alter table news_info drop constraint NEWS_INFO_FK;
Table altered.
SQL> alter table news_info add constraint NEWS_INFO_FK FOREIGN KEY (news_id) REF
ERENCES   news(news_id) on delete cascade enable novalidate ;
Table altered.
SQL> alter table news_info modify constraint NEWS_INFO_FK validate ;
Table altered.
SQL> alter table news_location drop constraint NEWS_LOCATION_FK;
Table altered.
SQL> alter table news_location add constraint NEWS_LOCATION_FK FOREIGN KEY (news
_id) REFERENCES   news(news_id) on delete cascade enable novalidate ;
Table altered.
SQL> alter table news_location modify constraint NEWS_LOCATION_FK validate ;
Table altered.
SQL> alter table news_PRODUCT drop constraint NEWS_PRODUCT_FK;
Table altered.
SQL> alter table news_PRODUCT add constraint NEWS_PRODUCT_FK FOREIGN KEY (news_i
d) REFERENCES   news(news_id) on delete cascade enable novalidate ;
Table altered.
SQL> alter table news_PRODUCT modify constraint NEWS_PRODUCT_FK validate ;
Table altered.
SQL> alter table news_SERVICE drop constraint NEWS_SERVICE_FK;
Table altered.
SQL> alter table news_SERVICE add constraint NEWS_SERVICE_FK FOREIGN KEY (news_i
d) REFERENCES   news(news_id) on delete cascade enable novalidate ;
Table altered.
SQL> alter table news_SERVICE modify constraint NEWS_SERVICE_FK validate ;
Table altered.
SQL> delete from news where TO_CHAR(news_date,'YYYY') <  2008 ON DELETE CASCADE;
delete from news where TO_CHAR(news_date,'YYYY') <  2008 ON DELETE CASCADE
ERROR at line 1:
ORA-00933: SQL command not properly ended
SQL> select constraint_name, constraint_type, table_name, delete_rule
  2    from user_constraints
  3   where constraint_name= 'NEWS_SERVICE_FK';
CONSTRAINT_NAME                C TABLE_NAME                     DELETE_RU
NEWS_SERVICE_FK                R NEWS_SERVICE                   CASCADE
SQL> select constraint_name, constraint_type, table_name, delete_rule
  2    from user_constraints
  3   where constraint_name= 'NEWS_PRODUCT_FK';
CONSTRAINT_NAME                C TABLE_NAME                     DELETE_RU
NEWS_PRODUCT_FK                R NEWS_PRODUCT                   CASCADE
SQL> select constraint_name, constraint_type, table_name, delete_rule
  2    from user_constraints
  3   where constraint_name= 'NEWS_LOCATION_FK';
CONSTRAINT_NAME                C TABLE_NAME                     DELETE_RU
NEWS_LOCATION_FK               R NEWS_LOCATION                  CASCADE
SQL> select constraint_name, constraint_type, table_name, delete_rule
  2    from user_constraints
  3   where constraint_name= 'NEWS_INFO_FK';
CONSTRAINT_NAME                C TABLE_NAME                     DELETE_RU
NEWS_INFO_FK                   R NEWS_INFO                      CASCADE
SQL> delete from news where TO_CHAR(news_date,'YYYY') <  2008 ON DELETE CASCADE;
delete from news where TO_CHAR(news_date,'YYYY') <  2008 ON DELETE CASCADE
ERROR at line 1:
ORA-00933: SQL command not properly ended
SQL>Edited by: Christy H. on Jan 17, 2013 1:58 AM
I removed ON DELETE CASCADE and ran again delete from news where TO_CHAR(news_date,'YYYY') <  2008; Wow deleted!!
This forum is miracle to learn oracle. Thanks from the bottom of my heart

Similar Messages

  • Delete rows on basis of other table

    I am trying to delete records from table PS_BUN_CM on basis of table PS_BUN_UPDATE ...
    below is the select statment which i want to convert to delete ... Please help
    select *  FROM PS_BUN_CM a
    inner join PS_BUN_UPDATE c on a.MAP_ID = c.MAP_ID
    where a.ps_map_id in ('2400') and (a.las_name = 'NA' or a.las_name = 'na')and (c.LAS_NAME <> '  ');  

    Not sure, how it is deleting all rows at your end. Please see below test
    create table PS_BUN_CM(MAP_ID number,PS_MAP_ID varchar2(10),las_name varchar2(100))
    Insert into PS_BUN_CM(MAP_ID, PS_MAP_ID, LAS_NAME)
    Values(100, '10', 'abc')
    Insert into PS_BUN_CM(MAP_ID, PS_MAP_ID, LAS_NAME)
    Values(100, '100', 'NA')
    Insert into PS_BUN_CM(MAP_ID, PS_MAP_ID, LAS_NAME)
    Values(100, '100', 'na')
    Insert into PS_BUN_CM(MAP_ID, PS_MAP_ID, LAS_NAME)
    Values(1000000, '10022', 'na')
    Insert into PS_BUN_CM(MAP_ID, PS_MAP_ID, LAS_NAME)
    Values(100, '2400', 'na')
    COMMIT
    SQL> select * from ps_bun_cm;
        MAP_ID PS_MAP_ID  LAS_NAME
        100    10         abc
        100    100        NA
        100    100        na
       1000000 10022      na
        100    2400       na
    SQL>
    create table PS_BUN_UPDATE(MAP_ID number,las_name varchar2(100))
    Insert into PS_BUN_UPDATE(MAP_ID, LAS_NAME)
    Values(100, 'xyz')
    Insert into PS_BUN_UPDATE(MAP_ID, LAS_NAME)
    Values(100, '  ') 
    COMMIT
    SQL> select a.*,length(las_name)  from ps_bun_update a;
        MAP_ID  LAS_NAME   LENGTH(A.LAS_NAME)
        100     xyz           3
        100                   2
    SQL>
    SQL>select a.* from PS_BUN_CM a
      2 inner join PS_BUN_UPDATE c on a.map_id=c.map_id
      3 where a.ps_map_id in('2400') and (a.las_name='NA' or a.las_name='na')
      4 and c.las_name='  ';
        MAP_ID  PS_MAP_ID LAS_NAME
        100       2400     na
    SQL> select * from     ps_bun_cm a
      2  where exists(select 1 from ps_bun_update c
      3               where a.map_id=c.map_id
      4               and a.ps_map_id in('2400')
      5               and (a.las_name='NA' or a.las_name='na')
      6               and c.las_name='  '
      7               ) ;
        MAP_ID  PS_MAP_ID  LAS_NAME
           100       2400   na
    SQL> ed
    Wrote file afiedt.buf
      1  delete from     ps_bun_cm a
      2  where exists(select 1 from ps_bun_update c
      3               where a.map_id=c.map_id
      4               and a.ps_map_id in('2400')
      5               and (a.las_name='NA' or a.las_name='na')
      6               and c.las_name='  '
      7*              )
    SQL> /
    1 row deleted.
    SQL> select * from PS_BUN_CM;
        MAP_ID PS_MAP_ID  LAS_NAME
         100   10         abc
         100   100        NA
         100   100        na
       1000000 10022      na
    SQL>

  • How to insert past record after updating the master table in history table through store PROC

    Master Table
    Party Status
    A Active
    B Inactive
    C Active
    D Inactive
    Duplicate Table
    Party Status
    A Active
    B Active
    C Active
    D Inactive
    Updated Master Table
    Party Status
    A Active
    B Active
    C Active
    D Inactive
    Party History Table
    B Inactive
    I have two table one master and another duplicate I need to update master table based on duplicate table insert the record which updated into Party history table as shown above. need help to write store proc.

    Check MERGE syntax in BOL (example D). There should be a sample with output, e.g.
    insert into PartyHistory (Party, [Status])
    select Party, [Status] FROM
    (MERGE Master M using Duplicate D on M.[Party]=D.[Party]AND M.[Status]<>D.[Status]
    WHEN MATCHED THEN UPDATE
    SET [Status] = D.[Status]
    OUTPUT Deleted.[Party], Deleted.[Status], $Action) AS Changes (Party, [Status], Action) WHERE Action = 'UPDATE'
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

  • Delete records on basis of primary key.

    HI all,
    I need an ODI code so that i Should delete the record from target table on the basis of primay key with the reference of I$ table.
    for example ..
    T is target table & I$T is IKm table.
    T has primary key P and I$t is also primay key.
    So catually the code should be
    delete from T where T.P <> I$.P
    I wnat to insert the ODI code in IKM .
    I had tried something like but it doesnt work
    delete from <%=snpRef.getTable("L","TARG_NAME","A")%>
    where     <%=snpRef.getColList("", "[COL_NAME]\t <> :[COL_NAME]", "\nand\t", "", "(UK)")%>
    Please sussgest.
    Edited by: neeraj_singh on Dec 1, 2008 12:01 AM

    Hi,
    Use:
    delete from <%=snpRef.getTable("L","TARG_NAME","A")%>
    where <%=snpRef.getColList("(", "COL_NAME", ",", ") in ", "(UK)")%>
    (select <%=snpRef.getColList("", "COL_NAME", ",", "", "(UK)")%> from <%=snpRef.getTable("L", "INT_NAME", "A")%>)
    That code will give what you need...
    Cezar Santos

  • How to reatin record selection in a master table

    Hi,
    I have table A with single selection item and detail table B below, both VO based. Once single selection is clicked the detail table shows corresponding result record set. I need to retain the selection of the record in table A once query on table B. is there was to do that?
    Thank you
    Anatoliy

    Hi,
    Refer below links:
    http://oracle.anilpassi.com/master-detail-screen-in-oa-framework-part-i.html
    https://kr.forums.oracle.com/forums/thread.jspa?threadID=2364464
    --Sushant                                                                                                                                                                                                                                                                                                                                                                               

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

  • 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

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

  • Writing a store procedure  for deleting records

    how can i write a store procedure for deleting records from a table.
    i have a table called employees
    empid
    empname
    deptid
    and table department
    deptid
    dept
    floor
    how can i write a store procedure which would delete records of employees from employees table for particular department when i delete that department from department table.
    thanks

    872959 wrote:
    how can i write a store procedure for deleting records from a table.
    i have a table called employees
    empid
    empname
    deptid
    and table department
    deptid
    dept
    floor
    how can i write a store procedure which would delete records of employees from employees table for particular department when i delete that department from department table.
    thanksDoes not seem to be a sound design, to me.
    Employees do not go missing when organization change is made & department is eliminated.
    If you insist such nonsense, write TRIGGER to issue desired DML

  • Get all records in multi selected master child tables

    Hi,
    I am using JDeveloper 11.1.1.4 version and using ADF-BC in my project.
    I have a simple master child[one to many] relationship in my project.
    In my view page,I display this master child [Ex: EmpVo1--->DeptVo2] as tables.
    I have multi-slection enabled for master table.
    My requirement is that,on multi selecting the rows in master tables,I want to get all the child records in my backing bean.
    that is if a master row has 3 child records and another master row has 4 child records and on multiple selection of these two records in master table,I should get all the child records in my backing bean.
    I need this to implement cascade delete functionality.
    Following is sample piece of code
    1) called on selecting the rows in master table
    public void onRSCGrpSelect(SelectionEvent selectionEvent) {
    // Add event code here...
    ADFUtil.invokeEL("#{bindings.RscGroupVO1.collectionModel.makeCurrent}",
    new Class[] { SelectionEvent.class },
    new Object[] { selectionEvent });
    RowKeySet rowKeySet = (RowKeySet)tblRSCGrp.getSelectedRowKeys();
    CollectionModel cm = (CollectionModel)tblRSCGrp.getValue();
    for (Object facesTreeRowKey : rowKeySet) {
    cm.setRowKey(facesTreeRowKey);
    JUCtrlHierNodeBinding rowData =
    (JUCtrlHierNodeBinding)cm.getRowData();
    Row row = rowData.getRow();
    System.out.println("\n" +
    row.getAttribute(0) + " :: " + row.getAttribute(1) +
    " :: " + row.getAttribute(2));
    System.out.println("Displaying Child Records");
    displayChildRecords(row.getAttribute(0));
    2. private void displayChildRecords(Object rscGrp) {
    ViewObject rscMapVo = getRscMapViewObj();
    RowSetIterator rsI = rscMapVo.createRowSetIterator(null);
    while (rsI.hasNext()) {
    Row row = rsI.next();
    System.out.println("\n" +
    row.getAttribute(0) + " :: " + row.getAttribute(1) +
    " :: " + row.getAttribute(2));
    rsI.closeRowSetIterator();
    But the problem is that ,it is always giving me the last selected rows child record details
    Please suggest the error I am doing.
    Thanks,
    Praveen

    Your problem is that you use makecurrent, which should not be used on a multi select table. Next if you have master detail relationship you should have a view link between them. In this case you can expose a method in you master to get the related child row. No need to get the VO itself as you can use the child iterator accessors to get the child record.
    public void onRSCGrpSelect(SelectionEvent selectionEvent) {
    // Add event code here...
    RowKeySet rowKeySet = (RowKeySet)tblRSCGrp.getSelectedRowKeys();
    CollectionModel cm = (CollectionModel)tblRSCGrp.getValue();
    for (Object facesTreeRowKey : rowKeySet) {
    cm.setRowKey(facesTreeRowKey);
    JUCtrlHierNodeBinding rowData =
    (JUCtrlHierNodeBinding)cm.getRowData();
    Row row = rowData.getRow();
    //cast to the right row class
    EmpEmpVoRow empRow = (EmpEmpVoRow) row;
    // now you cann access the child row iterator
    RowSetIterator it = empRow.getDepVO();
    //now you cna iterate over the child rows
    System.out.println("\n" +
    row.getAttribute(0) + " :: " + row.getAttribute(1) +
    " :: " + row.getAttribute(2));
    System.out.println("Displaying Child Records");
    //use hte child rows here
    }Not sure if the code compiles out of the box (doing this on the train :-)
    Timo

  • Hard Delete all soft delete records (members) in Master Data Service (MDS) database

    Hi,
    I am using Master Data Service for couple of months now. I can load, update, merge and soft delete data in MDS. Occasionally we even have to hard delete data from MDS. If we keep on soft deleting records in a MDS table eventually there will be huge number
    of soft deleted records.
    Is there an easy way to hard delete all the soft deleted records from all MDS tables in a specific Model.
    Regards,
    Rehan

    We did develop a Transact SQL procedure for this using the staging interface. It works and can be used freely :)
    God Luck!
    Jan Isacsson
    USE [MDS]
    GO
    /****** Object: StoredProcedure [dbo].[AutoPurge] Script Date: 4/21/2015 10:39:21 PM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- To be used without warranty but it works even for collections
    -- Jan Isacsson (www.independent.se) 2015-04-21
    CREATE PROCEDURE [dbo].[AutoPurge]
    @modelName nvarchar(200) = 'PDWmReferenceData',
    @versionName nvarchar(200) = 'VERSION_1',
    @entityName nvarchar(200) = NULL
    AS
    DECLARE @name nvarchar(200)
    DECLARE @tableName nvarchar(200)
    DECLARE @stagingName nvarchar(200)
    DECLARE @collectionTable nvarchar(200)
    DECLARE @collectionMemberTable nvarchar(200)
    DECLARE @isFlat INT
    DECLARE @sqlIsFlat NVARCHAR(512)
    DECLARE @sqlGetEntityTable NVARCHAR(512)
    DECLARE @sqlGetCollectionTable NVARCHAR(512)
    DECLARE @sqlGetCollectionMemberTable NVARCHAR(512)
    DECLARE @sqlGetStagingBaseTable NVARCHAR(512)
    DECLARE @sqlLoadPurgeConsolidated NVARCHAR(512)
    DECLARE @sqlLoadPurgeLeaf NVARCHAR(512)
    DECLARE @sqlRinseStaging NVARCHAR(512)
    DECLARE @sqlPurgeCollections NVARCHAR(512)
    DECLARE @sqlCheckErrorStaging NVARCHAR(512)
    DECLARE @batchTagName NVARCHAR(512)
    DECLARE @executePurge NVARCHAR(512)
    DECLARE @errorMsg NVARCHAR(512)
    DECLARE @i INT
    DECLARE @id INT
    DECLARE leaf_entity_cursor CURSOR FOR
    select e.ID, e.Name from
    mdm.tblModel m,
    mdm.tblEntity e
    where
    e.Model_ID = m.ID
    and m.Name = @modelName
    DECLARE noleaf_entity_cursor CURSOR FOR
    select e.ID, e.Name from
    mdm.tblModel m,
    mdm.tblEntity e
    where
    e.Model_ID = m.ID
    and m.Name = @modelName
    and e.IsFlat = 0
    -- Leaf purge
    SET @batchTagName = '''AutoPurge'''
    OPEN leaf_entity_cursor
    FETCH NEXT FROM leaf_entity_cursor INTO @id, @name
    WHILE @@FETCH_STATUS = 0
    BEGIN TRY
    if @name = @entityName or @entityName is null
    BEGIN
    SET @sqlGetEntityTable = N'select @tableName = EntityTable from mdm.tblEntity where ID = ' + CONVERT(nvarchar,@id)
    EXEC sp_executesql
    @query = @sqlGetEntityTable,
    @params = N'@tableName NVARCHAR(100) OUTPUT',
    @tableName = @tableName OUTPUT
    SET @sqlGetStagingBaseTable = N'select @stagingName = StagingBase from mdm.tblEntity where ID = ' + CONVERT(nvarchar,@id)
    EXEC sp_executesql
    @query = @sqlGetStagingBaseTable,
    @params = N'@stagingName NVARCHAR(100) OUTPUT',
    @stagingName = @stagingName OUTPUT
    SET @sqlRinseStaging = N'delete from stg.' + @stagingName + '_Leaf where BatchTag = ' + @batchTagName
    EXEC sp_executesql @query = @sqlRinseStaging
    SET @i = @@RowCount
    SET @sqlLoadPurgeLeaf = N'insert into stg.' + @stagingName + '_Leaf (ImportType, ImportStatus_ID, BatchTag, Code) select 6, 0, ''AutoPurge'', Code from mdm.' + @tableName + ' where Status_ID = 2'
    EXEC sp_executesql @query = @sqlLoadPurgeLeaf
    SET @i = @@RowCount
    if @i != 0
    BEGIN
    SET @executePurge = N'EXEC stg.udp_' + @stagingName + '_Leaf @VersionName = ''' + @versionName + ''', @LogFlag = 1, @BatchTag = ' + @batchTagName
    EXEC sp_executesql @query = @executePurge
    SET @i = @@RowCount
    SET @sqlCheckErrorStaging = N'select ErrorCode from stg.' + @stagingName + '_Leaf where BatchTag = ' + @batchTagName + ' and ErrorCode != 0'
    EXEC sp_executesql @query = @sqlCheckErrorStaging
    SET @i = @@RowCount
    if @i != 0
    BEGIN
    SET @errorMsg = 'Error in purging leaf entity: ' + CONVERT(nvarchar,@id)
    RAISERROR (@errorMsg, 16, 1 )
    END
    END
    END
    FETCH NEXT FROM leaf_entity_cursor INTO @id, @name
    END TRY
    BEGIN CATCH
    SET @errorMsg = ERROR_MESSAGE()
    PRINT @errorMsg
    BREAK
    END CATCH
    -- Consolidated purge
    OPEN noleaf_entity_cursor
    FETCH NEXT FROM noleaf_entity_cursor INTO @id, @name
    WHILE @@FETCH_STATUS = 0
    BEGIN TRY
    if @name = @entityName or @entityName is null
    BEGIN
    SET @sqlGetEntityTable = N'select @tableName = HierarchyParentTable from mdm.tblEntity where ID = ' + CONVERT(nvarchar,@id)
    EXEC sp_executesql
    @query = @sqlGetEntityTable,
    @params = N'@tableName NVARCHAR(100) OUTPUT',
    @tableName = @tableName OUTPUT
    SET @sqlGetStagingBaseTable = N'select @stagingName = StagingBase from mdm.tblEntity where ID = ' + CONVERT(nvarchar,@id)
    EXEC sp_executesql
    @query = @sqlGetStagingBaseTable,
    @params = N'@stagingName NVARCHAR(100) OUTPUT',
    @stagingName = @stagingName OUTPUT
    SET @sqlRinseStaging = N'delete from stg.' + @stagingName + '_Consolidated where BatchTag = ' + @batchTagName
    EXEC sp_executesql @query = @sqlRinseStaging
    SET @i = @@RowCount
    SET @sqlLoadPurgeLeaf = N'insert into stg.' + @stagingName + '_Consolidated (ImportType, ImportStatus_ID, BatchTag, HierarchyName, Code) select 4, 0, ''AutoPurge'', ''' +@entityName + ''', Code from mdm.' + @tableName + ' where Status_ID = 2'
    EXEC sp_executesql @query = @sqlLoadPurgeLeaf
    SET @i = @@RowCount
    if @i != 0
    BEGIN
    SET @executePurge = N'EXEC stg.udp_' + @stagingName + '_Consolidated @VersionName = ''' + @versionName + ''', @LogFlag = 1, @BatchTag = ' + @batchTagName
    EXEC sp_executesql @query = @executePurge
    SET @i = @@RowCount
    SET @sqlCheckErrorStaging = N'select ErrorCode from stg.' + @stagingName + '_Consolidated where BatchTag = ' + @batchTagName + ' and ErrorCode != 0'
    EXEC sp_executesql @query = @sqlCheckErrorStaging
    SET @i = @@RowCount
    if @i != 0
    BEGIN
    SET @errorMsg = 'Error in purging consolidated entity: ' + CONVERT(nvarchar,@id)
    RAISERROR (@errorMsg, 16, 1 )
    END
    END
    SET @sqlGetCollectionTable = N'select @collectionTable = CollectionTable from mdm.tblEntity where ID = ' + CONVERT(nvarchar,@id)
    EXEC sp_executesql
    @query = @sqlGetCollectionTable,
    @params = N'@collectionTable NVARCHAR(100) OUTPUT',
    @collectionTable = @collectionTable OUTPUT
    SET @sqlGetCollectionMemberTable = N'select @collectionMemberTable = CollectionMemberTable from mdm.tblEntity where ID = ' + CONVERT(nvarchar,@id)
    EXEC sp_executesql
    @query = @sqlGetCollectionMemberTable,
    @params = N'@collectionMemberTable NVARCHAR(100) OUTPUT',
    @collectionMemberTable = @collectionMemberTable OUTPUT
    SET @sqlPurgeCollections = N'delete mdm.' + @collectionMemberTable + ' from mdm.' + @collectionTable + ' cn inner join mdm.' + @collectionMemberTable +
    ' cm on cm.Parent_CN_ID = cn.ID where cn.Status_ID = 2'
    EXEC sp_executesql @query = @sqlPurgeCollections
    SET @i = @@RowCount
    if @i != 0
    BEGIN
    SET @sqlPurgeCollections = N'delete from mdm.' + @collectionTable + ' where Status_ID = 2'
    EXEC sp_executesql @query = @sqlPurgeCollections
    SET @i = @@RowCount
    END
    END
    FETCH NEXT FROM noleaf_entity_cursor INTO @id, @name
    END TRY
    BEGIN CATCH
    SET @errorMsg = ERROR_MESSAGE()
    PRINT @errorMsg
    BREAK
    END CATCH
    CLOSE noleaf_entity_cursor
    DEALLOCATE noleaf_entity_cursor
    GO
    JAIS

  • 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

  • How to delete record from table control in BDC?

    Hello friends,
    I am running a BDC program to delete records.
    I have file with following records and i got these records into t_itab.
    Material     Plant     Start date     End date     Cost
    MQ100001     S001     09/01/2008     09/31/2008     55.00
    MQ100004     S002     09/01/2008     09/31/2008     56.00
    MQ100008     S003     09/01/2008     09/31/2008     57.00
    Now, I have BDC transaction in which table control screen which contains following structure.
    MQ100001     S001     09/01/2008     09/31/2008     55.00
    MQ100002     S002     09/01/2008     09/31/2008     56.00
    MQ100004     S003     09/01/2008     09/31/2008     47.00     
    MQ100005     S004     09/01/2008     09/31/2008     25.00
    MQ100006     S012     09/01/2008     09/31/2008     76.00
    MQ100007     S033     09/01/2008     09/31/2008     17.00
    MQ100008     S011     09/01/2008     09/31/2008     95.00
    MQ100009     S002     09/01/2008     09/31/2008     46.00
    I have recorded from SHDB in which first record will be delete.
    So, when i loop through t_itab,instead of deleting MQ100001,MQ100004 and MQ100008 from BDC screen,
    it is deleting MQ100001,MQ100002 and MQ100004 (first record for each process ).
    Which i don't want to.
    Is there any facility in BDC to put records on top which i want to delete?
    Please guide me.
    Regards,
    RH

    Hi,
    While doing recording check for Filter button available for the table control, if it available then do the recording for the same.
    Once it is done while passing the data from internal table put the value into Filter field.
    Hope it resolves your issue.
    Thanks & Regards.
    Nagaraj Kalbavi

  • How to delete record from table control using BDC?

    Hello friends,
    I am running a BDC program to delete records.
    I have file with following records and i got these records into t_itab.
    Material     Plant     Start date     End date     Cost
    MQ100001     S001     09/01/2008     09/31/2008     55.00
    MQ100004     S002     09/01/2008     09/31/2008     56.00
    MQ100008     S003     09/01/2008     09/31/2008     57.00
    Now, I have BDC transaction in which table control screen which contains following structure.
    MQ100001     S001     09/01/2008     09/31/2008     55.00
    MQ100002     S002     09/01/2008     09/31/2008     56.00
    MQ100004     S003     09/01/2008     09/31/2008     47.00     
    MQ100005     S004     09/01/2008     09/31/2008     25.00
    MQ100006     S012     09/01/2008     09/31/2008     76.00
    MQ100007     S033     09/01/2008     09/31/2008     17.00
    MQ100008     S011     09/01/2008     09/31/2008     95.00
    MQ100009     S002     09/01/2008     09/31/2008     46.00
    I have recorded from SHDB in which first record will be delete.
    So, when i loop through t_itab,instead of deleting MQ100001,MQ100004 and MQ100008 from BDC screen,
    it is deleting MQ100001,MQ100002 and MQ100004 (first record for each process ).
    Which i don't want to.
    Is there any facility in BDC to put records on top which i want to delete?
    Please guide me.
    Regards,
    RH

    One option is to identify the table and find out the location as the number of row which should be deleted from the table and then in the bdc program instead of postioning the cursor on the row 1(using the statement perform bdc_cursor ....(01)), replace the 01 with the row number.
    Second option is that if a filter control is available for the table control, then filter the data each and every time with the material number to be deleted and then delete the first row.
    Regards
    Farzan

  • How to Restore deleted records in other table in oracle database 10g...

    Hi All,
    i want to restore deleted records of a particular table in other table
    suppose:
    i perform a query
    delete from emp
    where deptno =30;
    now i wont to restore deptno=30 records in other table, let say in emp1 table
    can any one let me know how to do it?
    Thanks..

    This is what flashback query is for:
    orclz> conn scott/tiger
    Connected.
    orclz> select count(*) from emp;
      COUNT(*)
            14
    orclz> delete from emp where deptno=30;
    6 rows deleted.
    orclz> commit;
    Commit complete.
    orclz> create table deleted30 as select * from emp as of timestamp(systimestamp - 5/1440) where deptno=30;
    Table created.
    orclz> select count(*) from deleted30;
      COUNT(*)
             6
    orclz>

Maybe you are looking for

  • Is't possible to partionate the time capsule  ?

    i'd like to split the time capsule, so my wife and i can use it for time machine. We have one computer each

  • HELP ME ! I need to change my credicard data, because my card was stolen and the company give me another. H

    Good Afternoon, I live in Brazil and I have an account in Creative Cloud. How can I change my credicard data? My card was stolen and I blocked it. I have an anual account and I don't know how can i proceed. I need help.

  • Alternative Options for Email Merge?

    I need a way to do personalized (merged) e-mails. Usually, I have fewer than 500 recipients, but the e-mails need to include some merged data like the recipient's name and physical address (so just sticking 500 e-mail addresses in the BCC field Mail.

  • Firefox has crashed 45 times in 24 hrs.

    I am working from a brand new instal of Windows; also new video card, processor, hard drive. Last night Firefox was crashing every five minutes. It crashed once before I put on my addons, and multiple times after (somewhere around 30 times). I tried

  • Data not displaying inside cfdocument

    Hello, I am encountering a strange situation when trying to display data inside a <cfdocument> tag. I have constructed the code outside of a <cfdocument> tag and all displays as intended. However, when I insert the data inside a <cfdocument> tag and