A. 使用带有提醒消息的触发器
当有人试图在 titles 表中添加或更改数据时,下例将向客户端显示一条消息。说明 消息 50009 是 sysmessages 中的用户定义消息。有关创建用户定义消息的更多信息,请参见 sp_addmessage。
USE pubs
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'reminder' AND type = 'TR')
DROP TRIGGER reminder
GO
CREATE TRIGGER reminder
ON titles
FOR INSERT, UPDATE
AS RAISERROR (50009, 16, 10)
GO
当有人试图在 titles 表中添加或更改数据时,下例将向客户端显示一条消息。说明 消息 50009 是 sysmessages 中的用户定义消息。有关创建用户定义消息的更多信息,请参见 sp_addmessage。
USE pubs
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'reminder' AND type = 'TR')
DROP TRIGGER reminder
GO
CREATE TRIGGER reminder
ON titles
FOR INSERT, UPDATE
AS RAISERROR (50009, 16, 10)
GO
触发器执行sql语句或存储过程,因此你可以在存储过程中执行外部调用,或者调用sql语句或系统的存储过程,比如楼上的 RAISError 返回错误信息,或 print,或发送邮件 xp_sendmail 等等。
xp_cmdshell 这个是否允许我就没试过啦,应该只要有master权限是可以的,hoho
go
drop proc send_message
go
create proc send_message as begin
create table #tspid(
spid int null,
ecid int null,
status varchar(20) null,
loginname varchar(20) null,
hostname varchar(20) null,
blk bit null,
dbname varchar(20) null,
cmd varchar(20)
)insert into #tspid(spid,ecid,status,loginname,hostname,blk,dbname,cmd) exec sp_who create table #userip(id int identity,txt varchar(1000))--可以得到hostName
declare @cmdStr varchar(100), @hostName varchar(30), @userip varchar(20), @sendstr varchar(100)declare tspid cursor for select hostname from #tspid where spid>50
open tspid fetch next from tspid into @hostname
WHILE @@FETCH_STATUS = 0
begin
select @cmdStr='ping '+@hostName
insert into #table(txt) exec master..xp_cmdshell @cmdStr select @userip=substring(txt,charindex('[',txt)+1,charindex(']',txt)-charindex('[',txt)-1) from #table where id=2 --得到IP地址,也可以不直接用hostName
select @sendstr='net send '+@userip+'Messages!'
exec master..xp_cmdshell @sendstr
fetch next from tspid into @hostname
end drop table #tspid
drop table #userip
endgo