--创建测试测试用表
create table test_log(id int identity(1,1),action varchar(10),content varchar(100),times datetime)
create table test(id int,name varchar(100))
go--创建测试用触发器
create trigger trg_test
on test
for insert,update,delete
as
begin    if not exists(select 1 from deleted)
    begin
        insert into test_log(action,content,times) 
        select 'insert','id='+rtrim(id)+';name='+name,getdate() from inserted
    end
    else
    if not exists(select 1 from inserted)
    begin
        insert into test_log(action,content,times) 
        select 'delete','id='+rtrim(id)+';name='+name,getdate() from deleted
    end
    else
    if exists(select 1 from deleted) and exists(select 1 from inserted)
    begin
        insert into test_log(action,content,times) 
        select 'update','id='+rtrim(a.id)+';oldname='+b.name+';newname='+a.name,getdate() from inserted a,deleted b
    end
end
go--执行各类触发操作
insert into test select 1,'AAAA'
insert into test select 2,'BBBB'
delete test from test where id=2
update test set name='CCCC' where id=1
--查看结果
select * from test_log
go
--删除测试环境
drop trigger trg_test
drop table test,test_log
go