第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關(guān)于SQL server觸發(fā)器的問題

關(guān)于SQL server觸發(fā)器的問題

FFIVE 2018-07-11 14:14:01
table1,table2兩表,table1表里面有a1,a2.字段,table2中有b1,b2字段。我想再table1表中添加一個觸發(fā)器,a2=b2 情況下 如果table2中b1=1時,table1中a1則允許修改,如果table2中b1=0時,則table1中a1不允許修改并提示 table2未審核
查看完整描述

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 -- 這個是不允許修改的,會報錯



剩下的你自己改改吧,大概思路就這樣了。


查看完整回答
反對 回復 2018-08-25
  • 1 回答
  • 0 關(guān)注
  • 746 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號