为什么日常工作被执行多次

时间:2018-06-01 08:10:10

标签: sql oracle

我创建了一个发送传递提醒电子邮件的程序。它将检查今天应该交付的PO并将项目列表发送给相应的员工。我还创建了一份工作,每天下午5点运行它。所以我认为它应该每天发送一次电子邮件。但实际上我每天总共收到6-7封邮件,电子邮件接收时间是从下午1:00到下午4:00。我不知道这是什么问题?我应该在哪里寻找原因?这是Oracle的SQL问题还是设置问题?我们使用了Oracle 11g数据库。任何帮助赞赏。感谢。

过程SQL 是:

CREATE OR REPLACE PROCEDURE SP_PO_INFO IS
  /******************************************************************************
     NAME:       SP_PRList_Assign
     REVISIONS:
     Ver        Date        Author               Description
     ---------  ----------  ---------------     ------------------------------------
     1.0        2011-09-08  Terry Sun            PR First assign Inform
  ******************************************************************************/
  TYPE M_VAR IS VARRAY(500) OF VARCHAR2(2000);
  M_BODY       M_VAR := M_VAR();
  L_OFFSET     NUMBER;
  M_SQLERRM    VARCHAR2(300);
  CONN         UTL_SMTP.CONNECTION;
  MAILSUBJECT  VARCHAR2(3000);
  MAILBODY     VARCHAR2(32767);
  MAIL_TO_REC  VARCHAR2(2000);
  MAIL_CC_REC  VARCHAR2(2000);
  MAIL_BCC_REC VARCHAR2(2000);
  L_CNT        INTEGER;
  L_TOTAL      INTEGER;

  --PR confirm completed no assign list
  CURSOR PO_INFO IS
    SELECT distinct TO_CHAR(PMN33, 'yyyy-mm-dd') PMN33,
       PMK12,

       GEN02,
       GEN06,
       PMN01,
       PMN04,
       PMN041,
       PMN07,
       PMN20,
       PMN24,
       PMMUD02
      FROM ZING.PMN_FILE, ZING.PMM_FILE,ZING.PMK_FILE, ZING.GEN_FILE, ZING.GEM_FILE
     WHERE PMN01 = PMM01
       AND PMN24 = PMK01
       AND PMK25 = '2'
       AND PMK12 = GEN01(+)
       and TO_CHAR(PMN33, 'yyyy-mm-dd')= TO_CHAR(SYSDATE, 'yyyy-mm-dd')
       and pmm25='2' ;

  --REceive E-mail Address
  CURSOR MAIL_ADD IS
   SELECT distinct TO_CHAR(PMN33, 'yyyy-mm-dd') PMN33,gen01,
       GEN02,
       GEN06
     FROM ZING.PMN_FILE, ZING.PMK_FILE, ZING.GEN_FILE, ZING.GEM_FILE
     WHERE PMN24 = PMK01
       AND PMK25 = '2'
       AND PMK12 = GEN01(+)
       and TO_CHAR(PMN33, 'yyyy-mm-dd')= TO_CHAR(SYSDATE, 'yyyy-mm-dd');

BEGIN
  ----------Total not assign PR---------------------------------
  SELECT COUNT(*)
    INTO L_TOTAL
    FROM ZING.PMM_FILE, ZING.PMN_FILE
   WHERE PMM01 = PMN01
     AND PMM18 = 'Y'
     AND PMM25 = '2'
     AND TO_CHAR(PMN33, 'yyyy-mm-dd')= TO_CHAR(SYSDATE, 'yyyy-mm-dd')
   ORDER BY PMM01, PMM02;

  -- if no information or the date in ('saturday','sunday') not send mail

  IF L_TOTAL = 0 OR TO_CHAR(SYSDATE, 'd') = '1' OR
     TO_CHAR(SYSDATE, 'd') = '7' THEN
    RETURN;
  ELSE
    FOR Y IN MAIL_ADD LOOP
    -- MAIL Subject
    MAILSUBJECT := 'ERP alarm';
    MAILBODY    := '<BR>' || 'Dear All :' || '<BR><BR>';
    MAILBODY    := MAILBODY || 'inventory will arrive today' ||'<BR>';
    MAILBODY    := MAILBODY || '<TABLE BORDER=1 CELLSPACING=0><TR>';

    -- lable column
  MAILBODY := MAILBODY || '<TD ALIGN=CENTER  BGCOLOR=C0C0C0>' || 'Item' ||
                '</TD>';
  MAILBODY := MAILBODY || '<TD ALIGN=CENTER  BGCOLOR=C0C0C0>' || 'Amount' ||
                '</TD>';
  MAILBODY := MAILBODY || '<TD ALIGN=CENTER BGCOLOR=C0C0C0>' || 'Date' ||
                '</TD></TR>'|| UTL_TCP.CRLF;

    L_CNT := 0;

  FOR X IN PO_INFO LOOP
  if    (x.pmk12=y.gen01) then
      M_BODY.EXTEND;
      L_CNT := L_CNT + 1;
      M_BODY(L_CNT) := '<TR><TD>' || X.PMN24 || '</TD>';
      M_BODY(L_CNT) := M_BODY(L_CNT) || '<TD>' || X.PMN01 || '</TD>';
      M_BODY(L_CNT) := M_BODY(L_CNT) || '<TD>' || X.PMN33 || '</TD>';
      M_BODY(L_CNT) := M_BODY(L_CNT) || '<TD>' || X.PMK12 || '</TD>';
      M_BODY(L_CNT) := M_BODY(L_CNT) || '<TD>' || X.GEN02 || '</TD>';
      M_BODY(L_CNT) := M_BODY(L_CNT) || '<TD>' || X.PMN04 || '</TD>';
      M_BODY(L_CNT) := M_BODY(L_CNT) || '<TD>' || X.PMN041 || '</TD>';
      M_BODY(L_CNT) := M_BODY(L_CNT) || '<TD>' || X.PMN07 || '</TD>';
      M_BODY(L_CNT) := M_BODY(L_CNT) || '<TD>' || X.PMN20 || '</TD>';
      M_BODY(L_CNT) := M_BODY(L_CNT) || '<TD>' ||
                       X.PMMUD02 || '</TD></TR>   ' || UTL_TCP.CRLF;
    end if;
    END LOOP;    

    -- last page
    M_BODY.EXTEND;
    L_CNT := L_CNT + 1;
    M_BODY(L_CNT) := '   </TABLE>' || UTL_TCP.CRLF;



  -- MAIL CONTENT
  CONN := UTL_SMTP.OPEN_CONNECTION('10.10.22.26', 25);
  UTL_SMTP.HELO(CONN, '10.10.22.26');
  UTL_SMTP.MAIL(CONN, 'ricky.luo@zitest.com');

  MAIL_BCC_REC := '';
  UTL_SMTP.RCPT(CONN, Y.GEN06);
  MAIL_BCC_REC := MAIL_BCC_REC || 'ricky.luo@zitest.com' || ',';
  MAIL_CC_REC :=  '';
  UTL_SMTP.OPEN_DATA(CONN);
  --      UTL_SMTP.WRITE_DATA(CONN, 'Date:' || TO_CHAR( SYSDATE, 'yyyy/mm/dd hh24:mi:ss' ) || utl_tcp.CRLF);   --This date will result in send mail time err!
  UTL_SMTP.WRITE_DATA(CONN,
                      'From:' || 'IT <ERP Administrator>' || UTL_TCP.CRLF);
  UTL_SMTP.WRITE_DATA(CONN,
                      'Subject: ' ||
                      UTL_ENCODE.MIMEHEADER_ENCODE(CONVERT(MAILSUBJECT,
                                                           'AL32UTF8')) ||
                      UTL_TCP.CRLF);
  UTL_SMTP.WRITE_DATA(CONN, 'TO: ' ||Y.GEN02 || UTL_TCP.CRLF);
  UTL_SMTP.WRITE_DATA(CONN, 'CC: ' || MAIL_CC_REC || UTL_TCP.CRLF);
  UTL_SMTP.WRITE_DATA(CONN, 'BCC: ' || MAIL_BCC_REC || UTL_TCP.CRLF);
  UTL_SMTP.WRITE_DATA(CONN, 'MIME-VERSION: 1.0' || UTL_TCP.CRLF);
  UTL_SMTP.WRITE_DATA(CONN,
                      'CONTENT-TYPE: TEXT/HTML;CHARSET=AL32UTF8;' ||
                      UTL_TCP.CRLF);

  UTL_SMTP.WRITE_RAW_DATA(CONN,
                          UTL_RAW.CAST_TO_RAW('' || UTL_TCP.CRLF ||
                                              MAILBODY));
  FOR L_OFFSET IN 1 .. L_CNT LOOP
    UTL_SMTP.WRITE_RAW_DATA(CONN,
                            UTL_RAW.CAST_TO_RAW('' || UTL_TCP.CRLF ||
                                                M_BODY(L_OFFSET)));
  END LOOP;

  UTL_SMTP.CLOSE_DATA(CONN);
  UTL_SMTP.QUIT(CONN);
  END loop;
  END IF;

EXCEPTION
  WHEN OTHERS THEN
    M_SQLERRM := SQLERRM;
    RAISE_APPLICATION_ERROR(-20001, M_SQLERRM);
    RAISE;
END;

工作是:

begin
  sys.dbms_job.submit(job => :job,
                      what => 'SP_PO_INFO;',
                      next_date => to_date('02-06-2018 06:56:13', 'dd-mm-yyyy hh24:mi:ss'),
                      interval => 'sysdate+1');
  commit;
end;

/

0 个答案:

没有答案