1 回答

TA貢獻2041條經(jīng)驗 獲得超4個贊
創(chuàng)建測試表,數(shù)據(jù):
create table table1
(a1 int,
a2 int)
create table table2
(b1 int,
b2 int)
insert into table1 values (1,1)
insert into table1 values (1,2)
insert into table2 values (0,1)
insert into table2 values (1,2)
創(chuàng)建觸發(fā)器:
create trigger t_update
on table1
for update
as
declare @a2 int
declare @b1 int
declare @cnt int
select @cnt=count(*) from inserted where a2 in (select b2 from table2)
select @b1=b1 from table2 where b2=(select a2 from inserted)
if (@cnt>0 and @b1=1)
begin
print '不允許修改'
rollback transaction
end
測試1:修改a2=1的那條數(shù)據(jù)里的a1為其他值:
1
update table1 set a1=100 where a2=1 -- 這個是允許修改的
測試2:修改a2=2的那條數(shù)據(jù)a1為其他值:
1
update table1 set a1=10 where a2=2 -- 這個是不允許修改的,會報錯
剩下的你自己改改吧,大概思路就這樣了。
- 1 回答
- 0 關(guān)注
- 746 瀏覽
添加回答
舉報