日期:2014-05-17 浏览次数:20571 次
--1在master数据库中建立服务器级别跟踪表
use master
go
create table server_eventdata
(eventdata xml,
principal_user nvarchar(100),
login_user nvarchar(100)
)
go
/*
select * from sys.trigger_event_types
where type_name like '%trigger%' or
type_name like '%deny%' or
type_name like '%revoke%'
*/
--2建立服务器级别触发器
--drop trigger gyy_server on all server
create trigger gyy_server
on all server
for CREATE_TRIGGER
as
insert into server_eventdata
select EVENTDATA(),USER,SUSER_NAME()
go
--3.建表,建触发器
--drop table wc_table
create table dbo.wc_table(v int)
go
--insert into dbo.wc_table values(1)
--go
create trigger dbo.tt_2
on dbo.wc_table
after insert
as
print 'dbo.tt_2'
go
--查看记录的事件
select EVENTDATA,
eventdata.value('(/EVENT_INSTANCE/EventType)[1]','nvarchar(100)') as '事件类型',
eventdata.value('(/EVENT_INSTANCE/TSQLCommand)[1]','nvarchar(100)') as 'sql授权语句',
'登录名' + eventdata.value('(/EVENT_INSTANCE/LoginName)[1]','nvarchar(100)') +
'用户名' + eventdata.value('(/EVENT_INSTANCE/UserName)[1]','nvarchar(100)') + ',授予者'+
eventdata.value('(/EVENT_INSTANCE/Grantor)[1]','nvarchar(100)') + ' 把类型为:' +
eventdata.value('(/EVENT_INSTANCE/ObjectType)[1]','nvarchar(100)') + '的对象' +
eventdata.value('(/EVENT_INSTANCE/DatabaseName)[1]','nvarchar(100)') + '.' +
eventdata.value('(/EVENT_INSTANCE/SchemaName)[1]','nvarchar(100)') + '.' +
eventdata.value('(/EVENT_INSTANCE/ObjectName)[1]','nvarchar(100)') +&n