`

【SQL】工作中的存储过程

阅读更多
工作中做了几个存储过程。
建立了几个零时表:
-- Create table
create table D_20190129
(
  billno     VARCHAR2(20),
  transtime  DATE,
  sap_status VARCHAR2(10),
  sap_note   VARCHAR2(100),
  sap_result VARCHAR2(100)
)
tablespace H2DB
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );


-- Create table
create table D_20190130
(
  billno     VARCHAR2(20),
  transtime  DATE,
  sap_status VARCHAR2(10),
  sap_note   VARCHAR2(100),
  sap_result VARCHAR2(100)
)
tablespace H2DB
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );



-- Create table
create table D_20190131
(
  memcardno   VARCHAR2(40) not null,
  busno       NUMBER(10) not null,
  saleamount  NUMBER(14,4) not null,
  realamount  NUMBER(14,4) not null,
  puramount   NUMBER(14,4) not null,
  integral    NUMBER(14,4) not null,
  integrala   NUMBER(14,4),
  cardholder  VARCHAR2(40),
  mobile      VARCHAR2(20),
  createtime  DATE not null,
  lasttime    DATE,
  integralsum NUMBER(16,4),
  cutmonth    VARCHAR2(8)
)
tablespace H2DB
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

-- Create table
create table D_20190132
(
  busno        VARCHAR2(10),
  busname      VARCHAR2(40),
  warecode     VARCHAR2(20),
  warename     VARCHAR2(80),
  wareqty      NUMBER,
  awaitqty     NUMBER,
  makeno       VARCHAR2(20),
  stallname    VARCHAR2(10),
  applyno      VARCHAR2(20),
  objbusno     VARCHAR2(20),
  dj_execdate  DATE,
  pro_execdate DATE,
  apptype      VARCHAR2(10),
  note         VARCHAR2(20)
)
tablespace H2DB
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );



对应的procedure。

create or replace procedure hydee_sap_check_qh
as
   cout integer := 0;
   cout1 integer := 0;
   cout2 integer := 0;
   cout3 integer := 0;
   status varchar2(1);
   note varchar2(100);
begin
  --首先查询t_20190129表中的数据是否已经被执行,即其请货单已经存在配送单情况
   for t in (select * from d_20190129) loop
       select count(1) into cout from t_distapply_h where billcode='APP' and compid=2 and status=1  and distno is null and applyno=t.billno;
       if cout=0
         then 
           delete from d_20190129 where billno=t.billno;
           commit;
       else
          DBMS_OUTPUT.put_line('ok');
       end if;
   end loop;  
-- 查询2019年1月1日到当前时间2天内的请货单没有配送单信息
 FOR x IN (select billno,transtime from T_SAP_QUEUE_R where tableobj='T_SAP_PO_CRE_R'and status=2 and billno in (select applyno from t_distapply_h where billcode='APP' and compid=2 and status=1 and execdate>to_date('2019-01-01','yyyy-mm-dd') and execdate<trunc(sysdate-2)  and distno is null)     order by transtime) LOOP
   --如果此单信息已经存在t_20190129表中则不进行处理,如有增加则插入
   SELECT count(1) into cout1 FROM t_distapply_h t_distapply_h WHERE billcode = 'APP' AND compid=2 and t_distapply_h.notes='从'||x.billno||'复制得来';
   if cout1=0
     then select count(1) into cout2 from d_20190129 where billno=x.billno;
     if cout2=0
       then insert into d_20190129(billno,transtime) values(x.billno,x.transtime);
            commit;
       else
         DBMS_OUTPUT.put_line('ok');
     end if;
     --查询sap下发中间表状态及信息并插入t_20190129表
     SELECT count(1) into cout3 FROM t_sap_queue a left join T_SAP_BILL_IN b on b.pkid=a.pkid WHERE  b.storebillno=x.billno;
     if cout3>0
       then select a.status,a.notes into status,note FROM t_sap_queue a left join T_SAP_BILL_IN b on b.pkid=a.pkid WHERE  b.storebillno=x.billno;
        update d_20190129 set sap_status=status,sap_note=note where billno=x.billno;
        commit;
     else
        DBMS_OUTPUT.put_line('ok');
     end if;
   else
     DBMS_OUTPUT.put_line('ok');
  end if;
 END LOOP;
 

end;


create or replace procedure hydee_sap_check_djdb
as
   cout integer := 0;
   cout1 integer := 0;
   cout2 integer := 0;
   status varchar2(1);
   note varchar2(100);
begin

    --首先查询t_20190130表中的数据是否已经被执行,即其请货单已经存在配送单情况
   for t in (select * from d_20190130) loop
      -- select count(1) into cout from t_distapply_h where billcode='APP' and compid=2 and status=1  and distno is null and applyno=t.billno;
       SELECT count(1) into cout FROM t_dist_h WHERE billcode = 'DSSM' AND compid=2 and status=0 and checkbit2=1 and checkbit3=0 and distno=t.billno  and lasttime>to_date('2019-01-01','yyyy-mm-dd');
       if cout=0
         then
           delete from d_20190130 where billno=t.billno;
           commit;
       else
          DBMS_OUTPUT.put_line('ok');
       end if;
   end loop;

   -- 查询及今年内店间调拨单sap没有执行的数据
   for x in ( SELECT distno,lasttime FROM t_dist_h WHERE billcode = 'DSSM' AND compid=2 and status=0 and checkbit2=1 and checkbit3=0 and lasttime>to_date('2019-01-01','yyyy-mm-dd')) loop
     --如果此单信息已经存在t_20190130表中则不进行处理,如有增加则插入 
     select count(1) into cout1 from d_20190130 where billno=x.distno;
     if cout1=0
       then insert into d_20190130(billno,transtime) values(x.distno,x.lasttime);
       commit;
     else
       DBMS_OUTPUT.put_line('订单已存在');
     end if;
     
        
     
     --查询sap下发中间表状态及信息并插入t_20190130表
     SELECT count(1) into cout2 FROM t_sap_queue a,T_SAP_BILL_IN b where a.tableobj='T_SAP_BILL_IN' and a.pkid=b.pkid and b.refbillno=x.distno;
     --select * from t_sap_queue a,T_SAP_BILL_IN b where a.tableobj='T_SAP_BILL_IN' and a.pkid=b.pkid and b.refbillno='店间调拨单号';
     if cout2>0
        then select distinct a.status,a.notes into status,note FROM t_sap_queue a,T_SAP_BILL_IN b where a.tableobj='T_SAP_BILL_IN' and a.pkid=b.pkid and b.refbillno=x.distno;
        update d_20190130 set sap_status=status,sap_note=note where billno=x.distno;
        commit;
     else
          DBMS_OUTPUT.put_line('ok');
     end if;
   end loop;


end;



create or replace procedure hydee_memcard_month_cut as
begin
  for u in (SELECT memcardno,
                   busno,
                   saleamount,
                   realamount,
                   puramount,
                   integral,
                   integrala,
                   cardholder,
                   mobile,
                   createtime,
                   lasttime,
                   integralsum,
                   to_char(add_months(trunc(sysdate), -1), 'yyyymm') as cutmonth
              FROM t_memcard_reg)

   loop
    insert into d_20190131
      (memcardno,
       busno,
       saleamount,
       realamount,
       puramount,
       integral,
       integrala,
       cardholder,
       mobile,
       createtime,
       lasttime,
       integralsum,
       cutmonth)
    values
      (u.memcardno,
       u.busno,
       u.saleamount,
       u.realamount,
       u.puramount,
       u.integral,
       u.integrala,
       u.cardholder,
       u.mobile,
       u.createtime,
       u.lasttime,
       u.integralsum,
       u.cutmonth);
    commit;
  end loop;

end;



create or replace procedure hydee_sap_check_dck as
       tc_count integer := 0;
       bsby_count integer := 0;
       djdb_count integer := 0;
       applyno varchar(20);--退仓单号
       objbusno varchar(10);
       dj_execdate date;
       abnormityno varchar(20);--报损报溢单号
       distno varchar(20);--店间调拨单单号
begin
  --清空记录表
  EXECUTE   IMMEDIATE 'truncate table d_20190132';
  --查询所有门店待出库商品及相关信息
  for c1 in (SELECT d.compid,d.busno,sz.zonename,vw.warecode,vw.warename,d.wareqty,d.awaitqty,i.makeno,ts.stallname stallname FROM t_store_d d JOIN t_ware vw
                ON d.wareid = vw.wareid AND d.compid = vw.compid LEFT JOIN t_stall ts ON d.stallno = ts.stallno AND d.compid = ts.compid AND d.busno = ts.busno
              LEFT JOIN s_zone sz ON ts.zoneno = sz.zoneno AND ts.compid = sz.compid LEFT JOIN t_store_i i ON d.compid = i.compid AND d.wareid = i.wareid
               AND d.batid = i.batid WHERE d.compid = 2  and awaitqty > 0 and length(d.busno) = 6) loop              
                    --首先查询退仓单是否存在
                     SELECT count(1) into tc_count FROM t_distapply_d JOIN t_distapply_h ON t_distapply_d.applyno = t_distapply_h.applyno LEFT JOIN v_ware_base
                     ON t_distapply_d.wareid = v_ware_base.wareid AND t_distapply_h.compid = v_ware_base.compid WHERE t_distapply_h.billcode = 'RAP'
                     AND t_distapply_h.compid = 2 and t_distapply_h.retdistno is null and (t_distapply_h.srcbusno = c1.busno and v_ware_base.warecode = c1.warecode and t_distapply_h.status = 1) and applyqty=c1.awaitqty and makeno=c1.makeno;
                     if tc_count=1
                         then
                             
                          SELECT t_distapply_d.applyno ,t_distapply_h.objbusno, t_distapply_h.lasttime into applyno, objbusno,dj_execdate FROM t_distapply_d JOIN t_distapply_h   ON t_distapply_d.applyno = t_distapply_h.applyno
                           LEFT JOIN v_ware_base    ON t_distapply_d.wareid = v_ware_base.wareid   AND t_distapply_h.compid = v_ware_base.compid WHERE t_distapply_h.billcode = 'RAP'
                           AND t_distapply_h.compid = 2 and t_distapply_h.retdistno is null   and (t_distapply_h.srcbusno = c1.busno and      v_ware_base.warecode = c1.warecode and t_distapply_h.status = 1) and applyqty=c1.awaitqty and makeno=c1.makeno;

                          insert into d_20190132(busno,busname,warecode,makeno,warename,wareqty,awaitqty,stallname,applyno,objbusno,apptype,dj_execdate,pro_execdate) values(c1.busno,c1.zonename,c1.warecode,c1.makeno,c1.warename,c1.wareqty,c1.awaitqty,c1.stallname,applyno
                          ,objbusno,'退仓单',dj_execdate,sysdate);
                          commit;
                         elsif tc_count>1
                           then
                           insert into d_20190132(busno,busname,warecode,makeno,warename,wareqty,awaitqty,stallname,apptype,note,pro_execdate) values(c1.busno,c1.zonename,c1.warecode,c1.makeno,c1.warename,c1.wareqty,c1.awaitqty,c1.stallname,'退仓单','需要手工执行',sysdate);
                           commit;
                         else
                           DBMS_OUTPUT.put_line('其他问题');
                         end if;
                      ---其次查询报损报溢单是否存在
                     SELECT count(1) into bsby_count  FROM t_abnormity_d t_abnormity_d  JOIN t_abnormity_h t_abnormity_h   ON t_abnormity_d.abnormityno = t_abnormity_h.abnormityno
                     and t_abnormity_h.billcode = 'ABN'  LEFT JOIN v_ware_base v_ware_base   ON t_abnormity_d.wareid = v_ware_base.wareid  AND t_abnormity_h.compid = v_ware_base.compid
                     left join s_busi s_busi    on t_abnormity_h.busno = s_busi.busno   and t_abnormity_h.compid = s_busi.compid WHERE  t_abnormity_h.compid = 2
                     and (v_ware_base.warecode = c1.warecode and  makeno=c1.makeno and wareqtyb=c1.awaitqty and s_busi.busno=c1.busno);
                     if bsby_count=1
                         then
                             SELECT t_abnormity_d.abnormityno,t_abnormity_h.lasttime into abnormityno,dj_execdate  FROM t_abnormity_d t_abnormity_d
                             JOIN t_abnormity_h t_abnormity_h    ON t_abnormity_d.abnormityno = t_abnormity_h.abnormityno   and t_abnormity_h.billcode = 'ABN'  LEFT JOIN v_ware_base v_ware_base
                             ON t_abnormity_d.wareid = v_ware_base.wareid   AND t_abnormity_h.compid = v_ware_base.compid  left join s_busi s_busi
                             on t_abnormity_h.busno = s_busi.busno   and t_abnormity_h.compid = s_busi.compid WHERE  t_abnormity_h.compid = 2
                              and (v_ware_base.warecode = c1.warecode and  makeno=c1.makeno and wareqtyb=c1.awaitqty and s_busi.busno=c1.busno);
                          insert into d_20190132(busno,busname,warecode,makeno,warename,wareqty,awaitqty,stallname,applyno,dj_execdate,apptype,pro_execdate) values(c1.busno,c1.zonename,c1.warecode,c1.makeno,c1.warename,c1.wareqty,c1.awaitqty,c1.stallname,abnormityno,dj_execdate,'报损报溢单',sysdate);
                          commit;
                         elsif bsby_count>1
                           then
                           insert into d_20190132(busno,busname,warecode,makeno,warename,wareqty,awaitqty,stallname,apptype,note,pro_execdate) values(c1.busno,c1.zonename,c1.warecode,c1.makeno,c1.warename,c1.wareqty,c1.awaitqty,c1.stallname,'报损报溢单','需要手工执行',sysdate);
                           commit;
                         else
                           DBMS_OUTPUT.put_line('其他问题');
                         end if;
                         
                         
                           ---最后查店间调拨单是否存在
                     SELECT count(1) into djdb_count FROM t_dist_d t_dist_d  JOIN t_dist_h t_dist_h   ON t_dist_d.distno = t_dist_h.distno and t_dist_h.billcode='DSSM'
                     LEFT JOIN v_ware_base t_ware    ON t_dist_d.wareid = t_ware.wareid AND t_dist_h.compid = t_ware.compid WHERE 
                      t_dist_h.compid=2 and (  t_dist_h.srcbusno =c1.busno and  t_ware.warecode = c1.warecode and  t_dist_h.status =0  and t_dist_d.makeno=c1.makeno and  t_dist_d.wareqty=c1.awaitqty);
                     if djdb_count=1
                         then
                              SELECT t_dist_h.distno, t_dist_h.objbusno, t_dist_h.lasttime into distno,objbusno,dj_execdate FROM t_dist_d t_dist_d  JOIN t_dist_h t_dist_h   ON t_dist_d.distno = t_dist_h.distno and t_dist_h.billcode='DSSM'
                     LEFT JOIN v_ware_base t_ware    ON t_dist_d.wareid = t_ware.wareid AND t_dist_h.compid = t_ware.compid WHERE 
                      t_dist_h.compid=2 and (  t_dist_h.srcbusno =c1.busno and  t_ware.warecode = c1.warecode and  t_dist_h.status =0  and t_dist_d.makeno=c1.makeno and  t_dist_d.wareqty=c1.awaitqty);
                          insert into d_20190132(busno,busname,warecode,makeno,warename,wareqty,awaitqty,stallname,applyno,objbusno,dj_execdate,apptype,pro_execdate) values(c1.busno,c1.zonename,c1.warecode,c1.makeno,c1.warename,c1.wareqty,c1.awaitqty,c1.stallname,distno,objbusno,dj_execdate,'店间调拨单',sysdate);
                          commit;
                         elsif djdb_count>1
                           then
                           insert into d_20190132(busno,busname,warecode,makeno,warename,wareqty,awaitqty,stallname,apptype,note,pro_execdate) values(c1.busno,c1.zonename,c1.warecode,c1.makeno,c1.warename,c1.wareqty,c1.awaitqty,c1.stallname,'店间调拨单','需要手工执行',sysdate);
                           commit;
                         else
                           DBMS_OUTPUT.put_line('其他问题');
                         end if;
                         
                         
  end loop;

end;
分享到:
评论

相关推荐

    SQL Server中存储过程比直接运行SQL语句慢的原因

    SQL Server 中存储过程比直接运行 SQL 语句慢的原因 在 SQL Server 中,存储过程比直接运行 SQL 语句慢的原因是 Parameter sniffing 问题。Parameter sniffing 是指 SQL Server 在执行存储过程时,使用参数的统计...

    在VB6.0中调用SQL Server的存储过程.pdf

    存储过程可以实现比单一SQL命令更加复杂的数据库操作,提供了封装对数据库重复性工作的一种方法。由于存储过程是一段程序,是对SQL命令的扩展,因此它可以实现更加复杂的数据库操作。 在SQL Server中,存储过程可以...

    SqlServer存储过程及调试指南

    7. 异常处理的详细机制:在TRY块中包含潜在失败的代码,如果在执行过程中发生错误,则流程转向CATCH块。在CATCH块中可以处理错误,并通过函数如ERROR_NUMBER()、ERROR_SEVERITY()、ERROR_STATE()、ERROR_PROCEDURE()...

    SQL_Server存储过程调试指南

    资源名称:SQL_Server存储过程调试指南内容简介: 存储过程( Stored Procedure)是一组为了完成特定功能的 SQL 语句集,经编译后存储在数据库中。用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来...

    vs中断点测试sqlserver的存储过程

    通过对上述步骤的详细解析,我们不仅了解了如何在VS中进行SQL Server存储过程的中断点测试,还掌握了整个调试过程中涉及的关键技术和操作流程。这一技能对于任何从事数据库应用开发的专业人士来说都是不可或缺的,它...

    SQLServer存储过程调用WebService

    ### SQL Server 存储过程中调用 WebService 的实现方法 #### 一、引言 在企业级应用开发中,Web Service 作为一种标准的接口技术,被广泛用于不同平台之间的服务交互。而在 SQL Server 数据库中直接调用 Web ...

    sql分页 sqlserver中存储过程分页

    存储过程是一种预先编译好的 SQL 代码块,存储在 SQL Server 数据库中。使用存储过程进行分页查询的好处包括:提高执行效率、增强代码复用性以及提供更好的安全性等。 #### 示例存储过程分析 下面将详细分析给定的...

    Sql Server 存储过程的导出导入.doc

    此外,如果存储过程中引用了源数据库特有的对象(如表、视图或函数),在导入到新环境中时可能需要调整依赖关系。 在实际应用中,存储过程的导出和导入非常实用,例如在数据库迁移、备份恢复、开发测试和版本控制等...

    SQL server存储过程习题,SQL触发器习题.rar

    6. **动态SQL**:存储过程中可以嵌入动态SQL语句,根据需要在运行时生成并执行SQL。 7. **返回值**:存储过程可以设置返回值,用`RETURN`语句传递结果给调用者。 **SQL触发器**: 1. **定义**:触发器是一种特殊的...

    SQL Server存储过程

    输出参数允许存储过程在执行过程中修改并返回值。实验中的`p11`存储过程接收四个参数:客户名称`@name1`,产品名称`@name1`,开始日期`@time1`,结束日期`@time2`,以及一个输出参数`@sum`,用于计算指定客户在指定...

    pl sql developer调试存储过程及调试包中创建的存储过程

    PL/SQL Developer 的调试功能可以帮助开发者快速地调试和解决存储过程中的错误,提高开发效率和代码质量。同时,PL/SQL Developer 还提供了创建存储过程的功能,帮助开发者快速地创建和管理存储过程。

    sql server 2008 存储过程与储发器 详解 书籍

    此外,还可以使用临时表和表变量在存储过程中暂存数据,提高处理效率。在优化方面,合理使用存储过程可以减少网络流量,但过度依赖存储过程也可能导致性能问题,因此需要根据实际情况权衡。 对于触发器,要特别注意...

    sql Server 通用分页存储过程

    sql Server 通用分页存储过程 sql Server 通用分页存储过程 sql Server 通用分页存储过程 sql Server 通用分页存储过程

    针对sqlserver 2008 存储过程通过With Encryption加密方式的解密

    SQL Server 2008 存储过程中使用 With Encryption 加密方式可以对存储过程进行加密保护,以防止未经授权的访问和修改。但是,在某些情况下,我们需要对加密的存储过程进行解密,以便进行维护、升级或 troubleshoot。...

    sqlserver存储过程解密工具

    SQL Server存储过程的加密通常是在开发或部署过程中,为了保护知识产权、防止未授权修改或者增加安全性而进行的。然而,在某些情况下,比如接手他人项目、排查问题或审计时,可能需要查看存储过程的原始代码。这时,...

    SQLServer存储过程转为oracle存储过程的工具

    可以将SQL Server存储过程转为oracle存储过程的工具

    SQLServer存储过程中事务的使用方法

    在SQL Server中,事务是确保数据一致性的重要机制,特别是在存储过程中。存储过程是一组预编译的SQL语句,可以在数据库中被多次调用,提高了效率并降低了网络流量。本篇将详细介绍如何在SQL Server存储过程中使用...

    解密SQL Server2000存储过程(可以选择存储过程查询)

    在存储过程中,可以通过条件判断来实现选择性查询。例如,可以使用`IF...ELSE`语句或者`CASE`表达式,根据参数值的不同执行不同的查询逻辑。 5. **存储过程的优势** - **性能优化**:存储过程预编译后,执行效率...

    C# winform调用SQL存储过程-菜鸟入门 详细注释

    内容概要:简单的C# winform调用存储过程实例,创建存储过程入参,通过SqlConnection对象和SqlCommand对象调用存储过程,获取存储过程的出参并显示出来,详细代码注释,希望对用到C#调用存储过程的小伙伴有帮助 ...

Global site tag (gtag.js) - Google Analytics