如何用SQLMail建立一个电子刊物自动处理系统?
时间:2010-06-13 14:36:00
如何用SQLMail建立一个电子刊物自动处理系统?
下面我们用SQLMail来做一个电子刊物自动处理系统。在这个系统中,主要实现两个功能:
1、每当新一期电子刊物编好,追加到发行数据库publication中,SQLServer会自动定期执行my_publish存储过程。在这个存储过程中,它会检查发行数据库,当发现有新的记录时,便通过逐个从订阅数据库sub_info(订阅者信息数据库)中取出订阅人的邮件地址的办法,发送给每名订阅者。
2、电子刊物的订阅与取消。SQLServer同样定期执行my_subscibe存储过程,检查特定邮箱中的所有邮件,如果发现标题为subscribe的邮件,就提取发件人等相关信息,在订阅数据库中追加一条新记录;同样,如果存在标题为stopsubscribe的邮件,就将相关的记录从订阅数据库中删除。
该系统中具体用到的数据库和存储过程如下所示:
sub_info
/*订阅者信息数据库*/
emailvarchar(20)NOTNULL订阅者电子邮件地址
sub_datedatetimeNOTNULL订阅时间
othertextNULL订阅者的其他信息
publication(电子刊物出版数据库)
字段名称类型允许空值含义
pub_classchar(10)NOTNULL电子刊物期号
pub_datedatetimeNULL出版日期
titletextNOTNULL本期电子刊物标题
contenttextNOTNULL电子刊物正文
endnotetextNULL附加于刊物的其他信息
flagsmallintNULL1,标志为未出版的新刊物
my_subscribe,用于处理订阅者信息的存储过程。
CREATEPROCEDUREmy_subscribe
AS
declare@msg_idvarchar(64)
declare@subjectvarchar(255)
declare@messagevarchar(255)
declare@originatorvarchar(255)
declare@datevarchar(255)
declare@statusint
declare@mapifailureint
select@mapifailure=0
while(1=1)
begin
/*查找邮件并获取消息ID*/
exec@status=master..
xp_findnextmsg@msg_id=@msg_idOUTPUT
if@status<>0
begin
select@mapifailure=1
break
end
if@msg_idisnullbreak
/*读取邮件的信息到变量中*/
exec@status=master..xp_readmail
@msg_id=@msg_id,
@originator=@originatorOUTPUT,
@subject=@subjectOUTPUT,
@message=@messageOUTPUT,
@date_received=@dateOUTPUT
if@status<>0
begin
select@mapifailure=1
break
end
/*根据邮件标题,在订阅数据库中添加或删除记录*/
execxp_deletemail@msg_id=@msg_id
if@subject='subscribe'
insertintosqlmailsample..
sub_infovalues(@originator,@date,@message)
/*sub_info字段名称类型允许空值含义*/
else
if@subject='stopsubscribe'
deletefromsqlmailsample..sub_infowhereemail=@originator
end/*循环结束*/
if@mapifailure=1
/*错误处理代码*/
else
return(0)
GO
my_publish
/*用于分发电子刊物的存储过程*/
CREATEPROCEDUREmy_publish
AS
declare@recipientvarchar(255)
declare@subjectvarchar(255)
declare@contentvarchar(255)
declare@endnotevarchar(255)
declare@statusint
select@subject=pub_classfrom
sqlmailsample..publicationwhereflag=1
/*如果有新的记录(电子刊物)则开始分发*/
if(@subjectisnotnull)
begin
/*将数据库记录取出,经处理后存放到变量中*/
select@subject=convert(varchar
(255),title)+'('+pub_class+')',
@content=convert(varchar(255),content)
+convert(varchar(255),endnote)
fromsqlmailsample..publication
whereflag=1
updatesqlmailsample..publication
setflag=0whereflag=1
declarecurcursorforselect
emailfromsqlmailsample..sub_info
opencur
fetchcur
while(@@fetch_status=0)
begin
/*取收件人的电子邮件地址*/
fetchnextfromcurinto@recipient
/*发送电子邮件*/
if@@fetch_status=0
begin
exec@status=master..xp_sendmail
@recipients=@recipient,
@message=@content,
@subject=@subject
if@status<>0
/*错误处理代码*/
end
end
/*结束向所有订阅人发送电子刊物的循环*/
closecur
deallocatecur
end
GO
具体的存储过程和参数说明可查阅相关文档。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 基于pygame实现俄罗斯方块
![](https://img.aspxhome.com/file/2023/0/113050_0s.jpg)
浅析MySQL replace into 的用法
详解python中docx库的安装过程
![](https://img.aspxhome.com/file/2023/6/110036_0s.jpg)
python中的集合及集合常用的使用方法
![](https://img.aspxhome.com/file/2023/2/77832_0s.png)
vue如何获取指定元素
Mysql四种分区方式以及组合分区落地实现详解
使用selenium自动控制浏览器找不到Chromedriver问题
![](https://img.aspxhome.com/file/2023/2/131932_0s.jpg)
pandas函数isnull的具体使用
![](https://img.aspxhome.com/file/2023/9/69469_0s.png)
python 5个实用的技巧
恢复被删除的数据 Log Explorer for SQL Server 4.2 (一)
![](https://img.aspxhome.com/file/UploadPic/20107/1/le001-56s.jpg)
python实现单张图像拼接与批量图片拼接
![](https://img.aspxhome.com/file/2023/0/59300_0s.jpg)
女神相册密码忘记了 我只用Python写了20行代码
![](https://img.aspxhome.com/file/2023/5/100765_0s.jpg)
VMWare 虚拟机Centos7安装Oracle数据库的教程图解
![](https://img.aspxhome.com/file/2023/5/66085_0s.png)
在Linux下搭建Git服务器的方法详解
Visual Studio 2017使用EF选择MySQL数据源
![](https://img.aspxhome.com/file/2023/0/75530_0s.jpg)
如何Tkinter模块编写Python图形界面
![](https://img.aspxhome.com/file/2023/3/97273_0s.jpg)
pytorch 中transforms的使用详解
![](https://img.aspxhome.com/file/2023/8/110418_0s.jpg)
mysql installer web community 5.7.21.0.msi安装图文教程
![](https://img.aspxhome.com/file/2023/2/115572_0s.png)
vue-music关于Player播放器组件详解
![](https://img.aspxhome.com/file/2023/4/133024_0s.jpg)