码迷,mamicode.com
首页 > 编程语言 > 详细

插入时排序

时间:2015-07-20 18:35:52      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

CREATE OR REPLACE PROCEDURE ASSIGN_package_prize(arr in varchar_array, oldtypeid in varchar_array, CID NUMBER,numArr in varchar_array,FLAG NUMBER,ServerCount NUMBER, RETVAL OUT VARCHAR2) IS
  PRIZECODE  VARCHAR2(255);
   servernum number;
   servernumc VARCHAR2(2);
   prizetypenum number(6);
   prizetypenumc VARCHAR2(6);
   hashcoded VARCHAR2(8);
   flaged  number(2);
   forInt number(2);
   type type_array is table of number(6) index by binary_integer;
   var_array type_array;
BEGIN

IF (FLAG = 3) THEN
  --修改奖品状态
  DECLARE CURSOR c1 IS
      SELECT a.prizetypeid, a.prizecode FROM mkt_campaignprize a WHERE a.campaignid=CID AND a.issend=0;
  BEGIN
      FOR r1 IN c1 LOOP
         UPDATE mkt_prize b SET b.prizestate = 0 WHERE b.prizetypeid = r1.prizetypeid AND b.prizecode = r1.prizecode;
      END LOOP;
  END;
  --更新奖品库存数量字段
  DECLARE CURSOR c2 IS
      SELECT distinct(a.prizetypeid) FROM mkt_campaignprize a WHERE a.campaignid=CID AND a.issend=0;
  BEGIN
      FOR r2 IN c2 LOOP
         UPDATE mkt_prizetype b SET b.stockprizecount =
                (select count(*) from mkt_prize p where p.prizetypeid=r2.prizetypeid and p.prizestate=0)
                where b.prizetypeid=r2.prizetypeid;
      END LOOP;
  END;
  --删除campaignprize
  DELETE FROM mkt_campaignprize c WHERE c.campaignid=CID AND c.ISSEND = 0;

ELSE
  IF (FLAG = 1) THEN
    for i IN oldtypeid.first .. oldtypeid.last loop
          UPDATE MKT_PRIZE SET PRIZESTATE = 0 WHERE PRIZECODE IN (SELECT N.PRIZECODE FROM MKT_CAMPAIGNPRIZE N WHERE N.CAMPAIGNID=CID
            AND N.ISSEND = 0 AND N.PRIZETYPEID = oldtypeid(i)) AND PRIZETYPEID=oldtypeid(i);
          DELETE FROM MKT_CAMPAIGNPRIZE M WHERE M.CAMPAIGNID=CID AND M.ISSEND = 0 AND M.PRIZETYPEID=oldtypeid(i);

     end loop;
     DELETE FROM MKT_CAMPAIGNPACKAGE Y WHERE Y.CAMPAIGNID=CID;
  END IF;

 --获取上次插入时最后一条记录在那台服务器上
    servernum:=0;
    flaged:=0;
     select count(1) into flaged  from MKT_CAMPAIGNPRIZE where  issend=0 and rownum<2 order by campaignprizeid desc;
     if flaged>0 then
       select  to_number( substr(HASHCODE,0,2)) into servernum  from MKT_CAMPAIGNPRIZE where  issend=0 and rownum<2 order by campaignprizeid desc;
         if servernum is null then
           servernum:=0;
         end if;
      end if;

 --开始循环插入操作
  for k IN arr.first .. arr.last LOOP
      prizetypenum:=0;
      flaged:=0;
      for forInt in 0 ..ServerCount-1 loop
        prizetypenum:=0;
         servernumc:=lpad(forInt,2,‘0‘);
      select  count(1) into flaged from MKT_CAMPAIGNPRIZE where hashcode like ‘‘||servernumc||‘%‘ and prizetypeid=arr(k) and CAMPAIGNID=CID and rownum<=1 order by campaignprizeid asc;
        if flaged>0 then
            with cet as(select  *  from MKT_CAMPAIGNPRIZE where hashcode like ‘‘||servernumc||‘%‘ and prizetypeid=arr(k) and CAMPAIGNID=CID   order by campaignprizeid desc  ) select hashcode into hashcoded from cet where  rownum<=1;
            if hashcoded is not null  then
             prizetypenum:=to_number(substr(hashcoded,3,6));
             prizetypenum:=prizetypenum+1;
             end if;
           end if;
           var_array(forInt):=prizetypenum;
          end loop;
     --插入到活动礼品表操作

          --
          DECLARE CURSOR c3 IS
          SELECT T.PRIZECODE from MKT_PRIZE T WHERE T.PRIZETYPEID=arr(k) AND T.PRIZESTATE = 0 AND ROWNUM <= numArr(k);
          BEGIN
             FOR r3 IN c3 LOOP
                servernum:=servernum+1;
                if servernum=ServerCount then
                 servernum:=0;
                 end if;
                 servernumc:= lpad(servernum,2,‘0‘);
                 prizetypenumc:=lpad(var_array(servernum),6,‘0‘);
                 hashcoded:= servernumc||prizetypenumc;
                  var_array(servernum):= var_array(servernum)+1;
                INSERT INTO MKT_CAMPAIGNPRIZE  values( MKT_CAMPAIGNPRIZE_S.NEXTVAL,SYSDATE,NULL,SYSDATE,NULL,CID,NULL,0,r3.PRIZECODE,0,arr(k),NULL,hashcoded );
              END LOOP;
            END;
          --

       UPDATE MKT_PRIZE SET PRIZESTATE = 1
        WHERE PRIZECODE IN (SELECT C.PRIZECODE FROM MKT_CAMPAIGNPRIZE C WHERE C.CAMPAIGNID=CID AND C.PRIZETYPEID=arr(k)) AND PRIZETYPEID=arr(k);
  end loop;
END IF;

 COMMIT;
  RETVAL := ‘1‘;
  Exception
     When others then
       RETVAL :=SUBSTR(SQLERRM, 1, 600);
       Rollback;

END ASSIGN_package_prize;

 

插入时排序

标签:

原文地址:http://www.cnblogs.com/linbl/p/4661857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!