我的一个存储过程中,循环插入记录或者更新记录,是插入还是更新是用selct count(*) from 该表主键来判断,其他都正常,但是如果在整个过程中有相同记录插入,后面就无法用select count(*)判断出来,于是就出错,说后面违反约束条件,怎么解决这个问题。下面是完整的存储过程:create or replace procedure a_wjk(ieid in string,iuserid in string) as
cursor mycur(c_eid string) is
select a.gsid gsid,a.bid bid,a.bname,b.status status,b.iid iid,b.iname,b.sid sid,b.sname sname,b.zjly rid,nvl(c.je,0) from a_wjk_gs_ent a,a_wjk_gssz b,a_wjk_gssz_detail c where a.gsid=b.id(+) and a.eid=c_eid and a.ld=c.id(+) and a.gsid=c.gsid(+) order by a.gsid;
cursor mycur1(c_gsid number,c_ld number,c_eid string) is
select 0,a.sortid,b.value,a.id1,a.id2 from basicdata b,A_WJK_GS_ITEM a where a.itemtype=1 and a.gsid=c_gsid and b.year=2009 and b.stepid=2 and b.eid=c_eid and a.id=b.cid
union all select 0,sortid,name,id1,id2 from a_wjk_gs_item where itemtype=0 and gsid=c_gsid
union all select 0,a.sortid,to_char(b.a01+b.a02+b.a03+b.a04+b.a05+b.a06+b.a07+b.a08+b.a09+b.a10+b.a11+b.a12+b.a13+b.a14+b.a15+b.a16+b.a17+b.a18+b.a19+b.a20+b.a21+b.a22+b.a23+b.a24+b.a25+b.a26+b.a27+b.a28+b.a29+b.a30),a.id1,a.id2 from WAGE b,A_WJK_GS_ITEM a where a.itemtype=2 and a.isleaf=0 and a.gsid=c_gsid and b.year=2009 and b.stepid=2 and b.eid=c_eid and a.id1=b.wagetype
union all select 1,sortid,'',id1,id2 from a_wjk_gs_item where itemtype=2 and isleaf=1 and gsid=c_gsid
union all select 0,a.sortid,to_char(b.num),a.id1,a.id2 from A_WJK_GS_ITEM a,A_WJK_GS_STANDARD b where a.itemtype=3 and a.gsid=c_gsid and b.year=2009 and a.id1=b.id1 and a.id2=b.id2
union all select 0,sortid,to_char(c_ld),id1,id2 from a_wjk_gs_item where itemtype=4 and gsid=c_gsid
order by sortid;
v_gsid number(5);
v_bid varchar2(50);
v_bname varchar2(300);
v_status int;
v_iid varchar2(50);
v_iname varchar2(300);
v_sid varchar2(30);
v_sname varchar2(300);
v_rid varchar2(50);
v_ld number(10,2);
v_str varchar(500);
v_flag int;
v_sortid int;
v_je varchar2(200);
v_tmp varchar2(100);
v_id1 int;
v_id2 int;
v_l int;
v_hj number(16,2);
BEGIN
  open mycur(ieid);
  loop
    fetch mycur into v_gsid,v_bid,v_bname,v_status,v_iid,v_iname,v_sid,v_sname,v_rid,v_ld;
    exit when mycur%notfound;
    open mycur1(v_gsid,v_ld,ieid);
    v_je:='';
    loop
      fetch mycur1 into v_flag,v_sortid,v_tmp,v_id1,v_id2;
      exit when mycur1%notfound;
      if v_tmp is null then
         v_tmp:='';
      end if;
      if v_flag=0 then
         v_je:=v_je||v_tmp;
       else
          begin
            v_str:='select to_char(A'||substr('0'||to_char(v_id2),length('0'||to_char(v_id2)) -1,2)||') from wage where year=2009 and wagetype='||to_char(v_id1)||' and stepid=2 and eid='||chr(39)||ieid||chr(39);
            execute immediate v_str into v_tmp;
            exception
            when others then
            v_tmp:='0';
          end ;
         v_je:=v_je||v_tmp;
       end if;
    end loop;
    close mycur1;
    begin
      execute immediate 'select '||v_je||' from bookset where year=0' into v_hj;
      exception
      when others then
      insert into a_wjk_gs_log(UTIME, EID, GSID, ERRORTYPE, UPDATETYPE, SQLSTRING) values(sysdate,ieid,v_gsid,6,0,v_je);
      goto LB1;
    end;
    select count(*) into v_l from payoutdata where year=2009 and setid=1 and eid=ieid and bid=v_bid and iid=v_iid and sid=v_sid and stepid=2 and rid=v_rid; --第二次如果有相同的记录的话就无法检索出来了,所有又一次用了insert语句
    if v_l>0 then
       update payoutdata set money=v_hj where eid=ieid and stepid=2 and setid=1 and year=2009 and bid=v_bid and sid=v_sid and rid=v_rid and iid=v_iid;    else
       insert into payoutdata(SETID, "YEAR", EID, BID, IID, SID, RID, STEPID, USERID, MONEY, ISLOCK, BNAME, INAME, SNAME) values(1,2009,ieid,v_bid,v_iid,v_sid,v_rid,3,iuserid,v_hj,0,v_bname,v_iname,v_sname);
    end if;
    insert into a_wjk_gs_log(UTIME, EID, GSID, ERRORTYPE, UPDATETYPE, SQLSTRING) values(sysdate,ieid,v_gsid,0,0,v_je);
    <<LB1>>
    null;
  end loop;
  close mycur;
END ;