有个问题请教下
我有1张表account 表结构如下
id int
username varchar(17)
account int
我想记录下account字段变动的情况 (包括人工手动更改和程序update)
请问下可以实现不?
我有1张表account 表结构如下
id int
username varchar(17)
account int
我想记录下account字段变动的情况 (包括人工手动更改和程序update)
请问下可以实现不?
CREATE TRIGGER tr_Count
ON dbo.YourTable
AFTER UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON; -- Insert statements for trigger here
if update(username)
update a set account=isnull(account,0)+1 from YourTable a inner join inserted b on a.id=b.id
END
GO
for update
as
if update(account) and exists(select 1 from inserted i,deleted d where i.id=d.id and isnull(i.account,0)<>isnull(d.account,0))
begin
insert into account_log
select d.account, --旧值
i.account, --新值
getdate(), --更新时间
suser_sname() --更改人
from inserted i,deleted d
where i.id=d.id and isnull(i.account,0)<>isnull(d.account,0)
end