日期:2014-05-17  浏览次数:20381 次

sqlserver 触发器问题
写了一个触发器,当表增删改的时候写入日志表。但是刚才发现如果一次删除一条数据的时候是写入日志表一条数据,但是如果一次删除2条以上的数据,日志表里还是 显示删除了一条数据,怎么破。附触发器代码,求大神搭救,谢谢

create trigger goodMan
on t_good
for insert,delete,update
as
declare @gid int
declare @gname nvarchar(50)
declare @gmancount int

select @gid=gid,@gname=gname,@gmancount=gcount from inserted where gid is not null
begin
if exists(select gid from inserted) and not exists(select gid from deleted)
begin 
insert into t_goodiolog (gid,gname,gmancount,gmankind,gdate) values(@gid,@gname,@gmancount,'insert',GETDATE())
end
end

------解决方案--------------------
试试
create trigger goodMan
on t_good
for insert,delete,update
as
declare @gid int
declare @gname nvarchar(50)
declare @gmancount int

select @gid=gid,@gname=gname,@gmancount=gcount from inserted where gid is not null
begin
if exists(select gid from inserted) and not exists(select gid from deleted)
begin 
insert into t_goodiolog (gid,gname,gmancount,gmankind,gdate)
SELECT @gid,@gname,@gmancount,'insert',GETDATE()
 FROM deleted
end
end

------解决方案--------------------
create trigger goodMan
on t_good
for insert,delete,update
as
declare @gid int
declare @gname nvarchar(50)
declare @gmancount int

--select @gid=gid,@gname=gname,@gmancount=gcount from inserted where gid is not null

begin

if exists(select gid from inserted) and not exists(select gid from deleted)
begin 
insert into t_goodiolog (gid,gname,gmancount,gmankind,gdate) 
select gid,gname,gcount,'insert',GETDATE() from inserted where gid is not null
end

end