【问题标题】:Can I use variable value in email body when using UTL_mail package in oracle SQL在 oracle SQL 中使用 UTL_mail 包时,我可以在电子邮件正文中使用变量值吗
【发布时间】:2019-11-22 09:38:24
【问题描述】:

上下文: 我正在比较 2 个变量并在电子邮件正文中生成带有变量值的电子邮件

------------代码------------------

--Variable 1
select sum(sales) 
into V1_sales
from sales

--variable 2
selsct sum(sales)
into v2_sales
from yesretday_sales

if v2_sales > V1_sales
then 
EMAIL_ALERT
(p_mail_host => 'mailhost.abc.com',
p_from => 'sender.abc.com',
p_to => 'rec.abc.com',
p_subject => 'Sales of yesterday is greater than today'
p_message => 'Message: Today's sales which is'@v1_sales 'is lesser than yesretday's sales which is @v2_sales'
endif 


Explanation:
Trying to compare 2 variables and include the variable in the email body, I'm using UTL_MAIL in orcale SQL

【问题讨论】:

    标签: sql oracle oracle-sqldeveloper plsqldeveloper


    【解决方案1】:

    是的,你可以。

    我建议您创建一个变量,用于编写消息文本,并在调用过程时使用它。

    另外,如果你声明第三个变量(v3_sales),它也可以在消息中使用。

    类似这样的:

    declare
      v_message varchar2(500);
      v1_sales  number;
      v2_sales  number;
      v3_sales  number;
    begin
      ...
      v3_sales := v1_sales - v2_sales;
    
      v_message := 'Message: Today''s sales which is '           || v1_sales ||
                   'is lesser than yesterday''s sales which is ' || v2_sales ||
                   '. Difference is ' || v3_sales;
    
      email_alert (...,
                   p_message => v_message
                  );
    end;
    

    您编写的代码错误,因为使用了无效的单引号;如果在字符串中使用,则必须使用两个连续的引号。

    【讨论】:

    • 是的,我不知道使用管道来表示变量。我还可以将 2 个变量(即 v1_sales - v2_sales)之间的差异存储为新变量,比如 V3_Sales?
    • 双管道是连接运算符;它连接字符串,但是 - 如您所见 - 也可以使用变量。从第三个变量开始:当然,没问题。声明 v3_sales 并将结果放入其中。我已经编辑了我的答案;请看一下。
    • 哇,谢谢。抱歉,再澄清一下,如果我在新行中进行了 Yesday's sales : 12000 in a line,今天的销售额:10000 in a line 和差异:2000 in a line 如何在新行中执行?
    • 在你想换行的地方连接chr(10),例如|| v1_sales || chr(10) || ...
    • 对不起,我无法得到你所说的连接片段。试过了,但它仍然在同一个句子中
    猜你喜欢
    • 1970-01-01
    • 2013-01-11
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多