plsql用loop循環帶日期參數的存儲過程


自己寫了 個查詢數據收入的前200名,建個新表dppaim200,然后逐條往表里插入數據,過程如下:

create or replace procedure p_xunhuan(qsrq dppaim200.dqrq%type,jsrq dppaim200.dqrq%type) is   --日期參數格式取表里的日期格式,默認是輸入
v_sum number :=1;                                                                                                                                 --定義數字類型的變量,初始值1,用做排名循環
begin
  delete from dppaim200;                                                                                                                        --插入之前先清空表數據,一天執行一次就不必要
  commit;
  loop                                                                                                                                                       --進入loop循環
    insert into dppaim200                                                                                                                        
    select to_char(sysdate, 'yyyy/mm/dd') dqrq,
                   t.jglgdid,
                   t.jglbarcode,
                   t.jglcname,
                   t.jglcatid,
                   w.catcname,
                   t.sl,
                   t.sr,
                   t.ml,
                   round(decode(t.sr, 0, null, t.ml / t.sr), 4) * 100 || '%' mll,
                   round(decode(t.sl, 0, null, t.sr / t.sl), 1) sj,
                   t.e paim
              from (SELECT round(sum(sglxssr / 0.81)) sr,
                           jglgdid,
                           jglbarcode,
                           jglcname,
                           jglcatid,
                           round(sum(sglsl)) sl,
                           round(sum(hsml / 0.81)) ml,
                           row_number() OVER(ORDER BY round(sum(sglxssr / 0.81)) desc) e
                      FROM jxcgoodslist_day
                     where to_char(jgldate, 'yyyymmdd') >= qsrq                                                                --定義起始日期輸入參數
                       and to_char(jgldate, 'yyyymmdd') <= jsrq                                                                   --定義結束日期輸入參數
                       and substr(jglcatid, 1, 6) not in
                           (022003, 011608, 011391)
                       and jglcname not like '%聯營%'         
                     group by jglgdid, jglbarcode, jglcname, jglcatid) t,
                   goodscat w
             where t.e = v_sum                                                                                                                --排名等於變量為1的記錄插入表
               and t.jglcatid = w.catcode
             order by t.e;
    v_sum := v_sum + 1;                                                                                                                     --變量自增1,進入下個循環插入排名為2 的記錄                                                                                                       
    exit when v_sum > 200;                                                                                                                 --當變量大於200時,退出循環
  end loop;                                                                                                                                          --結束循環
  commit;                                                                                                                                            --提交更新記錄
end p_xunhuan;

在命令窗口執行:

 

 執行成功。

查詢結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM