在 oracle SQL 中使用 UTL_mail 包时,我可以在电子邮件正文中使用变量值吗

Can I use variable value in email body when using UTL_mail package in oracle SQL

上下文: 我正在比较 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

是的,你可以。

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

此外,如果声明第三个变量(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;

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