create trigger tr_tab_update on tab for update asif update (物料名称) or update (数量) begin RAISERROR ('不能修改物料名称和数量',16, 1) ROLLBACK TRANSACTION endgo
if object_id('tb') is not null drop table tb gocreate table tb(物料名称 varchar(50),数量 int,生产单位 varchar(50)) insert into tb select 'W001',100,'A1' insert into tb select 'W002',200,'B1' go create trigger tri_name on tb instead of update as begin if update(生产单位) update a set 生产单位=b.生产单位 from tb a inner join inserted b on a.物料名称=b.物料名称 else begin raiserror 50005 N'不允许修改其他列' rollback end end go
create trigger test on tb1 for update asif update(N'物料名称') or update(N'数量') begin Rollback tran --回滚更改
--报错误信息 raiserror 99999 N'不允许 物料名称 或者 数量 列'
end
if object_id('tb') is not null drop table tb gocreate table tb(物料名称 varchar(50),数量 int,生产单位 varchar(50)) insert into tb select 'W001',100,'A1' insert into tb select 'W002',200,'B1' gocreate trigger test on tb for update as if update(N'物料名称') or update(N'数量') begin Rollback raiserror 50005 N'不允许修改其他列' end
on tab
for update
asif update (物料名称) or update (数量)
begin
RAISERROR ('不能修改物料名称和数量',16, 1)
ROLLBACK TRANSACTION
endgo
drop table tb
gocreate table tb(物料名称 varchar(50),数量 int,生产单位 varchar(50))
insert into tb select 'W001',100,'A1'
insert into tb select 'W002',200,'B1'
go
create trigger tri_name
on tb
instead of update
as
begin
if update(生产单位)
update a set 生产单位=b.生产单位 from tb a inner join inserted b on a.物料名称=b.物料名称
else
begin
raiserror 50005 N'不允许修改其他列'
rollback
end
end
go
for update
asif update(N'物料名称') or update(N'数量')
begin
Rollback tran --回滚更改
--报错误信息
raiserror 99999 N'不允许 物料名称 或者 数量 列'
end
drop table tb
gocreate table tb(物料名称 varchar(50),数量 int,生产单位 varchar(50))
insert into tb select 'W001',100,'A1'
insert into tb select 'W002',200,'B1'
gocreate trigger test on tb
for update
as
if update(N'物料名称') or update(N'数量')
begin
Rollback
raiserror 50005 N'不允许修改其他列'
end