本文转自:http://blog.netnerds.net/2008/02/create-a-basic-sql-server-2005-trigger-to-send-e-mail-alerts/

For as many times as I have read about sending e-mails using SQL Server triggers, I've rarely come across actual code samples. After someone asked for a "Triggers for Dummies" example in a Facebook SQL group, I created the following example which uses a trigger to alert a manager that an expensive item has been entered into inventory.

First, if SQL Mail isn't enabled and a profile hasn't been created, we must do so.

--// First, enable SQL SMail
use master
go
 
sp_configure 'show advanced options',1
go
 
reconfigure with override
go
 
sp_configure 'Database Mail XPs',1
go
 
reconfigure
go
--//Now create the mail profile.
--//CHANGE @email_address,@display_name and @mailserver_name VALUES to support your environment
EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = 'DBMailAccount',
@email_address = 'sqlserver@domain.com',
@display_name = 'SQL Server Mailer',
@mailserver_name = 'exchangeServer'

EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = 'DBMailProfile'

EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = 'DBMailProfile',
@account_name = 'DBMailAccount',
@sequence_number = 1 ;
 

Now that SQL will support sending e-mails, let's create the sample table.

This is not a useful or well designed table by any means -- it's just a simple example table:

CREATE TABLE dbo.inventory (
item varchar(50), price money
)
GO

Now that SQL mail and the table are setup, we will create a trigger that does the following:

  • Creates an AFTER INSERT trigger named expensiveInventoryMailer on the inventory table.

This means that the trigger will be executed after the data has been entered.

  • Checks for items being entered that have a price of $1000 or more
  • If there is a match, an email is sent using the SQL Mail profile we used above.
CREATE TRIGGER expensiveInventoryMailer
ON dbo.inventory AFTER INSERT
AS
DECLARE @price money
DECLARE @item varchar(50)

SET @price  = (SELECT price FROM inserted)
SET @item = (SELECT item FROM inserted)

IF @price >= 1000
  BEGIN
    DECLARE @msg varchar(500)
    SET @msg = 'Expensive item "' + @item + '" entered into inventory at $' + CAST(@price as varchar(10)) + '.'
    --// CHANGE THE VALUE FOR @recipients
    EXEC msdb.dbo.sp_send_dbmail
        @recipients=N'manager@domain.com',
        @body= @msg, 
        @subject = 'SQL Server Trigger Mail',
        @profile_name = 'DBMailProfile'
   END
GO

The only way to test a trigger is to add actual data, so let's do that here:

insert into inventory (item,price) values ('Vase',100)

insert into inventory (item,price) values ('Oven',1000)

Your email should arrive very quickly.

If it doesn't, check the SQL Server mail log in SQL Management Studio by running SELECT * FROM sysmail_allitems.

Have fun!

另:

Database Mail and SQL Mail Stored Procedures (Transact-SQL)

http://msdn.microsoft.com/en-us/library/ms177580(v=sql.90).aspx

[转]create a basic sql server 2005 trigger to send email alerts的更多相关文章

  1. SQL Server 2005 中实现通用的异步触发器架构

    在SQL Server 2005中,通过新增的Service Broker可以实现异步触发器的处理功能.本文提供一种使用Service Broker实现的通用异步触发器方法. 在本方法中,通过Serv ...

  2. 解密SQL SERVER 2005加密存储过程,函数

    在SQL SERVER 2005中必须用专用管理连接才可以查看过程过程中用到的表 EG:sqlcmd -A 1>use test 2>go 1>sp_decrypt 'p_testa ...

  3. SQL Server 2005入门到精通(案例详解)

    SQL Server 2005基础应用   一.数据库的基本操作 --创建数据库 create database new_db2 on primary ( name='new.mdf', filena ...

  4. 记一次sql server 2005访问http接口,并解析json的过程

    记一次sql server 2005访问http接口,并解析json的过程  JSON解析官方网站:https://www.red-gate.com/simple-talk/sql/t-sql-pro ...

  5. SQL Server 2005 中实现通用的异步触发器架构 (转)

    在SQL Server 2005中,通过新增的Service Broker可以实现异步触发器的处理功能.本文提供一种使用Service Broker实现的通用异步触发器方法. 在本方法中,通过Serv ...

  6. 深入理解SQL Server 2005 中的 COLUMNS_UPDATED函数

    原文:深入理解SQL Server 2005 中的 COLUMNS_UPDATED函数 概述 COLUMNS_UPDATED函数能够出现在INSERT或UPDATE触发器中AS关键字后的任何位置,用来 ...

  7. SQL SERVER 2005删除维护作业报错:The DELETE statement conflicted with the REFERENCE constraint "FK_subplan_job_id"

    案例环境: 数据库版本: Microsoft SQL Server 2005 (Microsoft SQL Server 2005 - 9.00.5000.00 (X64) ) 案例介绍: 对一个数据 ...

  8. SQL SERVER 2005/2008 中关于架构的理解(二)

    本文上接SQL SERVER 2005/2008 中关于架构的理解(一)      架构的作用与示例 用户与架构(schema)分开,让数据库内各对象不再绑在某个用户账号上,可以解决SQL SERVE ...

  9. sql server 2005导出数据到oracle

    一. 在sql server下处理需要导出的数据库 1. 执行以下sql,查出所有'float'类型的字段名,手动将float类型改为decimal(18,4). select 表名=d.name,字 ...

随机推荐

  1. Springboot问题合集

    1. springboot错误: 找不到或无法加载主类 springboot错误: 找不到或无法加载主类 一般是由于maven加载错误导致的,而我遇到是因为module没有导入正确,重新导一下modu ...

  2. for in 对象时,属性为非负整数的情况

    在我做一个需求的时候 for in 一个对象,对象的属性都是数字 但是我想给这个对象加一个默认的属性跟值 原对象是{5446:"广州市"}.....类似于下去 然后我想给我页面展示 ...

  3. vue之v-text渲染多值

    其原理,是利用vue里的computed计算属性来做. 请看代码: <div id='app'> <div v-text="newUsers"></d ...

  4. ngx_lua_waf完整安装说明

    测试环境:centos5.6 x32,nginx1.4.4,LuaJIT-2.0.3,ngx_devel_kit-0.2.19.lua-nginx-module-0.9.11,ngx_lua_waf ...

  5. 2018CCPC 中国大学生程序设计竞赛 网络赛

    链接 1.括号序列贪心/CF&51nod原题 [分析]: 贪心,每次到i的时候,假如你要在i里面要卖掉股票,获益是a[i], 肯定要在前面要么:1)把已经卖了的变成不买不卖,需要-a[j], ...

  6. 关于公众号JavaTokings侵权声明

    该公众号几乎有所有文章都是在未经原作者的同意下私自将文章转移至其公众号.其中 [消息中间件ActiveMQ使用详解](链接是:https://www.cnblogs.com/yanfei1819/p/ ...

  7. Oracle常用常考集合

    登陆远程服务器 sqlplus scott/tiger@192.168.2.1[:port]/sid [as sysdba] 简单查询 select  table_name from user_tab ...

  8. SPOJ11469 Subset(折半枚举)

    题意 给定一个集合,有多少个非空子集,能划分成和相等的两份.\(n\leq 20\) 题解 看到这个题,首先能想到的是\(3^n\)的暴力枚举,枚举当前元素是放入左边还是放入右边或者根本不放,但是显然 ...

  9. 【Go】windows下搭建go语言编译环境

    主要是协助杨哥做Kubernetes相关工作,由于Kubernetes和Docker都是由Go语言编写,因此改源码后还是需要go语言编译器来编译运行.所以打算先在windows上安装一下go语言环境. ...

  10. Funny Car Racing CSU - 1333 (spfa)

    There is a funny car racing in a city with n junctions and m directed roads. The funny part is: each ...