Help in delete statement

Hi all,
We have the following:
create table tt1 (c1 varchar2(10), c2 varchar2(10));
insert into tt1 values('AA','11');
insert into tt1 values('AA','11');
insert into tt1 values('AA','11');
insert into tt1 values('AA','11');
insert into tt1 values('AA','11');
insert into tt1 values('BB','22');
insert into tt1 values('BB','22');
insert into tt1 values('BB','22');
insert into tt1 values('BB','22');I want to delete repeating rows... So output should be:
AA 11
BB 22
and other rows to be deleted. We don't have uniqueness.
Any ideas?
Version: 10g
Thanks in advance,
Bahchevanov.

There are a number of methods:
AskTom: Deleting duplicate records
AskTom: Deleting duplicate records without using rowid and rownum
AskTom: to remove duplicate records

Similar Messages

  • Help on delete statement

    hi
    I need to maintain records in a table1 only latest 2 records .
    remaining i nee to delete
    can any one help me out in delete statement
    Table1 contains below columns
    empno deptno lastupdatedate

    The below query will delete all the rows except the 2 latest record -
    DELETE TABLE1
    WHERE (EMPNO, DEPTNO, LASTUPDATEDATE) NOT IN (
    SELECT EMPNO, DEPTNO, LASTUPDATEDATE
    FROM (SELECT EMPNO, DEPTNO, LASTUPDATEDATE
    FROM TABLE1
    ORDER BY LASTUPDATEDATE DESC)
    WHERE ROWNUM <= 2);
    There is similar thread already answered for this. You can refer the below link -
    Performing Top-N Analysis

  • Help needed to build delete statement

    i need to keep last 10 days data+ last day of everymonth data in my table.
    this table will grow on daily basis.
    ex:
    data_date no.of records
    2006/02/28 10000
    2006/03/31 11000
    2006/04/30 12000
    2006/05/31 13000
    2006/06/09     13100
    2006/06/10 13200
    2006/06/11 13300
    2006/06/12 13400
    2006/06/13     13500
    2006/06/14     13600
    2006/06/15 13700
    2006/06/16 13800
    2006/06/17     13900
    2006/06/18     14000
    means last 10 days + last day of evermonth data need to store and remaining all
    records i need to delete from this table.
    could you help me to build this delete statement in single statement.

    select * from mytbl;
    DD                 NR
    28-02-2006      12000
    31-03-2006      10000
    30-04-2006      12050
    31-05-2006       9500
    06-06-2006      13100
    07-06-2006      13200
    08-06-2006      13300
    09-06-2006      13400
    10-06-2006      13500
    11-06-2006      13600
    12-06-2006      13700
    13-06-2006      13800
    14-06-2006      13900
    15-06-2006      14000
    16-06-2006      14100
    17-06-2006      14200
    18-06-2006      14300
    17 rows selected.
    delete from mytbl
    where not
    ( dd = last_day( dd ) -- Keep last days of months
       or  dd >= trunc(sysdate) - 10 -- Keep last 10 days
    3 rows deleted.
    select * from mytbl;
    DD                 NR
    28-02-2006      12000
    31-03-2006      10000
    30-04-2006      12050
    31-05-2006       9500
    09-06-2006      13400
    10-06-2006      13500
    11-06-2006      13600
    12-06-2006      13700
    13-06-2006      13800
    14-06-2006      13900
    15-06-2006      14000
    16-06-2006      14100
    17-06-2006      14200
    18-06-2006      14300To me...it looks like it does work!

  • Delete Statement Exception Handling

    Hi guys,
    I have a problem in my procedure. There are 3 parameters that I am passing into the procedure. I am matching these parameters to those in the table to delete one record at a time.
    For example if I would like to delete the record with the values ('900682',3,'29-JUL-2008') as parameters, it deletes the record from the table but then again when I execute it with the same parameters it should show me an error message but it again says 'Deleted the Transcript Request.....' Can you please help me with this?
    PROCEDURE p_delete_szptpsr_1 (p_shttran_id IN saturn.shttran.shttran_id%TYPE,
    p_shttran_seq_no IN saturn.shttran.shttran_seq_no%TYPE,
    p_shttran_request_date IN saturn.shttran.shttran_request_date%TYPE) IS
    BEGIN
    DELETE FROM saturn.shttran
    WHERE shttran.shttran_id = p_shttran_id
    and shttran.shttran_seq_no = p_shttran_seq_no
    and trunc(shttran_request_date) = trunc(p_shttran_request_date);
    DBMS_OUTPUT.PUT_LINE('Deleted the Transcript Request Seq No (' || p_shttran_seq_no || ') of the Student (' || p_shttran_id ||') for the requested date of (' || p_shttran_request_date ||')');
    COMMIT;
    EXCEPTION WHEN NO_DATA_FOUND THEN
    DBMS_OUTPUT.PUT_LINE('Error: The supplied Notre Dame Student ID = (' || p_shttran_id ||
    '), Transcript Request No = (' || p_shttran_seq_no || '), Request Date = (' || p_shttran_request_date || ') was not found.');
    END p_delete_szptpsr_1;
    Should I have a SELECT statement to use NO_DATA_FOUND ???

    A DELETE statement that deletes no rows (just like an UPDATE statement that updates no rows) is not an error to Oracle. Oracle won't throw any exception.
    If you want your code to throw an exception, you'll need to write that logic. You could throw a NO_DATA_FOUND exception yourself, i.e.
    IF( SQL%ROWCOUNT = 0 )
    THEN
      RAISE no_data_found;
    END IF;If you are just going to catch the exception, though, you could just embed whatever code you would use to handle the exception in your IF statement, i.e.
    IF( SQL%ROWCOUNT = 0 )
    THEN
      <<do something about the exception>>
    END IF;In your original code, your exception handler is just a DBMS_OUTPUT statement. That is incredibly dangerous in real production code. You are relying on the fact that the client has enabled output, that the client has allocated a large enough buffer, that the user is going to see the message, and that the procedure will never be called from any piece of code that would ever care if it succeeded or failed. There are vanishingly few situations where those are safe things to rely on.
    Justin

  • Delete statement is not working.

    Hi,
    find the code. Here the delete statement is not working and i am getting sy-subrc = 4. although ,
    xe1edp10-idnkd = 34596 and dint_edidd -sdata = 34596.
    please help me ...
    loop at dekek_x.
    loop at dint_edidd where segnam = 'E1EDP10'.
    if dekek_x-stpin = 1 .
    CLEAR xe1edp10.
      MOVE dint_edidd-sdata TO xe1edp10.
    delete dint_edidd where sdata = xe1edp10-idnkd.
    endif.
    endloop.

    1st thing..
    i tried this :
    tables: edidd, e1edp10.
    edidd-sdata = '315934 EA 017'.
    WRITE edidd-sdata.
    move edidd-sdata to e1edp10.
    IF edidd-sdata = e1edp10-idnkd.
    WRITE: e1edp10-idnkd.
    else.
      WRITE: 'nothing'.
    ENDIF.
    output
    >315934 EA 017
    >315934 EA 017
    2nd thing,.
    your loop inside loop doesnt make any sense as they are not related any where.
    3rd thing:
    the fields are not type compatible.. this might be the reason for wrong delete statement..
    and 1 more clarification:
    TABLES: edidd, e1edp10.
    DATA :it TYPE TABLE OF edidd WITH HEADER LINE.
    edidd-sdata = '315934 EA 017'.
    WRITE edidd-sdata.
    APPEND edidd TO it.
    edidd-sdata = '315934 EA 018'.
    APPEND edidd TO it.
    LOOP AT it." where segnam = 'E1EDP10'.
      CLEAR e1edp10.
      MOVE it-sdata TO e1edp10.
      DELETE it WHERE sdata = e1edp10-idnkd.
    ENDLOOP.
    in this also delete is working perfectly fine... you run and check..

  • Looping delete statement where table name is coming from another table

    Hi All,
    We have to write code to delete records from active tables of WDSO.We have 5 DSO  so inspite of writing delete statement for each table we want to put all table names into one table and then loop through it .but we are getting error when we are refering that table field which has active table name.error is :
    "dictionary structure or table is either not active or does not exist "
    As per my understanding in a delete /select /insert /update statement we need to put table name (whose field we are refering ) it can't be replaced by a variable .
    ex: v_table = 'EMPLOYEE' .
    DELETE FROMv_table WHERE EMP_NAME = 'ABDC' .
    is wrong and it must be like
    ex : DELETE FROM EMPLOYEE WHERE EMP_NAME = 'ABDC' .
    but we want to make our code dynamic .
    Can you please suggest a way so that we can read the table names from another table and delete data based on some selection fom those tables .
    I tried variants ,perform etc and even searched FM for the same but not found a solution .Your help will be greatly appreciated .
    Thanks in advance .
    Regards,
    Jaya

    Hi,
    You can change your statement as follows:
    DELETE FROM (v_table) WHERE EMP_NAME = 'ABDC' .
    However, I would not recommend this. There is a standard function module RSAN_ODS_DATA_DELETE which allows selective deletion - that should be a safer way to do this. You can

  • Need help in deleting multiple tables

    Hi,
    In one of my scenario i want to delete One record from the table, but there are some child and sub child exist for the table. And the on delete cascade is not set fro any of the table.
    Structure of my table,
    Table-1
    tab1_pk
    tab1_name
    Table-2
    tab2_pk
    tab1_pk
    Table-3
    tab3_pk
    tab2_pk
    i want to delete the record from Table-1 and all the related record of Table-2 and Table-3 in a single query.
    Plz help

    Dear 786725,
    What has just came to my mind at first sight is that you can create a trigger for your approach. I don't think that a single query will delete some rows from more than one table.
    Please go to the http://tahiti.oracle.com , select your database version and search for the after trigger. Hint: You may need to use the FOR EACH ROW clause and you again may need to use :old :new parameters in the trigger to have the information of what has just been deleted from the relevant table.
    Hope That Helps.
    Ogan
    Edited by: Ogan Ozdogan on 05.Ağu.2010 19:00
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_7004.htm#SQLRF01405
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b31695/dialogs.htm#sthref454
    4.24 Create Trigger
    The following information applies to a trigger, which is which is a stored PL/SQL block associated with a table, a schema, or the database, or an anonymous PL/SQL block or a call to a procedure implemented in PL/SQL or Java. The trigger is automatically executed when the specified conditions occur.
    Schema: Database schema in which to create the trigger.
    Name: Name of the trigger. Must be unique within the database.
    Add New Source in Lowercase: If this option is checked, new text is entered in lowercase regardless of the case in which you type it. This option affects only the appearance of the code, because PL/SQL is not case sensitive in its execution.
    Trigger tab
    Trigger Type: The type of object on which to create the trigger: TABLE, VIEW, SCHEMA, or DATABASE. (The remaining items depend on the type of trigger.)
    Table Owner or View Owner: For a trigger on a table or a view, the name of the owner of the table or the view.
    Table Name or View Name : For a trigger on a table or a view, the name of the table or the view.
    Before or After: For a trigger on a table, select Before to cause the database to fire the trigger before executing the triggering event, or select After to cause the database to fire the trigger after executing the triggering event.
    Statement Level or Row Level: For a trigger on a table, Statement Level fires the trigger once before or after the triggering statement that meets the optional trigger constraint defined in the WHEN condition; Row Level fires the trigger once for each row that is affected by the triggering statement and that meets the optional trigger constraint defined in the WHEN condition.
    Insert, Update, Delete: For a trigger on a table or a view, Insert fires the trigger whenever an INSERT statement adds a row to a table or adds an element to a nested table; Update fires fire the trigger whenever an UPDATE statement changes a value in one of the columns specified in Selected Columns (or in any column if no columns are specified); Delete fires the trigger whenever a DELETE statement removes a row from the table or removes an element from a nested table.
    Referencing - Old: For a trigger on a table, the correlation names in the PL/SQL block and WHEN condition of a row trigger to refer specifically to old value of the current row.
    Referencing - New: For a trigger on a table, the correlation names in the PL/SQL block and WHEN condition of a row trigger to refer specifically to new value of the current row.
    Available Columns: For a trigger on a table, lists the columns from which you can select for use in an Update trigger definition.
    Selected Columns: For a trigger on a table, lists the columns used in an Update trigger definition.
    When: For a trigger on a table, an optional trigger condition, which is a SQL condition that must be satisfied for the database to fire the trigger. This condition must contain correlation names and cannot contain a query.
    Schema: For a trigger on a schema, the name of the schema on which to create the trigger.
    Available Events: For a trigger on a schema or database, lists events from which you can select for use in the trigger definition.
    Selected Events: For a trigger on a schema or database, lists events used in the trigger definition.
    DDL tab
    This tab contains a read-only display of a SQL statement that reflects the current definition of the trigger.
    "

  • DELETE STATEMENT IN ECC6.0

    HI,
    How to use the DELETE statement in ECC6.0.
    when i am using this statement , it is going to short dump.
    Delete itab from wtab.
    pls let me know
    i am getting the error ,
    convert the charater to numerics, only numerics are supported in at the argument position , bcos it is a unicode program.
    let me know pls .
    regards,

    hi,
    chk the syntax:
    DELETE - itab_lines
    Syntax
    ... itab [FROM idx1] [TO idx2] [WHERE log_exp]... .
    Extras:
    1. ... FROM idx1
    2. ... TO idx2
    3. ... WHERE log_exp
    Effect
    To delete several lines at once, you have to specify at least one of the additions FROM, TO, or WHERE. You can only use the additions FROM and TO with standard tables and sorted tables.
    If you specify several of the additions, the rows are deleted that result from the intersection of the individual additions.
    Addition 1
    ... FROM idx1
    Effect
    If you specify FROM, all the table rows from the table index idx1 onwards are included. idx1 must be a data object with type i. If the value of idx1 is less than or equal to 0, a runtime error occurs. If the value is greater than the number of table rows, no rows are deleted.
    Addition 2
    ... TO idx2
    Effect
    If you specify TO, only the table rows up to table index idx2 are included. idx2 must be a data object with type i. If the values of idx2 is less than or equal to 0, a runtime error occurs. If the value is greater than the number of table rows, it is set to the number of rows. If idx2 is less than idx1, no rows are deleted.
    Addition 3
    ... WHERE log_exp
    Effect
    You can specify any logical expression log_exp after WHERE, for which the first operand of each individual comparison is a component of the internal table. This enables all logical expression with the exception of IS ASSIGNED, ISREQUESTED, and IS SUPPLIED. The dynamic specification of components using character-type data objects in parentheses is not supported here. All rows for which the logical expression is true are deleted.
    hope this helps u,
    keerthi

  • HR_INFOTYPE_OPERATION - Need help w/Deleting infotype records

    Hello all,
    We have an issue where infotype records got created where the ENDDA is less than the BEGDA (ENDDA = 06/30/2006 and BEGDA = 07/01/2006).  We are trying to delete these using HR_INFOTYPE_OPERATION but we are getting message PG-009 (No data stored for 9001 in the selected period) returned in our tests. 
    BTW, 9001 is one of our customer infotypes. 
    I know I can remove these records using a direct SQL statement, but I would prefer to remove them more gracefully if possible.  Is there a way around this error?  I actually think function module HR_READ_INFOTYPE is raising the error.
    Any suggestions would be greatly appreciated.
    Thanks,
    Al

    You can check report RHRHDC00, I think it can help you with custom infotype too.
    Otherwise you might need to delete the records forcefully using DELETE statement, which is not suggested since all the  standard FM consider BEGDA and ENDDA and in your case they will always return blank.
    You can also check FM -
    HR_ECM_DELETE_INFOTYPE
    Regards,
    Amit
    Reward all helpful replies.

  • Creating delete statements out of insert statements

    Hi, I'm running an insert script with lots of columns. Before that script is run I want to run a delete script for those records that match the ones in the insert.
    The things is that I have generated all these insert scripts and I do not want to manually create delete statments for all these records (there are many...)
    So for an insert into like this:
    Insert into ANSWERS(A,B,C,X,Y,Z) values ('Yes',No','No','No',null,'Yes')
    I want to create a delete somewhat like this:
    Delete from ANSWERS where (A,B,C,X,Y,Z) = ('Yes',No','No','No',null,'Yes')
    This way I can simple use a find and replace to do this task. Is there anyway to write a delete statement somewhat like this?
    Thanks!

    A very unusual request.
    the things is that I have generated all these insert scriptsWhy can't you generate a script to delete in a similar manner.
    Is there anyway to write a delete statement somewhat like this?something like this may help you but remember that this script will fail when you have columns values as "NULL"
    SQL> create table answers(a varchar2(3), b varchar2(3), c varchar2(3), x varchar2(3) , y varchar2(3),
      2  z varchar2(3));
    Table created.
    SQL> Insert into ANSWERS(A,B,C,X,Y,Z) values ('Yes','No','No','No','Yes','Yes');
    1 row created.
    SQL> select substr(txt, 1, length(txt)-1) || ' from dual)' from (
      2  select
      3  replace(replace(replace(
      4  trim('Insert into ANSWERS(A,B,C,X,Y,Z) values (''Yes'',''No'',''No'',''No'',''Yes'',''Yes'')'),
      5           'Insert into', 'Delete From'), '(A', ' where (A'), 'values (', ' in (select ') txt
      6  from dual);
    SUBSTR(TXT,1,LENGTH(TXT)-1)||'FROMDUAL)'
    Delete From ANSWERS where (A,B,C,X,Y,Z)  in (select 'Yes','No','No','No','Yes','Yes' from dual)
    SQL> Delete From ANSWERS where (A,B,C,X,Y,Z)  in (select 'Yes','No','No','No','Yes','Yes' from dual);
    1 row deleted.
    SQL>

  • The DELETE statement conflicted with the REFERENCE constraint "FK_Equipment_Assets". The conflict occurred in database "WorkOrderSystem", table "dbo.Equipment", column 'AssetCode'........can someone give me a solution for this Error.

    The DELETE statement conflicted with the REFERENCE constraint "FK_Equipment_Assets". The conflict occurred in database "WorkOrderSystem", table "dbo.Equipment", column 'AssetCode'

    ya that's true row is referenced by another table.....but i want the row to referred by another table ,I don't know how to fix it
    Actually this is not a C# issue. It is a T-SQl issue.
    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/home?forum=transactsql
    The above link should be posted.
    Per my understanding, first you should
    delete the record from the Foreign Table and then after you have to delete the record from the Primary Key table
    Hope it helps you.
    Best regards,
    Kristin
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Prallel query and Delete statements

    Hi Gurus, need your help in understanding parallel execution
    We are noticing that delete statements are executing very slowly when parallel query is forced. Is this expected?
    All the literature that I read says that parallel query has no impact on DML statements. Yet, the query plan on the delete statement shows that it will be executed in parallel mode. This could mean that the scan portion is happening in parallel but the delete operation itself is happening in serial, correct?
    I tested in various servers and multiple tables before posting my question here. They all seem to show consistent results. Delete statements are twice slower when parallel query is forced. The same happens when parallel degree is set in table definition.
    For your information, we are running 10g on windows server with 15 million rows in table, 5 million rows being deleted with the statement. There is one index on the table and it doesn’t match the columns in query. Query plan shows full table scan. Table is not portioned.
    Thanks for your help in advance

    Parallel DML is supported by Oracle. Obviously when enabled, it can impact a DML statement.. (what literature have you read that said otherwise?)
    The delete operation itself is done in parallel using rowid ranges (e.g. each PQ slave process does a distinct physical "piece" of the table).
    Parallel DML should typically speed up the process. Why? Because I/O itself has latency. The process needs to wait (idle CPU time) for the I/O operation to complete before continuing.
    So let's assume the process can only do a 100 deletes per second. The actual I/O channel is capable of a 1000 I/O's per second. But due to inherant latency, the "max delete speed limit" for a procces is a 100 I/O's per second. The full capacity of the I/O channel is thus not used (and cannot be used by a single process).
    Parallel Query enables more processes to do I/O in order to utilise this "max speed limit".
    Why would you see a degradation in performance? It could be due to overutilising the I/O channels (attempting to go faster than the speed limit so to say).
    It could be due to some other contention in Oracle or even the o/s. You will need to investigate the wait state and events of the PQ processes to try and determine the probable cause.

  • Delete statements very slow

    hi,
    We have a performance issue the delete statements on the database are running very slow they are taking about 12 hours.
    the delete statements are like
    DELETE table_name
    WHERE column1 = 'temp'
    AND col2 LIKE 'A%'
    AND col3 = 0;
    that table has an composite index with 5 columns and the columns specified in the delete statements are in the order
    column1 position 4
    col2 position 3
    col3 position 5
    no additional indexes are present on the table

    As already suggested get an execution plan for the deletes to see what it is doing to make informed decisions about what to do.
    Until then ...
    You said you have one index on 5 columns
    WHERE column1 = 'temp'
    AND col2 LIKE 'A%'
    AND col3 = 0;
    that table has an composite index with 5 columns and the columns specified in the delete statements are in the order
    column1 position 4
    col2 position 3
    col3 position 5Oracle might be doing a skip scan on the index or a full table scan - it is possible neither is efficient for your operation. An index ordered by # of unique values (most unique first) on column1, col2, col3 might help performance on this delete if a small percentage of rows are being deleted (anywhere from 5% to maybe 30%, it depends). If a large percentage of rows are being deleted then a full table scan might be better.
    If you have the license and the database is set up for it parallel DML is a possiblity - check the on-line documentation to see how to do the delete in parallel.

  • Regarding delete statement.

    Hi guys,
    could anybody help me in this.
    There are 5 records in this internal table. (1,2,3,4,5)
    loop at itab.
        check itab-number = '3'.
          delete table itab.
           write: itab-number.
         endloop.
    Here the out put is: 3.
    what  'DELETE'  statement will do here.

    Hi,
    Note that itab is a table with Headerline. and itab means headerline and itab[] is the table.
    In your case DELETE statements has removed that row or record from the table.
    But it is still there in the Headerline <b>itab</b> so it gets displayed with WRITE.
    But the table will not have that record.
    Check by executing this code.
    LOOP AT ITAB.
    WRITE: itab-number.
    ENDLOOP.
    <b>You can use CLEAR ITAB. after DELETE statement to clear the headerline.</b>
    Regards,
    Sesh

  • FORALL bulk delete statement it requires lot of time

    Hi,
    when I execute FORALL bulk delete statement it requires lot of time even for 10 rows deletion.so how to avoid this problem? I checked SGA_TARGET and PGA_TARGET cureent_size and max_size are same for both. Is their is memory problem?
    thanks in advance
    Vaibhav

    Please look at this:
    http://psoug.org/definition/LIMIT.htm
    If you add the LIMIT 100 part to your query your bulk collect / forall construction will process 100 records per part.
    This will avoid filling up undo tablespaces / PGA memory.
    There's also a discussion wether to use LIMIT 100 or LIMIT 1000 (or greater).
    It depends on what your system can handle, if it's a busy production statement I'd stick to a value of 100.
    Reason for this, is that your query will not take a huge amount of PGA.
    DECLARE
    TYPE t_id_tab IS TABLE OF test.c1%TYPE;
    l_id_tab t_id_tab := t_id_tab();
    Begin
    dbms_output.put_line(DBMS_UTILITY.get_time);
    select c1 bulk collect into l_id_tab
    from TEST where c1<=150000
    LIMIT 100;
    FORALL i IN l_id_tab.first .. l_id_tab.last
    delete from TEST where c1= l_id_tab(i);
    commit;
    dbms_output.put_line(DBMS_UTILITY.get_time);
    End;we use this construction to load data into our warehouse and bumped into the LIMIT 100 part while testing.
    It did speed up things significantly.
    Another (risky!) trick is to do:
    -- exclude table from archivelogging
    ALTER TABLE TEST NOLOGGING;
    -- execute plsql block
    <your code comes here>
    -- enable logging on the table
    ALTER TABLE TEST LOGGING;The risky part is that you get inconsistent archivelogging this way because your deletes arent logged.
    When you do a restore from backup and want to roll forward until the latest change in your archive logs, you'll miss these deletes and your table will not be consistent with the rest of your database.
    In our warehouse situation we dont mind, data gets refreshed every night.
    Hope this helps!
    Robin

Maybe you are looking for