PL / SQL:如何以特定方式删除记录,例如,如果存在特定类型X的记录,则删除除一条记录之外的所有记录

时间:2014-03-12 11:50:57

标签: plsql oracle11g oracle10g

我正在尝试创建一个PL / SQL过程,我删除了由游标分组和选择的记录,但我只想要剩下一个记录。我想首先通过Xcomment删除,如果有多个id_number,activity_code,start_dt,activity_participation_code存在的条目,则删除除空白/空xcomment之外的所有条目。如果有多个带有空白xcomment的条目,则删除除空白table_nmb之外的所有条目。如果多个条目具有空白table_nmb,则删除最高序列,直到只剩下一个。基本上,我只希望每个字段都有一条记录。我很难想到如何做到这一点,所以任何帮助都会受到赞赏。

到目前为止,这是我的代码:

Create Or Replace Function Y_Cleanup_Cursor
  Return Sys_Refcursor
As
  My_Cursor Sys_Refcursor;
Begin
  Open My_Cursor For 
    Select Q.Id_Number, Q.Activity_Code, Q.Start_Dt, Q.Activity_Participation_Code, Q.Rec_Count, A.Xcomment, A.Table_Nmb, A.Xsequence
     From (Select Id_Number, Activity_Code, Start_Dt, Activity_Participation_Code, Count(0) As Rec_Count
        From Activity A
        Group By Id_Number, Activity_Code, Start_Dt, Activity_Participation_Code
        Having Count(0) > 1) Q, 
        Activity A
        Where 
            Q.Id_Number = A.Id_Number And 
            Q.Activity_Code = A.Activity_Code And 
            Q.Start_Dt = A.Start_Dt And  
            Q.Activity_Participation_Code = A.Activity_Participation_Code;
  Return My_Cursor;
End Y_Cleanup_Cursor;


Create Or Replace Procedure Help_Me_Please(Code In Varchar2)
Is
--  Declare Variables
--  I Stands For Internal Variable

    L_Cursor                        Sys_Refcursor;

    I_Id_Number                     Varchar2(10 Byte);
    I_Xsequence                     Number  (6); 
    I_Activity_Code                 Varchar2(05 Byte); 
    I_Start_Dt                      Varchar2(08 Byte); 
    I_Activity_Participation_Code   Varchar2(02 Byte); 
    I_Table_Nmb                     Varchar2(15 Byte); 
    I_Xcomment                      Varchar2(255 Byte);
    I_Rec_Count                     Number  (6);

    L_Counter                       Integer;

Begin

    L_Cursor := Y_Cleanup_Cursor;

    Loop
      Fetch L_Cursor Into 
        I_Id_Number, I_Activity_Code, I_Start_Dt, I_Activity_Participation_Code, I_Rec_Count, I_Xcomment, I_Table_Nmb, I_Xsequence;

        Select Count (Id_Number)
        Into L_Counter
        From Activity Where
                                  Id_Number = I_Id_Number 
            And               Activity_Code = I_Activity_Code
            And                    Start_Dt = I_Start_Dt 
            And Activity_Participation_Code = I_Activity_Participation_Code
            And Trim(Xcomment) Is Null;

     If L_Counter <> I_Rec_Count Then
        Begin
            Delete From Activity
            Where 
                                  Id_Number = I_Id_Number 
            And               Activity_Code = I_Activity_Code
            And                    Start_Dt = I_Start_Dt 
            And Activity_Participation_Code = I_Activity_Participation_Code
            And Trim(Xcomment) Is Null;
        end;
     End If;
        Exit When L_Cursor%Notfound;
    End Loop;

    Close L_Cursor;  
End Help_Me_Please;

1 个答案:

答案 0 :(得分:0)

从我收集的内容中,您要删除除重复列之外的所有行

首先确保备份你的桌子:

create table [backup_table] as select * from [table];

试试这个:

DELETE FROM backup_table
WHERE rowid not in
(SELECT MIN(rowid)
FROM backup_table
GROUP BY [col1], [col2]);

Col1和col2等是应该相同的列