日期:2014-05-17 浏览次数:20648 次
create trigge dbo.a_U_b on dbo.a for update,delete
as
--update,delete
if exists(select 1 from b join deleted a on a.id=b.id)
begin
rollback tran
raiserror('不能修改,删除记录',16,1)
return
end
if OBJECT_ID('b') is not null
drop table b
go
if OBJECT_ID('a') is not null
drop table a
go
create table a(id int primary key,v varchar(10) )
insert into a
select 1,'aa' union all
select 2,'111'
create table b(
id int ,a_id int ,vv varchar(10)
)
insert into b
values(2,1,'bb')
go
create trigger dbo.trigger_a
on a
for delete,update
as
if exists(select *
from deleted d
inner join b
on b.a_id= d.id )
rollback
go
--不能删除
delete from a where id = 1
/*
消息 3609,级别 16,状态 1,第 1 行
事务在触发器中结束。批处理已中止。
*/
--不能修改
update a
set v = 'xxx'
where id= 1
/*
消息 3609,级别 16,状态 1,第 1 行
事务在触发器中结束。批处理已中止。
*/
--id 为2的可以修改,可以删除
update a
set v = 'xxx'
where id= 2
delete from a where id = 2
create table&nb