declare @mail nvarchar(200);
declare @note nvarchar(500);
declare c cursor --游标
for select email,note from cux_dls_notice_v where operatedate + ' '+ operatetime >= DATEADD(MINUTE,-60,GETDATE()) --取最近一小时的记录发送,计划任务是60分钟执行一次。
open c
fetch next from c into @mail,@note;
while @@FETCH_STATUS = 0
begin
EXEC msdb.dbo.sp_send_dbmail
@profile_name= '<账户名>', --定义好的sql server 邮箱账户名
@recipients=@mail, --需要发送的邮箱
@subject=@note, --邮件标题
@body=@note --邮件主题
fetch next from c into @mail,@note;
end
close c;
deallocate c;
';
--启用游标
declare c cursor for
--查询结果
select
a.email
,a.note
,@tableHTML+'
'+rfq_quotation_number+'
'
+'
'+lastname+'
'
+'
'+pl3+'
'
+'
'+customer+'
'
+'
'+disty_name+'
'
+'
'+snd_disty+'
'
+'
'+sold_to_customer+'
'
+'
'+fully_part_no+'
'
+'
'+currency+'
'
+'
'+volume+'
'
+'
'+requested_disty_cost+'
'
+'
'+cust_requested_price+'
'
+'
'+competitor+'
'
+'
'+competitor_part_no+'
'
+'
'+Competitor_Price+'
'
from
(
select
email
,note
,rfq_quotation_number
,lastname
,pl3
,客户中文+'/'+客户英文 as customer
,disty_name
,snd_disty
,sold_to_customer
,fully_part_no
,currency
,isnull(cast(volume as nvarchar(10)),'') volume
,isnull(cast(requested_disty_cost as varchar(10)),'') requested_disty_cost
,isnull(cast(cust_requested_price as varchar(10)),'') as cust_requested_price
,isnull(cast(competitor as varchar(100)),'') competitor
,isnull(cast(competitor_part_no as varchar(50)),'') competitor_part_no
,isnull(cast(competitor_price as varchar(10)),'') competitor_price
from cux_rfq_v
where currentnodetype = 1 and lastoperatedate + ' '+ lastoperatetime >= DATEADD(MINUTE,-60,GETDATE()) --找最近60分的记录,并发送
) a
open c
fetch next from c into
@mail
,@note
,@tableHTML;
while @@FETCH_STATUS = 0
begin
EXEC msdb.dbo.sp_send_dbmail
@profile_name= '<账户名>', --定义好的sql server 邮箱账户名
,@recipients=@mail
,@subject=@note
,@body= @tableHTML
,@body_format='HTML'
fetch next from c into
@mail
,@note
,@tableHTML;
end
close c;
deallocate c;