oracle 存储过程 游标

贴出个人工作中写的几个存储过程:

 一 删除数据库中重复数据,保留一条

create or replace procedure proc5 as
begin

    -- 游标cur  查询出记录中 主键id  和 重复记录列
    declare cursor cur is select t.mission_id,t.task_name from sst_mission t where t.project_name='114登录扫描第三轮测试';

    --传入重复记录列参数,查找出重复记录条数
    cursor cur2(tName sst_mission.task_name%type) is select count(*) as c from sst_mission where sst_mission.task_name = tName and sst_mission.project_name='114登录扫描第三轮测试';
    missionId sst_mission.mission_id%type;
    taskName sst_mission.task_name%type;
    begin
     if cur%isopen = false then

     --打开游标
     open cur;
     end if;
       --遍历游标,赋值给变量

     loop
        fetch cur into missionId,taskName;

     --循环遍历,删除数据量大于1的纪录
          for countNum in cur2(taskName)
          loop
            if countNum.c>1 then
              delete from sst_execute where sst_execute.mission_id = missionId;
              delete from sst_mission where sst_mission.task_name = taskName and sst_mission.mission_id = missionId;    
            end if;
          end loop;

        --游标未提取到数据为true退出 循环
        exit when cur%notfound;
      end loop;


     end;
end proc5;

你可能感兴趣的:(oracle,数据库,工作,测试,delete,存储)