存储过程例子

       -- 会话级别临时表  Create Global Temporary Table tableanme (col1 type1, col2, type2) On Commit Preserve Rows;
       
       -- 创建事务临时表
       --Create Global Temporary Table transTempTable (oldid number(20),newid number(20),pid number(20)) On Commit Delete Rows;
       --select * from transTempTable;
       --drop table transTempTable;

declare
       maxIndex number(20);--存储最大ID
       tableCount number(20);--存储总条数
       fromId number(20);
       toId number(20);
       fromName varchar(30);
       toName varchar(30);
       fromCount number(20);
       toCount number(20);
       unifyFlag number(1);
       unifyName varchar(50);
       cursor pcCuesor is select * from placecomparecity;
begin
       DBMS_OUTPUT.PUT_LINE('Hello World!');
       -- 两个城市赋值
       fromId := 7;
       toId := 2;
       unifyFlag := 0;
       
       -- if else 用法
       if unifyFlag = 0 then
          unifyName := '非统一管理';
       --else if (unifyFlag = 1) then
          --unifyName := '统一管理';
       --else
          --unifyName := 'abc';
       end if;
       -- decode 用法:DECODE(value,if1,then1,if2,then2,if3,then3,...,else)
       select decode(unifyFlag,0,'非统一管理',1,'统一管理',2,'unify是2','都不是') into unifyName from dual;
       select name into fromName from city where id = fromId;
       select name into toName from city where id = toId;
       -- 查原来两个城市的条数
       select count(ap.id) into fromCount 
              from advdeploy ad, placecomparecity pc, city c, advplace ap
              where ad.compareid=pc.id and pc.cid =c.id and pc.pid=ap.id 
              and c.id = fromId and ad.isunify=unifyFlag;
       select count(ap.id) into toCount 
              from advdeploy ad, placecomparecity pc, city c, advplace ap
              where ad.compareid=pc.id and pc.cid =c.id and pc.pid=ap.id 
              and c.id = toId and ad.isunify=unifyFlag;
       dbms_output.put_line(unifyName||'的'||fromName||'共有'||fromCount||'条记录');
       dbms_output.put_line(unifyName||'的'||toName||'共有'||toCount||'条记录');
       select max(id) into maxIndex from advdeploy;
       select count(1) into tableCount from advdeploy;
       dbms_output.put_line('目前advdeploy表中最大ID是'||maxIndex||',共有'||tableCount||'条记录');
       dbms_output.put_line('要开始复制了========================================================');
       
       delete from transTempTable;
       
       -- 目标表的pc关系插入到临时表中
       insert into transTempTable (oldid, newid ,pid)
              select  pc.id,pc.id,pc.pid  from placecomparecity pc where pc.cid = fromId; 
       -- 更新compareid为目标城市对应的compareid
       for rec in pcCuesor loop
          update transTempTable tt set tt.newid=rec.id where tt.pid=rec.pid and rec.cid=toId;
       end loop;
       
       -- 将来源城市的数据插入到表中,ID自己增长
       insert into advdeploy (id,compareid,isunify,advid)
          -- 来源城市已有的
          select seqadvdeploy.nextval,ad.compareid,ad.isunify,ad.advid from advdeploy ad, placecomparecity pc, city c, advplace ap
          where ad.compareid=pc.id and pc.cid =c.id and pc.pid=ap.id and c.id = fromId and ad.isunify = unifyFlag
          -- 目的城市已有的就不用插了
           and ap.id not in (
          --  目的城市已有的
          select ap.id from advdeploy ad, placecomparecity pc, city c, advplace ap
          where ad.compareid=pc.id and pc.cid =c.id and pc.pid=ap.id and c.id = toId and ad.isunify = unifyFlag
          );
          
        -- 将表中新插入的数据中的compareid改为目标城市相关的
        declare
               cursor tempCursor is select * from transTempTable;
        begin
             for rec in tempCursor loop
                 update advdeploy ad set ad.compareid = rec.newid where ad.compareid = rec.oldid and ad.id > maxIndex;
             end loop;
        end;
       
       -- 查询复制后两个城市的条数
       select count(ap.id) into fromCount 
              from advdeploy ad, placecomparecity pc, city c, advplace ap
              where ad.compareid=pc.id and pc.cid =c.id and pc.pid=ap.id 
              and c.id = fromId and ad.isunify=unifyFlag;
       select count(ap.id) into toCount 
              from advdeploy ad, placecomparecity pc, city c, advplace ap
              where ad.compareid=pc.id and pc.cid =c.id and pc.pid=ap.id 
              and c.id = toId and ad.isunify=unifyFlag;
       dbms_output.put_line(unifyName||'的'||fromName||'共有'||fromCount||'条记录');
       dbms_output.put_line(unifyName||'的'||toName||'共有'||toCount||'条记录');
       select max(id) into maxIndex from advdeploy;
       select count(1) into tableCount from advdeploy;
       dbms_output.put_line('目前advdeploy表中最大ID是'||maxIndex||',共有'||tableCount||'条记录');
       dbms_output.put_line('复制结束了========================================================');
end;

你可能感兴趣的:(sql,C++,c,C#)