0

我有一个检查是否有新代码的程序。如果有,则将它们插入表中。并将新数据保存到 csv 或 txt 文件中并通过电子邮件发送给我。

我无法了解如何将新数据重定向到文件,甚至无法将数据作为简单文本放入电子邮件中。谢谢

create or replace
PROCEDURE new_codes_test( today_date IN VARCHAR2 DEFAULT NULL,  v_proc_return OUT NUMBER) AS
sql_str         VARCHAR2(4000);
.....

BEGIN
    v_start_time    := SYSDATE;
    v_proc_return   := 0;
   ....
      INSERT INTO NEW_CODES_test
      SELECT DISTINCT   Sy_ID ,P_CODE, SYSDATE
      FROM X.B
      WHERE NOT EXISTS (SELECT DISTINCT Sy_ID, P_CODE 
                        FROM X.C
                        WHERE today = today_date) ;

     COMMIT;

 --SELECT ___ into ___ from X.B;

sql_str := 'UTL_MAIL.send(sender       => ''
                         ,recipients   => ''
                         ,cc           => ''
                         ,subject      => 'New codes'
                         ,MESSAGE      => '' )';

 --EXECUTE IMMEDIATE sql_str;
 p_proc_return     :=  v_proc_return;

EXCEPTIONS
  ....
  END;
4

1 回答 1

1

要写入文件,UTL_FILE包会派上用场。要编写电子邮件,您需要将要发送的文本放入某种字符串中,然后再将其传递给 UTL_MAIL.SEND 的 MESSAGE 参数。您还需要确保在您的服务器上安装并设置了 UTL_MAIL;请参阅此常见问题解答

因此,以下内容可能有用:

CREATE OR REPLACE FUNCTION NEW_CODES_TEST(today_date IN VARCHAR2)
  RETURN NUMBER
AS
  strMessage  VARCHAR2(32767);
  nRows       NUMBER := 0;
  fHandle     UTL_FILE.FILE_TYPE;
BEGIN
  v_start_time    := SYSDATE;

  fHandle := UTL_FILE.FOPEN(someDirectory, someFilename, 'w');

  FOR aRow IN (SELECT DISTINCT SY_ID ,P_CODE
                 FROM X.B
                 WHERE NOT EXISTS (SELECT DISTINCT Sy_ID, P_CODE 
                                     FROM X.C
                                     WHERE today = today_date)
  LOOP
    INSERT INTO NEW_CODES_test
      VALUES (aRow.SY_ID, aRow.P_CODE, SYSDATE);

    UTL_FILE.PUT_LINE(fHandle, aRow.SY_ID || ', ' || aRow.P_CODE);

    strMessage := strMessage || 'Added ' || aRow.SY_ID || ', ' ||
                                aRow.P_CODE || CHR(10);

    nRows := nRows + 1;
  END LOOP;

  COMMIT;

  UTL_FILE.FCLOSE(fHandle);

  UTL_MAIL.SEND(sender     => 'me@mycompany.com',
                recipients => 'you@someplaceelse.net',
                subject    => 'New codes',
                message    => strMessage);

  RETURN 0;
EXCEPTIONS
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
    RETURN SQLCODE;
END NEW_CODES_TEST;

分享和享受。

于 2014-05-02T22:00:46.753 回答