简体   繁体   English

SQL触发批处理文件

[英]SQL to trigger batch file

We are using SQL Server 2008R2 to handle a mail queue. 我们正在使用SQL Server 2008R2处理邮件队列。 New mail is entered into the queue and then once every minute a batch file reads the unprocessed mails, does some processing, sends the mail via smtp and flags the mail in the queue as sent (records is not deleted from queue because table also serves as mail history table). 将新邮件输入队列,然后每隔一分钟,批处理文件就会读取未处理的邮件,进行一些处理,通过smtp发送邮件,并将队列中的邮件标记为已发送(记录不会从队列中删除,因为表也用作邮件历史记录表)。

The problem with this method is that it is run only once a minute so mails are not sent out "automatically", and the batch file runs every minute even when there is no outgoing mail for hours. 这种方法的问题在于,它每分钟仅运行一次,因此不会“自动”发送邮件,并且即使几小时内没有外发邮件,批处理文件也会每分钟运行一次。

Is there a way for SQL server to run the batch file on each new item inserted into the mail queue? SQL Server是否可以在插入邮件队列的每个新项目上运行批处理文件? I know there are triggers but from what i understand they are asynchronous so if the batch file takes time it will hold up sql server 我知道有触发器,但据我了解它们是异步的,因此如果批处理文件需要时间,它将触发sql server

I would create a SQL server agent job to run the batch file using a cursor in the job in case there are multiple rows that are updated at once. 我将创建一个SQL Server代理作业,以在作业中使用光标运行批处理文件,以防一次有多个行被更新。 Create a trigger on the table that runs the agent job. 在运行代理作业的表上创建触发器。 This won't hold up SQL server since it's running through SQL agent. 由于它通过SQL代理运行,因此不会阻止SQL Server。 The rows will be processed and the emails sent out immediately. 将处理行,并立即发送电子邮件。

Are you talking about sending emails from SQL Server? 您是在谈论从SQL Server发送电子邮件吗?

http://msdn.microsoft.com/en-us/library/office/aa155737(v=office.10).aspx http://msdn.microsoft.com/en-us/library/office/aa155737(v=office.10).aspx

If so, just incorporate the send into your procedure. 如果是这样,只需将send合并到您的过程中即可。

If not, assuming you have IIS, don't try to do your own queue. 如果没有,假设您有IIS,请不要尝试做自己的队列。 Create a CLR procedure that adds the email to a .NET queue somewhere such as your batch server. 创建一个CLR过程,将电子邮件添加到某个地方(例如批处理服务器)的.NET队列中。 You can then run a process on your batch server that to read the queue and send the email. 然后,您可以在批处理服务器上运行一个进程,以读取队列并发送电子邮件。 Don't run it periodically - just keep it in a loop that does a blocking read the queue and sends mail. 不要定期运行它-只需将其保持在一个循环中,该循环会阻塞读取队列并发送邮件。 (You may have to add it to startup/run it periodically or something for resilience and DR reasons). (您可能必须将其添加到定期启动/运行中,或者出于弹性和灾难恢复的原因而添加)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM