数据的表A的格式如下
年 数量
2012 132
2011 332
1985 222
1984 332
触发器如何写,可以禁止update,insert,delete 2011年前的数量
------解决方案--------------------
- SQL code
create table 表A(年 int, 数量 int)insert into 表Aselect 2012, 132 union allselect 2011, 332 union allselect 1985, 222 union allselect 1984, 332;-- 建触发器create trigger tr_表A on 表A for update,delete,insertasbegin if exists(select 1 from inserted where 年<2011) or exists(select 1 from deleted where 年<2011) begin rollback transaction; raiserror(N'禁止update,insert,delete 2011年前的数量.',10,1); end end--> 测试update,insert,delete 2011年前的数量update 表A set 数量=611 where 年=1985/*禁止update,insert,delete 2011年前的数量.Msg 3609, Level 16, State 1, Line 1The transaction ended in the trigger. The batch has been aborted.*/insert into 表A select 1981,132/*禁止update,insert,delete 2011年前的数量.Msg 3609, Level 16, State 1, Line 1The transaction ended in the trigger. The batch has been aborted.*/delete from 表A where 年=1984/*禁止update,insert,delete 2011年前的数量.Msg 3609, Level 16, State 1, Line 1The transaction ended in the trigger. The batch has been aborted.*/--> 表数据未变更select * from 表A/*年 数量----------- -----------2012 1322011 3321985 2221984 332(4 row(s) affected)*/