2014-03-12 40 views
1

我试图运行一个执行SQL的shell scrpit。在SQL文件中,我有查询从数据库中检查某些东西,当其中一个检查失败时,它会向指定的id发送电子邮件警报。 我的代码是这样的:从oracle发送电子邮件时输入太长

Declare 
    numrows number(4,2); 
    c UTL_SMTP.CONNECTION; 


PROCEDURE send_header(name IN VARCHAR2, header IN VARCHAR2) AS 
    BEGIN 
    UTL_SMTP.WRITE_DATA(c, name || ': ' || header || UTL_TCP.CRLF); 
    END; 


Begin 

select count (*) into numrows 
<some sub queries> 
    Minus  
select count(*) 
<from something else> 
; 

--if differences are found, then trigger an email. 
IF numrows <> 0 THEN 
    BEGIN 
     c := UTL_SMTP.OPEN_CONNECTION('localhost',25); 
     UTL_SMTP.HELO(c, 'localhost'); 
     UTL_SMTP.MAIL(c, '[email protected]'); 
     UTL_SMTP.RCPT(c, '[email protected]'); 
     UTL_SMTP.OPEN_DATA(c); 
     send_header('From', '"noreply" <[email protected]>'); 
     send_header('To',  '"[email protected]'); 
     send_header('Subject', '<subject here>'); 
     UTL_SMTP.WRITE_DATA(c,'brief error message'|| chr(13)); 



     FOR I IN (
       <some logic>         
       )       

     LOOP 
      UTL_SMTP.WRITE_DATA(c, xyz || ' - '); 
      UTL_SMTP.WRITE_DATA(c, qwe); 
      UTL_SMTP.WRITE_DATA(c,UTL_TCP.CRLF); 
     END LOOP; 
     UTL_SMTP.WRITE_DATA(c, chr(13)||'-Sent by the batch process.'); 

     UTL_SMTP.CLOSE_DATA(c); 
     UTL_SMTP.QUIT(c); 
     EXCEPTION 
     WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN 
      BEGIN 
       UTL_SMTP.QUIT(c); 
       EXCEPTION 
       WHEN UTL_SMTP.TRANSIENT_ERROR OR UTL_SMTP.PERMANENT_ERROR THEN 
       NULL; -- When the SMTP server is down or unavailable, we don't have 
         -- a connection to the server. The QUIT call will raise an 
         -- exception that we can ignore. 
      END; 
     raise_application_error(-20000, 
       'Failed to send mail due to the following error: ' || sqlerrm); 

    END; 


END IF; 

END; 

现在我得到的错误是:

SP2-0027: Input is too long (> 2499 characters) - line ignored 
SQL> SP2-0734: unknown command beginning "ggg..." - rest of line ignored. 
SQL> SP2-0734: unknown command beginning "ddd..." - rest of line ignored. 
SQL> SP2-0734: unknown command beginning "eee..." - rest of line ignored. 
SQL> SP2-0734: unknown command beginning "nnn..." - rest of line ignored. 
SP2-0044: For a list of known commands enter HELP 
and to leave enter EXIT. 
SQL> SP2-0734: unknown command beginning "hfhf..." - rest of line ignored. 
SQL> Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production 
With the Partitioning, Oracle Label Security, OLAP, Data Mining 
and Real Application Testing options 

我抬起头来SP2-0027和我收集的输入线已经超过2499个字符。我的输入行没有这么多的字符,并且SQL文件本身的总字符数大约是4000.

我不知道是否有任何内部实现的UTL_SMTP在后台传入某些数据。或者如果有其他事情正在发生。

这在使用TOAD时运行完美。

有人可以帮忙吗?

+0

尝试用文本编辑器打开SQL文件(不是记事本,也许是textpad,不会打包的东西)并添加一些换行符。 – tbone

+0

@tbone我使用了Notepad ++,它没有显示空格或类似的东西。 –

+0

不,我说你可能有一个很长的SQL语句(没有换行符)。记事本经常包装,看起来并不那样。 – tbone

回答

0

我想我已经找到了答案:

我所要做的就是到shell脚本正在执行的SQL文件拆分成2个文件和volia`它的工作:d

@tbone :fyi &感谢您的帮助!

+0

没问题,很高兴帮助 – tbone