[转]create a basic sql server 2005 trigger to send email alerts
本文转自: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 ) GONow 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 GOThe 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的更多相关文章
- SQL Server 2005 中实现通用的异步触发器架构
在SQL Server 2005中,通过新增的Service Broker可以实现异步触发器的处理功能.本文提供一种使用Service Broker实现的通用异步触发器方法. 在本方法中,通过Serv ...
- 解密SQL SERVER 2005加密存储过程,函数
在SQL SERVER 2005中必须用专用管理连接才可以查看过程过程中用到的表 EG:sqlcmd -A 1>use test 2>go 1>sp_decrypt 'p_testa ...
- SQL Server 2005入门到精通(案例详解)
SQL Server 2005基础应用 一.数据库的基本操作 --创建数据库 create database new_db2 on primary ( name='new.mdf', filena ...
- 记一次sql server 2005访问http接口,并解析json的过程
记一次sql server 2005访问http接口,并解析json的过程 JSON解析官方网站:https://www.red-gate.com/simple-talk/sql/t-sql-pro ...
- SQL Server 2005 中实现通用的异步触发器架构 (转)
在SQL Server 2005中,通过新增的Service Broker可以实现异步触发器的处理功能.本文提供一种使用Service Broker实现的通用异步触发器方法. 在本方法中,通过Serv ...
- 深入理解SQL Server 2005 中的 COLUMNS_UPDATED函数
原文:深入理解SQL Server 2005 中的 COLUMNS_UPDATED函数 概述 COLUMNS_UPDATED函数能够出现在INSERT或UPDATE触发器中AS关键字后的任何位置,用来 ...
- 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) ) 案例介绍: 对一个数据 ...
- SQL SERVER 2005/2008 中关于架构的理解(二)
本文上接SQL SERVER 2005/2008 中关于架构的理解(一) 架构的作用与示例 用户与架构(schema)分开,让数据库内各对象不再绑在某个用户账号上,可以解决SQL SERVE ...
- sql server 2005导出数据到oracle
一. 在sql server下处理需要导出的数据库 1. 执行以下sql,查出所有'float'类型的字段名,手动将float类型改为decimal(18,4). select 表名=d.name,字 ...
随机推荐
- python_day4学习笔记
一.内置函数
- php之trait-实现多继承
PHP是单继承的语言,在PHP 5.4 Traits出现之前,PHP的类无法同时从两个基类继承属性或方法.php的Traits和Go语言的组合功能类似,通过在类中使用use关键字声明要组合的Trait ...
- redis之(九)redis的事务机制
[一]什么是redis的事务 --->redis的事务是一组命令的集合. --->redis的事务是保证一组命令,要么都执行,要么都不执行.但不支持一组命令中,其中一个或多个执行失败,不支 ...
- numpy及scipy的使用
numpy的使用 把list A转换为numpy 矩阵 np.array(A) np.array(A, 'int32') numpy加载txt文件里面的矩阵 matrix = np.loadtxt(t ...
- 如何在SQL Server中的SELECT TOP 中使用变量
语法 [ TOP (expression) [PERCENT] [ WITH TIES ] ] 注意:expression 是在一对圆括号内的,而之后又有如下的例子 在 TOP 中使用变量 以下示 ...
- 【剑指offer】面试题 2. 实现 Singleton 模式
面试题 2. 实现 Singleton 模式 题目:设计一个类,我们只能生成该类的一个实例. 单例模式:确保一个类只有一个实例,并提供了一个全局访问点. Java 实现 1.饿汉模式 //饿汉模式 p ...
- python正则的中文处理(转)
匹配中文时,正则表达式规则和目标字串的编码格式必须相同 print sys.getdefaultencoding() text =u"#who#helloworld#a中文x#" ...
- [Sgu395][bzoj2363]Binary Cat Club
一道神题…… rzO 发现立杰在初三(http://hi.baidu.com/wjbzbmr/item/4a50c7d8a8114911d78ed0a9据此可以推断)就怒A了此题…… Orz /*** ...
- 关于lower_bound的优先级重载
今天才知道$lower\_bound$最后有一个优先级参数…… 首先$lower\_bound$中的优先级和序列优先级必须相同才有效 $lower\_bound$中优先级默认的是小于号,也就是说仅当序 ...
- 【计算几何】【极角排序】【二分】Petrozavodsk Summer Training Camp 2016 Day 6: Warsaw U Contest, XVI Open Cup Onsite, Sunday, August 28, 2016 Problem J. Triangles
平面上给你n(不超过2000)个点,问你能构成多少个面积在[A,B]之间的Rt三角形. 枚举每个点作为直角顶点,对其他点极角排序,同方向的按长度排序,然后依次枚举每个向量,与其对应的另一条直角边是单调 ...