需要创建 sql 触发器以向客户发送 html 电子邮件

Need to create sql trigger that will send out html email to a customer

我需要通过 sql 服务器 2005 进行一些设置,它会自动向新客户发送欢迎信息。通过Prophet 21,每次有新客户进入系统,系统都会给他们一个唯一的9位数字。这些存储在名为 "customer" 的 table 中。我的数据库电子邮件已经设置好,并且我有个人资料。

我在想这样的事情应该行得通吗?

    IF EXISTS (SELECT 1 FROM inserted WHERE (customer id is not already present in table))
    BEGIN
        EXEC msdb.dbo.sp_send_dbmail
          @recipients = 'whoever@customeremail.com', 
          @profile_name = 'P21 Alert',
          @subject = 'Welcome!',
          @body_format = 'HTML'
          @body = 'Thank you for ordering product <product ID>, welcome to our company';
    END
END
GO`    

请帮助或指出正确的方向!

您的触发器需要考虑一次可以插入多于 1 行的事实(想想 INSERT INTO... SELECT .. FROM)。

由于您想要分别向每位客户发送电子邮件,您几乎是看着光标一次循环浏览一行,您绝对不会希望你的触发器主体中有一个光标,因为它很可能会导致性能问题。

因此,您希望触发器将行插入 holding/queue table,然后编写 SQL 作业以从游标中的 table 读取行循环并发送电子邮件。