请帮解决两个(也是4个)问题,每个100分,谢谢!
------------------------/****** 建表******/------------------------if object_id('p') is not nulldrop table pgocreate table p(name nvarchar(20) not null,ver nvarchar(20) not null,active bit null,constraint [pk_p] primary key(name,ver))go------------------------/****** 建触发器******/------------------------create trigger p_updon pfor updateasif update(active) and (select count(1) from inserted)>1begin rollback tran; raiserror('每次只能更新一条',11,1); return;endif update(active)begin update p set active=0 from inserted i where i.name=p.name and i.ver<>p.ver and isnull(i.active,0)=1;endgo------------------------/****** 测试数据******/------------------------insert into p values('p1','a',0);insert into p values('p1','b',1);insert into p values('p1','c',0);go------------------------
if update(active) and (select count(1) from inserted)>1begin rollback tran; raiserror('每次只能更新一条',11,1); return;end
第一个问题update p set name='p2',ver='f',active=1 where name='p1' and ver='c'select * from p--下面这样的算不算?/*name ver active-------------------- -------------------- ------p1 a 0p1 b 1p2 f 1(3 行受影响)*/--如果对,要避免的话 把 and isnull(i.active,0)=1;--去掉,因为一次只能更新一行,那么它跟定的把0的更新成1才会出现两行--所以你只需要把更新的那一行在更新一遍(更新为0),就行了。
[解决办法]
------------------------/****** 建触发器******/------------------------ALTER trigger p_updon pfor update,INSERTasif update(active) and (select count(1) from inserted)>1begin rollback tran; raiserror('每次只能更新一条',11,1); return;endif update(active)begin update p set active=0 from inserted i where i.name=p.name and i.ver<>p.ver and isnull(i.active,0)=1;endgo