发送通知的函数

huangapple go评论75阅读模式
英文:

function to send notification

问题

我想在每位员工的合同到期前三个月发送一条消息。

我尝试这样做:

从员工表中选择雇佣日期

然后将其与sysdate进行比较,如果雇佣日期比合同结束日期早3个月,则发送通知。

英文:

I would like to send a message to each employee three months before the end of their contract

I try to do this as:

select hire_date from the emp table 

and compare it with sysdate then if hire date was less than the contract end date with 3 months then send notificatin end if

答案1

得分: 0

这不应该是一个函数,而应该是一个过程。例如:

create or replace procedure p_end_of_contract_mail is
begin
  for cur_r in (select e_mail
                from emp
                where months_between(trunc(sysdate), end_of_contract_date) = 3
               ) 
  loop
  apex_mail.send(
    p_to   => cur_r.e_mail,
    p_from => 'noreply@my_application.com',
    p_body => 'Your contract ends in 3 months',
    p_subj => 'End of contract');
    
  apex_mail.push_queue;
end;
/

你应该调度它,以便每天运行,并在必要时自动发送邮件,例如:

begin
   dbms_scheduler.create_job ( 
    job_name        => 'end_of_contract', 
    job_type        => 'PLSQL_BLOCK', 
    job_action      => 'p_end_of_contract_mail;',
    start_date      => SYSTIMESTAMP, 
    enabled         => true, 
    repeat_interval => 'FREQ=DAILY; INTERVAL=1'
   ); 
end;
/
英文:

That shouldn't be a function, but a procedure. For example:

create or replace procedure p_end_of_contract_mail is
begin
  for cur_r in (select e_mail
                from emp
                where months_between(trunc(sysdate), end_of_contract_date) = 3
               ) 
  loop
  apex_mail.send(
    p_to   => cur_r.e_mail,
    p_from => 'noreply@my_application.com',
    p_body => 'Your contract ends in 3 months',
    p_subj => 'End of contract');
    
  apex_mail.push_queue;
end;
/

You'd schedule it so that it runs every day and automatically sends mail whenever necessary, e.g.

begin
   dbms_scheduler.create_job ( 
    job_name        => 'end_of_contract', 
    job_type        => 'PLSQL_BLOCK', 
    job_action      => 'p_end_of_contract_mail;',
    start_date      => SYSTIMESTAMP, 
    enabled         => true, 
    repeat_interval => 'FREQ=DAILY; INTERVAL=1'
   ); 
end;
/

huangapple
  • 本文由 发表于 2023年7月11日 03:47:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定